Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Ae Script: Adding generated layer to selected layers

  • Ae Script: Adding generated layer to selected layers

    Posted by Mike Pindara on October 24, 2016 at 5:07 pm

    Hey folks,

    I’m writing a script for some project management things in the studio I work at.
    I’ve got everything working except the most important part.

    With the selected layers, the script should create a precomp of the selected layers, and the duplicate of a master layer that is at the bottom of the comp. Right now, everything gets duplicated correctly, and precomped and trimmed correctly. I just can’t figure out how to add the duplicated layer into the selected array, so that it gets precomped as well.

    Any help? I posted the function below.

    function precompGroup(userLayers){
    // create undo group
    app.beginUndoGroup("Pre-Compose to Layer Duration");

    // select the active item in the project window
    // and make sure it's a comp
    var myComp = userLayers;
    if(myComp instanceof CompItem) {

    // make sure one or more layers are selected
    var myLayers = myComp.selectedLayers;
    if(myLayers.length > 0){

    // set new comp's default in and out points to those of first layer
    var newInPoint = myLayers[0].inPoint;
    var newOutPoint = myLayers[0].outPoint;

    //ensure master audio exists
    if(masterAudio instanceof CompItem){

    // create new comp name of "Scene " and global variable
    var newCompName = "Scene ";
    newCompName += currentShot;

    // "precompose" expects an array of layer indices
    var layerIndices = new Array();
    for (var i = 0; i < myLayers.length; i++) {
    layerIndices[layerIndices.length] = myLayers[i].index;

    // make sure new comp in point is in point of earliest layer
    // and new comp out point is out point of latest layer
    if (myLayers[i].inPoint < newInPoint) newInPoint = myLayers[i].inPoint;
    if (myLayers[i].outPoint > newOutPoint) newOutPoint = myLayers[i].outPoint;
    }

    //create ref audio layer
    var origAudio = app.project.activeItem.layer("0_Master Audio");
    var refAudio = origAudio.duplicate();
    refAudio.inPoint = newInPoint;
    refAudio.outPoint = newOutPoint;

    //addRef Audio to selected Layers

    //add index to layerIndicies

    // create the new comp
    var newComp = myComp.layers.precompose(layerIndices, newCompName, true );

    // set in and out points of new comp
    var preCompLayer = myComp.selectedLayers[0];
    preCompLayer.inPoint = newInPoint;
    preCompLayer.outPoint = newOutPoint;

    currentShot++;

    }

    Miguel De mendoza replied 9 years, 6 months ago 2 Members · 1 Reply
  • 1 Reply
  • Miguel De mendoza

    October 25, 2016 at 9:23 am

    If that master layer is at the bottom of layers you can get his index like this:

    var bottomLayerIndex = myComp.layer(myComp.layers.length).index;

    If that layer have a concrete name:

    var bottomLayerIndex = myComp.layers.byName(yourMasterLayerName).index;

    Then you can push it into the array:

    layerIndices.push(bottomLayerIndex);

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