Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Randomly turn on and leave on multiple shape layers using opacity

  • Randomly turn on and leave on multiple shape layers using opacity

  • Kenny Ringgold

    April 28, 2020 at 2:07 am

    I’ve got a comp with a bunch of shape layers, and I want them to turn on randomly by going from 0 to 100 opacity and then once on, stay on. The top shape layer fill is keyframed to accomplish this, and I tried to have the subsequent (lower layers) turn on randomly using an offset expression (connected to a slider to control the amount). This has gotten me part of the way there, I just can’t figure out how to randomize the offset and keep the layer on once it’s on….

    offset = thiscomp.layer(“ADJUSTMENTS”).effect(“Opacity Offset”)(“Slider”)
    thisComp.layer(index-1).content(“Fill 1”).opacity.valueAtTime(time – offset)

    I also tried this:

    if (r<=99) {0} else {100}

    Which turns layers on randomly but again I can’t keep them on and I also can’t control the offset.

    Can anyone educate me on where I’m going wrong?
    Thanks in advance for any help.


  • Dan Ebberts

    April 28, 2020 at 6:43 am

    Try this for each shape’s opacity transform:

    maxDelay = 5; // turn all shapes on within 5 seconds
    myDelay = random(maxDelay);
    time – inPoint < myDelay ? 0 : 100


  • Andrei Popa

    April 28, 2020 at 6:54 am

    Add a slider to your first layer. This will save the times in which the layers appear. Add the following expression. You can modify at which key to start by changing the parameter in the key(2) : sTime = thisLayer.opacity.key(2).time;. numLayers is the number of child layers you have(exclude your main). maxDur is the maximum duration of the delay in seconds.

    function getRandomTimes(sTime, numLayers, maxDur){
    var crtTime = sTime;
    var values = [sTime];
    for(var i=1; i <=numLayers;i++){
    return values;

    sTime = thisLayer.opacity.key(2).time;
    numLayers = 7;
    maxDur = 1;
    var times = getRandomTimes(sTime, numLayers, maxDur);
    val = 0;
    while (time>=times[i]){

    Then, to the following layers add this to opacity. firstLayerIndex is the index of your main layer in the current composition.

    firstLayerIndex = 2;
    index-firstLayerIndex <= thisComp.layer("Shape Layer 1").effect("Slider Control")("Slider") ? 100 : 0;

    My Envato portfolio.

  • Andrei Popa

    April 28, 2020 at 6:56 am

    Didn’t see Dan answered.
    His solution makes them appear in random order. Mine keeps the order of the layers.

    My Envato portfolio.

  • Kenny Ringgold

    April 28, 2020 at 12:21 pm

    Thanks Dan,
    I actually used this and then added some code I located in one of your earlier posts that allowed me to set the duration of the fade and the offset using sliders in a Null adjustment layer. Thanks for both instances of help!

  • Kenny Ringgold

    April 28, 2020 at 12:24 pm

    Thanks for your response…your expression doesn’t do exactly what I needed, but it is a big help with generating random numbers for other related things I’m working on as well. I’m grateful for the knowledge!

  • Kenny Ringgold

    April 29, 2020 at 12:21 am

    One more quick inquiry if I may, Dan…
    What would be the easiest way to control length of time from the composition starts till the layers start randomly turning on?


  • Dan Ebberts

    April 29, 2020 at 12:26 am

    You could add a minDelay variable, like this:

    minDelay = 3; // shapes wait at least 3 seconds to turn on
    maxDelay = 5; // all shapes turn on within 5 seconds
    myDelay = random(minDelay,maxDelay);
    time – inPoint < myDelay ? 0 : 100

    then adjust the first two lines to suit.


  • Kenny Ringgold

    April 29, 2020 at 2:37 am

    Thanks Dan!

Viewing 1 - 9 of 9 posts

Log in to reply.

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