Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions extendscript to pre-compose selected layers, moving all attributes and adjust composition to the time span of the selected layers

  • extendscript to pre-compose selected layers, moving all attributes and adjust composition to the time span of the selected layers

  • Chris Orcfilm

    September 17, 2019 at 3:59 am

    So, I’m surprised I can’t find this on the web, but I’m trying to write a script to essentially precompose selected layers like you would manually by Ctrl/cmd+Shift+C, selecting to move all attributes and adjust the composition to the time span of the selected layers.

    I’ve found a great case study tweaked by Dan Ebberts from an idea by Brian Maffitt and coded by Keiko Yamada here:

    But alas, it doesn’t seem to behave like the manual steps in AE.

    It takes layer(s), Pre-composes them and sets the in/out point of the layer in the original comp, but does not trim the content of the resulting comp to the in/out points of the layer(s). Instead it retains the full length of the parent comp unlike when you Ctrl/cmd+Shift+C, selecting to move all attributes and adjust the composition to the time span of the selected layers.

    What would be the best way to go into the resulting pre-comp, and making the work area (trimmed) to the in and out points of the layers so it behaves more like the manual process?

    New to extendscript so please forgive me if I’m overlooking the obvious.

    BC Canada

  • Tomas Bumbulevičius

    September 22, 2019 at 10:08 am

    Hey Chris, I just checked that script, and my questions are, assuming that pre-comp is built, and is auto-added in the main comp, and with these sample conditions:

    Main comp length: – 60s long;
    1st layer in point – starts at 10s;
    xth layer out point – ends and 40s;

    Now when script executed:

    1. Would you prefer that ‘pre-comp’ actually would have its start point at the area where current in point belongs? Because now, pre-comp turns out to be the same length as main comp, but in/out points are trimmed to 1st and xth layers times.
    2. Accordingly, in this sample above, pre-comp shall turn out to be 30s long instead, or not necessarily this way?

    From what I see, and how I like things to be pre-composed, is that every pre-comps starting layer, would begin at 0s, instead of matching main-comp length. Is this is the same behaviour you are after?

    Find out more:
    After Effects Tutorials: motion design, expressions, scripting.
    Boxer – Dynamic Text Boxes Template with a Live Preview

  • Chris Orcfilm

    September 22, 2019 at 11:28 pm

    Thanks Tomas, yes, I wanted the generated precomp to be trimmed to the length of the layer(s) chosen, much like the behaviour if you Ctrl/cmd+Shift+C, selecting to “move all attributes…” AND select to “adjust the composition to the time span of the selected layers”.

    That said, in lieu of any feedback I managed to figure it out. There are still a couple of bugs, but I will post the solution here once it’s cleaned up.



    BC Canada

  • Alexey Boyarin

    September 22, 2020 at 12:51 am

    I’m trying to write a script and have the same issue. Have you found way to make it? Will appreciate any feedback.

  • Filip Vandueren

    September 22, 2020 at 8:25 am

    Something like this:

    thisComp = app.project.activeItem;
    selLayers = thisComp.selectedLayers;
    min_inPoint = Math.min.apply(0, (l) {return l.inPoint} ));
    max_outPoint = Math.max.apply(0, (l) {return l.outPoint} ));
    layers_duration = max_outPoint - min_inPoint;
    newComp = thisComp.layers.precompose( (l) {return l.index}), "precomp", true);
    for (l=1; l<=newComp.numLayers; l++) newComp.layer(l).startTime-=min_inPoint;
    newComp.duration = layers_duration;
    thisComp.selectedLayers[0].startTime = min_inPoint; // the precomp will now be the selected layer in our main comp
  • Baqer Asad

    March 15, 2021 at 4:55 pm

    I edited the code just a little :


    app.beginUndoGroup("Pre-Compose to Layer Duration");

    var myComp = app.project.activeItem;

    var copySelected = myComp.selectedLayers.slice();

    if(myComp instanceof CompItem) {

    var myLayers = myComp.selectedLayers;

    if(myLayers.length > 0){

    var newIndex = copySelected[copySelected.length-1].index;

    var myNewInPoint = copySelected[0].inPoint;

    var newInPoint = myLayers[0].inPoint;

    var newOutPoint = myLayers[0].outPoint;

    var newCompName = "comp ";

    var layerName = myLayers[0].name;

    if(layerName.length > 26) {

    layerName = layerName.substring(0, 26);


    newCompName += layerName;

    var layerIndices = new Array();

    for (var i = 0; i < myLayers.length; i++) {

    layerIndices[layerIndices.length] = myLayers[i].index;

    if (myLayers[i].inPoint < newInPoint) newInPoint = myLayers[i].inPoint;

    if (myLayers[i].outPoint > newOutPoint) newOutPoint = myLayers[i].outPoint;


    var newComp = myComp.layers.precompose(layerIndices, newCompName, true );

    var preCompLayer = myComp.selectedLayers[0];

    preCompLayer.outPoint = newOutPoint;

    newComp.workAreaStart = newInPoint;

    newComp.duration = newOutPoint;


    app.executeCommand(app.findMenuCommandId("Trim Comp to Work Area"));


    myComp.layer(newIndex).startTime = myNewInPoint;


    alert("select at least one layer to precompose.");



    alert("please select a composition.");




    but the problem with this code is that you have to select the layers from bottom to top or
    you will get an error
    maybe someone could enhance it ?

Viewing 1 - 6 of 6 posts

Log in to reply.

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