Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Check for existence of specific layer name from top to bottom of comp

  • Check for existence of specific layer name from top to bottom of comp

    Posted by Paul Roper on November 14, 2016 at 7:22 pm

    I’ve found an expression which almost does what I’m looking for here:

    https://forums.creativecow.net/thread/227/6932

    Here’s the expression, slightly modified:
    findName = "Fred";
    for (i =index; i <=thisComp.numLayers; ++i) {
    if (thisComp.layer(i).name.indexOf(findName) != -1) {
    // do what you need for this;
    }
    }

    It successfully finds a layer of name “Fred”, and by simply looking at the variable i, returns the layer number, which is exactly what I want. But if I have several layers named Fred 1, Fred 2, Fred 3, it always returns the number of the layer lowest in the comp stack, which seems really odd, seeing as the code seemingly starts at layer index (the layer with this expression is always ABOVE the Fred layer), increases by 1 on each iteration, then does the “// do what you need for this;” when it’s reached the appropriate layer. But seemingly the loop continues until it finds the highest number…or something.

    I’m looking to find the next layer BELOW the current one (the one with the expression), so if I have, in my comp, the layers:
    1 nothing
    2 layer with expression
    3 not important
    4 something else
    5 Fred 1
    6 another irrelevant layer
    7 Fred 2
    8 background or something
    …then I’d like the “layer with expression” to search downwards, and stop the search when it reaches the first Fred layer; in this case layer 5. But I have no idea why this expression is not doing this. Any suggestions, please?

    motion graphics | VFX | web design | etc.
    https://paulroper.com/

    Paul Roper replied 9 years, 9 months ago 2 Members · 4 Replies
  • 4 Replies
  • Paul Roper

    November 14, 2016 at 7:24 pm

    Well, that was a quick fix.

    findName = "Fred";
    for (i =index; i <=thisComp.numLayers; ++i) {
    if (thisComp.layer(i).name.indexOf(findName) != -1) {
    i;
    break;
    }
    }

    The addition of “break” after the “// do what you need for this;” line (which in this case is to just return i, the layer number), stops the expression continuing to evaluate and search through the layers.

    motion graphics | VFX | web design | etc.
    https://paulroper.com/

  • Paul Roper

    November 14, 2016 at 7:35 pm

    ….but then again, that probably won’t work because I just need to exit the “for…” loop and continue to other parts of the expression, not “break” out of the whole expression. Is there some kind of “endloop” command?

    motion graphics | VFX | web design | etc.
    https://paulroper.com/

  • Dan Ebberts

    November 14, 2016 at 8:26 pm

    break just jumps you out of whatever loop you’re in.

    Dan

  • Paul Roper

    November 14, 2016 at 9:02 pm

    D’oh! So it does!

    Thanks, Dan!

    motion graphics | VFX | web design | etc.
    https://paulroper.com/

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