ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Random automations

FAQ   •   VIEW ALL
 Random automations on Jul 22, 2020 at 9:21:31 pm

So I have about 150 PNG files loaded up in a comp containing varied sized dots, lines, scratches. The goal is to make a 5 minute long overlay effect, where all these dots, lines and scratches appear in random positions across the screen at random times lasting 1 frame each, and not all together but a maximum of 3 at a time (per frame). Its basically meant to replicate an old film look with random dust and scratches.

I tried doing it manually but it took over an hour for me to make about 10 seconds (can show example if needed), so I'm asking to see if this could be solvable with any expressions? seems a bit complex, at least for my skill level. If anyone could chime in on it would be awesome. Thanks!

 Re: Random automationson Jul 23, 2020 at 7:49:02 am

Hi Jayden,

randomizing properties like position and opacity is pretty easy with expressions.
In your case however, the problem is that every layer would have to know if there are already 3/4 layers visible, but expressions on different layers and properties don't "communicate" with each other, there are no shared or global variables.

Expressions can however look up the value of other properties and use those in their calculations.

For example a (Null-) Layer could have 4 sliders with random values every frame and each layer could look at those values to share the same randomness.

A favourite alternative technique of mine is to calculate "global things" with an expression inside of 1 text-layer, and then have other expressions read that textLayer's sourceText value.

In this scenario if the textLayer would spit out an array of 3-4 random integers every frame,
each "noise"-layer can check if their layer-index is in that list and if it is, set opacity to 100.

textLayer expression:
``` num_layers=150; startingIndex=2; // create an array of 4 elements arr = startingIndex * [1,1,1,1]; // for every element in the array, add a random value and round down the result arr=arr.map( (e) => Math.floor(e + random(num_layers)) ); // 66 % probability for 3 instead of 4 elements: if (random(100) <= 66) arr.pop(); JSON.stringify(arr); // "exports" the array as a readable string ```
Caveat, the random numbers are not guaranteed to be 4 different numbers, in those (rather rare) case, you would get less layers light up that frame.

Opacity expression for every dust-speck layer:
``` // read the array from the text-layer arr = eval(thisComp.layer("Array").text.sourceText); // "Array" = name of the textLayer arr.indexOf(thisLayer.index)>-1 ? 100 : 0; ```

Position is more straightforward:
`random([0,0],[thisComp.width,thisComp.height]); `

or even set it manually, possibly with some negative margins so elements have more chance of being partially out of frame:
`random ( [-100,-100] , [1920+100, 1080+100] );`