Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Find how many layers with the same name there are in the comp?

  • Find how many layers with the same name there are in the comp?

    Posted by Lord Scales on February 1, 2008 at 8:54 pm

    Hi there. I need to find out how many layers with the same name there are in the Comp.

    For exemple, I have 1 layer called “Lord Scales” and I duplicate it several times. I’d like to know if there is a way to find out how many layers with the name “Lord Scales” are there in the comp.

    I have got this in the opacity:

    for (i=1; i<=thisComp.numLayers;i++) { a = 1; //number of layers with the same name L = thisComp.layer (i); if (L.name == name) a++; } value/(a-1) I need the value of "a", but if I duplicate my layer nothing happens to the opacity of the first one. The value of "a" doesn't change as I duplicate the layers. I changed the expression several times but it never works as I want. What is going wrong? Thank you.

    Alex Printz replied 7 years, 10 months ago 4 Members · 8 Replies
  • 8 Replies
  • Kevin Camp

    February 1, 2008 at 10:54 pm

    move ‘a’ outside the loop, so it would look like this:

    a = 1; //number of layers with the same name
    for (i=1; i<=thisComp.numLayers;i++) { L = thisComp.layer (i); if (L.name == name) a++; } value/(a-1) this will change the the value of 'a', howeveer it doesn't exactly count the number of layers with the same name... if you were after that, change it to: a = 0; //number of layers with the same name for (i=1; i<=thisComp.numLayers;i++) { L = thisComp.layer (i); if (L.name == name) a++; } a Kevin Camp Senior Designer KCPQ, KMYQ & KRCW

  • Kevin Camp

    February 1, 2008 at 10:55 pm

    rather neat expression, by the way.

    Kevin Camp
    Senior Designer
    KCPQ, KMYQ & KRCW

  • Lord Scales

    February 2, 2008 at 2:52 am

    Thanks, worked fine.

    But why the “a” has to be out of the loop?

    Lord Scales

  • Kevin Camp

    February 2, 2008 at 5:51 pm

    otherwise ‘a’ keeps getting reset at the beginning of each loop… you wanted to define ‘a’ prior to the loop so it would increase at each loop…

    i’m not sure that made sense…?

    if you set ‘a=1’ at the beginning of the loop, then increase ‘a’ at the end of the loop, ‘a’ will get reset back to ‘a=1’ when the loop starts again, negating your ‘a++’ at the end of the prior loop…. you essentially kept reseting ‘a’ back to the starting value so ‘a’ could never exceed 2… and that’s why your value for a never changed.

    by setting ‘a=1’ (or a=0) before the loop, ‘a++’ will continue to increase with each loop.

    Kevin Camp
    Senior Designer
    KCPQ, KMYQ & KRCW

  • Scott Mcgee

    June 20, 2018 at 5:57 pm

    Could you adapt this so it breaks. For example you have 10 layers. 5 of them layers have the same name.

    The result would come back with something like this. I really still can’t get my head around for through expressions

    Bar1 = 1
    Bar2 = 1
    Bar3 = 1
    Bar1 = 2
    Bar4 = 1
    Bar1 = 3
    Bar6 = 1
    Bar7 = 1
    Bar1 = 4
    Bar1 = 5

  • Alex Printz

    June 21, 2018 at 3:19 pm

    sure scott, all you would do is search until i < thisLayer.index instead of i <= thisComp.numLayers.

    Then it would stop at the current layer.

  • Scott Mcgee

    June 22, 2018 at 8:18 am

    Amazing.

    I can’t believe how I can understand so much of these expression yet For through expressions I struggle so much. Is there anywhere that has a better understand of how to use them?

    I’ve read most scripting guides and never find anything about them, yet I’m finding myself using them more and more.

    Cheers for that though, that does exactly what I want it to do.

  • Alex Printz

    June 22, 2018 at 1:55 pm

    I mean, expressions run on javascript, so I used a lot of javascript library reference and tutorials for learning.

    It really just comes down to knowing what your target goal is, what resources you have available, and then how to build that bridge. I would suggest you read a lot of Dan’s site, particularly the bottom part where he highlights and explains what his code does.

    I learned from mostly writing expressions myself out of project demands, and just a lot of searching and reading (and note taking!)

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