Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects After Effects Randomly selecting and showing layers in a precomp question

  • After Effects Randomly selecting and showing layers in a precomp question

    Posted by Jessica Love on January 22, 2025 at 11:20 pm

    I was trying to research if it’s possible to randomly select layers in a precomp to appear and have them stay visible while more layers are appearing. I found this code in another thread here that’s almost what I want but then the layer switches to another one without the others staying visible.

    n = 100; // number of images
    minDur = 25;
    maxDur = 40;

    seedRandom(index,true);
    images = [];

    for(i = 0; i < n; i++){ images[i] = i; } for (i = 0; i < n; i++){ idx = i + Math.floor(random(images.length – i)); temp = images[i]; images[i] = images[idx]; images[idx] = temp; } dur = 0; f = -1; while (time >= dur*thisComp.frameDuration && f < images.length – 1){ f++; dur += Math.floor(random(minDur,maxDur+1)); } images[f]*thisComp.frameDuration

    Is it possible to edit this code to get the effect I want?

    Walter Soyka replied 1 year, 3 months ago 3 Members · 8 Replies
  • 8 Replies
  • Walter Soyka

    January 23, 2025 at 11:15 pm

    If I’m understanding right and you’d like a layer to randomly appear and stay visible, you could apply this expression to its Opacity property. I’ve tried to make the code as approachable as possible, but I’m happy to answer any questions:

    // set the earliest and latest possible appearance times, in seconds

    var minTime = 0;

    var maxTime = 5;

    // use the layer index to seed the random number generator,

    // and make the result timeless (so the random numbers that

    // are generated are consistent from frame to frame

    seedRandom(index, true);

    // randomly choose a time for this layer to appear,

    // from the minimum time to the maximum time specified

    // above. We'll use "gaussRandom()" instead of "random()"

    // for a more pleasing distribution of random values

    var appearTime = gaussRandom(minTime, maxTime);

    // this layer should start with 0% opacity (invisible)

    var visibility = 0;

    // if the current time is greater than the appearance time,

    // set visibility to 100%

    if (time > appearTime) {

    visibility = 100;

    }

    // return our "visibility" variable

    visibility

  • Nir Vanaskitz

    January 25, 2025 at 2:46 pm

    Hey Walter, thanks for sharing your expression! Using seedRandom with the layer index and gaussRandom is a clever way to achieve consistent and natural-looking randomness for layer visibility. Your breakdown makes it easy to follow. Great job!

    – Nir-Vana

  • Nir Vanaskitz

    January 25, 2025 at 2:46 pm

    Hi Jessica,

    Yes, you can modify the code to have previously selected layers remain visible while adding new ones randomly. Adjusting the logic to accumulate visible layers should achieve the effect you’re looking for.

    Best,
    Nir-Vana

  • Jessica Love

    January 25, 2025 at 2:54 pm

    So would I use this could in the Opacity section with my original code, or would these two sections of code be together and I have to edit them to make them work properly? I greatly appreciate your help.

  • Nir Vanaskitz

    January 25, 2025 at 4:12 pm

    Hi Jessica,

    You should combine both sections of code within the Opacity property. Make sure to integrate them properly so they work together seamlessly in your precomp.

    Hope that helps!

    Best,
    Nir-Vana

  • Walter Soyka

    January 28, 2025 at 4:41 pm

    Jessica, this will replace your previous code. Apply just the expression I wrote to the Opacity property of any layer(s) you want to have randomly appear.

  • Jessica Love

    January 28, 2025 at 6:36 pm

    So would this be on the precomp with all of the layers in it? My apologies for all the questions, I greatly appreciate your help so much!

  • Walter Soyka

    January 28, 2025 at 8:20 pm

    Not the precomp itself. Apply this to each layer within the precomp that you want to appear randomly. (As a shortcut, you can apply it to the first layer, then copy the opacity property, select all the other layers, and paste.)

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