Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Script that changes the duration of some compositions and arrange them on timeline

  • Script that changes the duration of some compositions and arrange them on timeline

    Posted by Nuria Sanchez on February 25, 2016 at 5:51 pm

    Hello,
    a couple of mounths ago i have dicoverd After Effects and i started learn to work with it, but now i am stuck with my new project, becouse my scripting and expressions skills are almost 0.

    The project looks like this Project screenshot,and it has:
    3 audio files named audio 1.wav, audio 2.wav, audio 3.wav
    3 compositions named Song 1, Song 2, Song 3 wich include each one the coresponding audio file
    1 composition named Render Comp which include the 3 Song compositions.

    My intention is to create some kind of template, becouse i will have to render
    some videos with different audio files.

    My question is: is it posible to create a script which:

    1. on click changes the duration of each “Song” composition using the length of its corresponding audio file
    2. arrange the “Song” compositions one after each other in the Render Comp, and cut the Render Comp duration at the end of Song 3 composition?

    If is possible, can i have a little help with the code please, if is not too much, because i am quite new in scripting and i don’t have any idea how to do it. Thanks in advance

    Avinash Ramanath replied 7 years, 9 months ago 3 Members · 9 Replies
  • 9 Replies
  • Nuria Sanchez

    February 26, 2016 at 12:51 am

    For the second point i have created a script already and it is working…i only need help with the script for the first part please

  • Miguel De mendoza

    February 26, 2016 at 2:02 am

    Is there any reason you want to do it through a script?

    According to the screenshot you’ve uploaded, you can do it by simply dragging the audio tracks from your project panel on the Create comp icon, or by right click on the tracks and click on “Create comps from selection”

  • Nuria Sanchez

    February 26, 2016 at 9:39 am

    The composition will be much more complex…will have maybe 20 audio files, and different other footages…so changing this stuff manually each time when i do a video like this is very time consuming….do you think that can be done with the help of a script?

  • Miguel De mendoza

    February 26, 2016 at 11:33 am

    Yes, it can be done by many ways. The key is to stablish a way to find the correct layer on each comp. Let’ say that you always have your song in the last layer of each comp, you can do this:

    var proj = app.project;

    function chageCompsDuration() {
    var selectedComps = proj.selection;
    if(selectedComps.length == 0) {
    alert("Select one or more comps");
    }

    for (var i = 0; i < selectedComps.length; i++){
    var curComp = selectedComps[i];
    var layerIndex = curComp.layers.length; //change this for other search type
    var audioLayer = curComp.layer(layerIndex);

    if( audioLayer.hasAudio == false || audioLayer.hasVideo == true) {
    alert("Layer" + audioLayer.name + "in Comp" + curComp.name + "is not an audio file");
    }

    curComp.duration = audioLayer.source.duration;
    }
    }

    chageCompsDuration()

    Then you only have to select the comps you want to change and run the script (or use the function in a onClick event, anywhere you want).

  • Nuria Sanchez

    February 26, 2016 at 2:21 pm

    Thanks for helping me, the code seems not to work with my project…the one that i have attatched, it gives a error at line 12:

    var audioLayer = curComp.layer(layerIndex);

    i have selected all 3 compositions ‘Song 1’ ‘Song 2’ and ‘Song 3’ together, i have tryed with each one individual and nothing…same error..

  • Miguel De mendoza

    February 27, 2016 at 4:46 pm

    Can you write the full error code?

  • Nuria Sanchez

    March 2, 2016 at 1:15 pm

    Well it does not say anything…it stops and the 12 line is in red…

    Anyway, i manage to do what i needed using a script, but i have to take all the comps one by one…it is not a big problem…

    Thanks for trying to help me.

  • Avinash Ramanath

    April 21, 2017 at 7:40 pm

    Try This

    for example:
    var audioLayer = curComp.layer(2); this means the layer number of your audio file is 2

  • Avinash Ramanath

    April 26, 2017 at 7:02 am

    ” For the second point i have created a script already and it is working…i only need help with the script for the first part please ”
    Can you please share that script with me. It will be a huge help.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy