Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions script to search for a layer name and return it’s index

  • script to search for a layer name and return it’s index

    Posted by Rob Packer on February 26, 2022 at 7:37 am

    I am trying to write a bit of code I can call to search within a composition for a layer name and return the layer’s index, so I can then perform some actions on it.

    The following shows that the variables have the expected values when running, but once it gets back to the code it was called from the layIndex variable shows as undefined.

    How do I get the layIndex value to be returned?

    Thanks

    function sLN(compToSearch, layNS, layIndex){

    // compToSearch: name of composition to search (a global variable) , layNS: the layer name I'm searching for (a global variable), layIndex: the index of the layer that was searched for

    var K=compToSearch.numLayers, layer;

    for (var k = 1; k<=K; k++) {

    if (compToSearch.layer(k).name === layNS) {layer = compToSearch.layer(k);

    break;

    };

    layIndex = k;

    }

    Rob Packer replied 4 years, 2 months ago 2 Members · 2 Replies
  • 2 Replies
  • Andrei Popa

    February 26, 2022 at 9:56 am

    I don’t think you need a function for that. This should work:

    compToSearch.layer(layNS).index
  • Rob Packer

    February 26, 2022 at 12:09 pm

    Thank you once more Andrei!

    Simple and elegant.

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