FORUMS: list search recent posts

# Turning layers on and off in a random order

FAQ   •   VIEW ALL
 Turning layers on and off in a random order on Jun 12, 2018 at 9:57:52 am

Hi there,

I'm a relative newcomer to after effects so be gentle please. The problem I have is this:

I have 6 layers in a comp. I want those layers to turn on and off so that one layer is always visible.
I have animated the first iteration of this using opacity and hold keyframes. Each layer comes on for a period of 15 keyframes and goes off and the next one comes on at the exact same time as the previous turns off. My problem is I now want the second iteration to be in a different order to the previous ie random, and the third to be different again, and the fourth and so on.

Any help would be greatly appreciated.

Cheers

 Re: Turning layers on and off in a random orderon Jun 12, 2018 at 12:46:43 pm

This is a perfect place to use expressions. The complexity depends upon whether it is enough to randomly pick one of the 6 layers OR if you want to shuffle the layer order so that each of the 6 layers are displayed exactly once per round, just in a random order.

For the former, I would create a slider control with an expression that changes the value once in 15 seconds and then have an expression on each layer's opacity property that checks is the (rounded) value matches 1, 2, 3... etc depending on layer (different value for all six layers). If it does, the layer opacity is set to 100, if it doesn't, it is 0.

For the latter, you would need a loop in the slider expression that picks random values AND also makes sure that they don't repeat in any set of six.

Kalleheikki Kannisto
Senior Graphic Designer

 Re: Turning layers on and off in a random orderon Jun 12, 2018 at 12:56:46 pm

Thank you very much for your help. I shall try this and let you know how I get on.

Cheers

 Re: Turning layers on and off in a random orderon Jun 12, 2018 at 1:54:30 pm

Hi
I'm very sorry Kalleheikki. I understand the theory of what you are suggesting, but I'm struggling to put it into practice. I've started learning After Effects but I'm a bit weak on expressions. I've messed about a bit with what you suggested, but I'm disappearing down the rabbit hole. I wonder if you could explain a bit more in layman's terms or show me some examples. Thank you for your help thus far.

Cheers

 Re: Turning layers on and off in a random orderon Jun 12, 2018 at 3:11:12 pmLast Edited By Kalleheikki Kannisto on Jun 12, 2018 at 3:17:06 pm

Ok, so here're the expressions for the simpler case, where the visible layer is randomly picked every 15 frames.

There is a slider control on a layer named "controls". Its value is controlled by this expression:

```batchSeed = Math.floor(timeToFrames()/15); // variable with value that increases every 15 frames seedRandom(batchSeed, true); // value used to set random number generator seed Math.floor(random(0,6)) // generate random integer between 0 and 5 for slider value ```

The first of the layers whose visibility you want to control has this expression for opacity:

```sliderValue = thisComp.layer("controls").effect("Slider Control")("Slider").value; //gets slider value if (sliderValue == 0) 100 else 0 // visible if slidervalue is 0, otherwise invisible ```

For the other layers, change the expression to check if the value is 1, 2, 3, 4 or 5.

This does not check for repeating values, so occasionally you get the same layer being visible twice or three times in a row -- or more, with rapidly lessening probability.

Kalleheikki Kannisto
Senior Graphic Designer

 Re: Turning layers on and off in a random orderon Jun 12, 2018 at 7:23:21 pm

Thank you very much for your help. That worked a treat. I'm going to go away now and throughly learn expressions

Thanks again

K