ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

How would I get the nth number from a number sequence?

FAQ   •   VIEW ALL
 How would I get the nth number from a number sequence? on Apr 24, 2020 at 8:30:30 am

Hi smarter people than I.....

I'm sure I am over thinking this and that there is a simple solution to my problem.

So, I have, for example, 100 layers, each layer with an index from 1 to 100. What I would like to do is execute an expression on each layer depending on if the layer is the nth (or in my case every 3rd) layer in the sequence. i.e the expression executes if the layer is the 3rd, 6th, 9th, 12th etc...

I'd wrap this in an if() statement so it would return as true if the layer had an index of 3, 6, 9, 12 and so on.

The way I am doing it currently is below:
``` if(idObjectIndex == 3 || idObjectIndex == 6 || idObjectIndex == 9 || idObjectIndex == 12 || idObjectIndex == 15 || idObjectIndex == 21) { idControlColours[0];```

Obviously doing it this way is very long winded, inefficient and not dynamic if I were to add more layers beyond the what I have already setup.

Getting either 1st or 2nd layer in the sequence I can do by evaluating if it is either odd or even:

idObjectIndex % 2

Which is straight forward enough but where I come unstuck is getting the 3rd, 4th or even the 5th layer in the sequence.

I'm sure there is a simple way to do this and my scouring of the inter-webs has just made things more confusing... or maybe I am asking the wrong questions.

Any pointers would be great!

Standing by to slap my forehead and say "doh, of course, how obvious !"

Many thanks!

P.

 Re: How would I get the nth number from a number sequence?on Apr 24, 2020 at 2:13:14 pm

if (index%3 == 0)

should be true for layers 3,6,9,etc.

Dan

 Re: How would I get the nth number from a number sequence?on Apr 25, 2020 at 5:47:14 am

Hi Dan,

Thanks for the quick response and help as always.

I tried what you suggested but run into problems if I then wanted to evaluate if the layer was either the 1st, 2nd or 3rd in the sequence within the same expression - so, index%3 and index%2 would both return true for layer six as an example, thus not giving me the result I was looking for. However it got me thinking and after some tinkering I realised I need to set the index%3 as a variable and call it in the if() statement and not wrap it within it.

This is what eventually worked for me - this also means I can quickly add conditions, like if the layer is the fifth layer in the sequence.

```idObjectIndex = effect("fauxIndex")("Slider"); idThirdLayer = idObjectIndex % 3; if(idObjectIndex <= 0 && idThirdLayer == 0) { 000; } else if(idThirdLayer == 1) { 111; } else if(idThirdLayer == 2) { 222; } else if(idThirdLayer == 0) { 333; }```

Thanks again!

P.