ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Script to change composition length depending on the length of a layer

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Ole Peters
Script to change composition length depending on the length of a layer
on Oct 29, 2015 at 4:34:57 pm

Hello,
I'm really new to scripting and want to write a script which trims the Main composition depending to the length of the layer from another comp. I already have this:
var mComp = app.project.activeItem; //Set a variable equal to the active music composition
var mLayer = mComp.selectedLayers[0]; //Set a variable for the selected layer


if (mComp) {
if (mLayer) {

} else {
alert("Please select the song layer inside the music composition")
}
} else {

}

I know I could just leave out the mComp if statement but oh well.
Cheers


Return to posts index

Ole Peters
Re: Script to change composition length depending on the length of a layer
on Oct 30, 2015 at 12:03:37 am
Last Edited By Ole Peters on Oct 30, 2015 at 12:08:46 am

Since no one replied yet and I'm still hoping for at least Dan to reply I thought I'd rewrite the question. I really need to get that script to work, so please..

I have a Project file with multiple compositions, 2 of those named "Music" and "Main".
I'm in the Music composition and the script is supposed to be executed inside of the Music composition while having the song layer selected. Now I want the script to take the length of the song layer and trim the Main composition to the length of the song. Also, onClick doesn't seem to work.
I have a basic GUI with a button and the onClick line would be for example this:
tabContainer1.onClick = function () {alert("Hello World")} I don't get anything though, when pressing the button.

Again, I really need to get this to work and would really really appreciate if someone could help me out with this..


Return to posts index


Dan Ebberts
Re: Script to change composition length depending on the length of a layer
on Oct 30, 2015 at 12:51:07 am

Something like this maybe:


function main(){
var mainComp = null;
for (var i = 1; i <= app.project.numItems; i++){
if (app.project.item(i) instanceof CompItem && app.project.item(i).name == "Main"){
mainComp = app.project.item(i);
break;
}
}
if (mainComp == null){
alert ("Can't find main comp.");
return;
}
if (app.project.activeItem == null || !(app.project.activeItem instanceof CompItem)){
alert ("No comp selected.");
return;
}
var musicComp = app.project.activeItem;
if (musicComp.selectedLayers.length == 0){
alert ("No layer selected.");
return;
}
var musicLayer = musicComp.selectedLayers[0];
mainComp.duration = musicLayer.source.duration;
}
main();



Dan



Return to posts index


Ole Peters
Re: Script to change composition length depending on the length of a layer
on Oct 30, 2015 at 1:57:39 pm

Awesome, works perfectly!
Thank you soo much Dan!


Return to posts index

Teddy Denton
Re: Script to change composition length depending on the length of a layer
on Sep 13, 2019 at 7:13:40 pm

I'm trying to do this same thing. Ole Peters/Dan, where are you inserting this script?


Return to posts index

Dan Ebberts
Re: Script to change composition length depending on the length of a layer
on Sep 13, 2019 at 7:28:38 pm

>where are you inserting this script?

Maybe you're thinking this is an expression? It's a script so you can either run it from the ExtendScript script editor, or save it as a .jsx file and run it from:

File > Scripts > Run Script File...


Dan



Return to posts index


Teddy Denton
Re: Script to change composition length depending on the length of a layer
on Sep 13, 2019 at 7:33:42 pm

Got it. Thanks, Dan.


Return to posts index

Teddy Denton
Re: Script to change composition length depending on the length of a layer
on Sep 13, 2019 at 7:52:20 pm

Dan, I'm curious about expanding on this and would love if you'd be willing to give some direction.

As context, what I'm trying to do is create a project that's just a simple photo slideshow set to music. I've written some simple expressions to get the photo comps to do a Ken Burns-y move and transition into each other, added some text, etc.

I've been trying to figure out a way to (a) set up the main composition's duration to respond to the length of the audio layer I insert and (b) adjust the duration of the photo comps so that they evenly distribute across the duration of the audio layer.

Your solution worked wonderfully for my first (a) problem. For the second (b) issue, do you have any suggestions or resources that would help me solve this problem? I'm coming at this from a design background but trying to get more into scripting. Any help is much appreciated.

Thanks!


Return to posts index

Dan Ebberts
Re: Script to change composition length depending on the length of a layer
on Sep 13, 2019 at 8:12:42 pm

I guess I'd start with the audio duration divided by the number of photos, call that variable dur. I'd stack the photos from bottom to top with the first one (bottom in the layer stack) fading up at time = 0, the next one at time = dur, the next one at time = 2*dur, etc. You might have to make adjustments maybe for a delay before the fade up of the first photo, fade out of the last photo, etc. Pretty straightforward though, I think.


Dan



Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]