Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Move created layer below selected layers

  • Move created layer below selected layers

    Posted by Bryan Woods on May 6, 2013 at 6:18 pm

    I have a script that does some stuff to a selection of layers, then parents them to a newly created null object. I want to position this null object below the selected layers, but I don’t know how to set that up. I’m familiar with the moveTo commands, but not sure how to move something that isn’t part of the original selection. Basically the script creates the null, adds the expression sliders It needs to have, then moves it to below all the selected layers (Note: this is different from moving the null to the very bottom of the composition).

    Bryan Woods replied 13 years, 2 months ago 2 Members · 6 Replies
  • 6 Replies
  • Dan Ebberts

    May 6, 2013 at 7:39 pm

    Before you create the null, you could save the selected layer with the highest index (not tested, but should be close):

    var maxIndex = -1;
    for (var i = 0; i < myComp.selectedLayers.length; i++)){
    maxIndex = Math.max(maxIndex,selectedLayers[i].index);
    }
    var lastSelectedLayer = myComp.selectedLayers[maxIndex];

    Then after you create the null, you could do this:

    myNull.moveAfter(lastSelectedLayer);

    Dan

  • Bryan Woods

    May 7, 2013 at 5:47 pm

    No go after testing. I also noticed it looks like there was an extra “)” after myComp.selectedLayers.length; i++. I took that out since it was first getting hung up over that. After removing that, I get a new error: “Unable to execute at line 29. Unable to call “moveAfter” because of parameter 1. undefined is not of correct type”.

    Here’s the portion of my script that is handling this for your reference.

    //Define project and active items
    var proj = app.project;
    var theComp = proj.activeItem;
    var selectedLayers = theComp.selectedLayers;

    //Count number of selected layers
    var maxIndex = -1;
    for (var i = 0; i < selectedLayers.length; i++){
    maxIndex = Math.max(maxIndex,selectedLayers[i].index);
    }
    var lastSelectedLayer = selectedLayers[maxIndex];

    //Create Null Object to act as controller for items
    controlNull = theComp.layers.addNull();
    controlNull.source.name = "Logo_Controller";
    controlNull.moveAfter(lastSelectedLayer);

    Thanks for your help.

    EDIT:: Line 29 is referring to the line that starts with “ControlNull.moveAfter”

  • Dan Ebberts

    May 7, 2013 at 6:53 pm

    I warned you that it wasn’t tested . 😉

    Try changing this line:

    var lastSelectedLayer = selectedLayers[maxIndex];

    to this:

    var lastSelectedLayer = theComp.layer(maxIndex);

    Dan

  • Bryan Woods

    May 7, 2013 at 7:08 pm

    Ah, I should have noticed that your “myComp” was essentially the same thing as my “theComp” variable. They were calling the same thing, just broken out more.

    What is the difference though between .layer[maxIndex] and .layer(maxIndex)? I don’t follow why the change in brackets to parenthesis.

    Thanks again for your help on this Dan. I always greatly appreciate your support.

  • Dan Ebberts

    May 7, 2013 at 7:14 pm

    In the first go at it, I was using maxIndex as an array index into the selectedLayers array, but it’s really a layer index, so instead of

    selectedLayers[maxIndex]; // indexing into array of selected layers

    it should have been

    theComp.layer(maxIndex); // indexing layer within comp

    I think (not sure though) that you can actually use array indexing to access layers, but I never do it that way.

    Dan

  • Bryan Woods

    May 7, 2013 at 8:21 pm

    Ah, makes sense. Thank you for the explanation!

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