ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

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

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Lord Scales
Find how many layers with the same name there are in the comp?
on Feb 1, 2008 at 8:54:08 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.


Return to posts index

Kevin Camp
Re: Find how many layers with the same name there are in the comp?
on Feb 1, 2008 at 10:54:25 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


Return to posts index

Kevin Camp
Re: Find how many layers with the same name there are in the comp?
on Feb 1, 2008 at 10:55:37 pm

rather neat expression, by the way.

Kevin Camp
Senior Designer
KCPQ, KMYQ & KRCW


Return to posts index


Lord Scales
Re: Find how many layers with the same name there are in the comp?
on Feb 2, 2008 at 2:52:35 am

Thanks, worked fine.

But why the "a" has to be out of the loop?

Lord Scales


Return to posts index

Kevin Camp
Re: Find how many layers with the same name there are in the comp?
on Feb 2, 2008 at 5:51:40 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


Return to posts index

Scott McGee
Re: Find how many layers with the same name there are in the comp?
on Jun 20, 2018 at 5:57:30 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


Return to posts index


Alex Printz
Re: Find how many layers with the same name there are in the comp?
on Jun 21, 2018 at 3:19:13 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.


Return to posts index

Scott McGee
Re: Find how many layers with the same name there are in the comp?
on Jun 22, 2018 at 8:18:43 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.


Return to posts index

Alex Printz
Re: Find how many layers with the same name there are in the comp?
on Jun 22, 2018 at 1:55:12 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!)


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]