Skip to main content
New Participant
February 5, 2025
Answered

after effects randomise timing with expressions

  • February 5, 2025
  • 2 replies
  • 1074 views

hi, I am pretty new to using after effects expressions and I am not quite sure how to solve my problem and i also found no working solution on the internet.

 

so I want to animate objects with always one in the background and one in the foreground and want to alternate them - this i figured out works with giving one layer:

 

rate = 1/1;

posterizeTime(rate);

seedRandom(index, false);

random(0,100) < 50 ? 0 : 100;

 

and the other one:

 

if(thisComp.layer(index+1).transform.opacity==0)100;
else 0;
 
now comes my breaking point: I have a lot of these alterning pairs and I dont want them to all switc at the same time but rather randomized. I´ve tried several options working with offset, startTime/endTime or durations and either I did it wrong or it was the absolute wrong approach to my problem.
(e.g. pair 1 always switches on frames with :10 and pair 2 on frames with :15)

I am also working on a file with around 400 layers which also moves me to find a simple solution to my problem. 

 

(I also wanted it to only randomize between second 1 and 6 but I am more frustrated about finding a solution to the timing problem)

 

thank you soo so much in advance!

Correct answer emma_7029

Hi @Hani ElTiby ,

thank you very much for your help and quick response!

 

Unfortunatly I think I didn't quite understand your code so I wasn't able to make it work.
But I found another solution that seems to work with:

seedRandom(index, true); 

minFrames = 100; 

maxFrames = 200; 

frameHold = Math.round(random(minFrames, maxFrames));

currentFrame = timeToFrames(time); // Aktuelles Frame der Komposition

 

switchTime = Math.floor(currentFrame / frameHold);

 

(switchTime % 2 == 0) ? 100 : 0;

and:

if (thisComp.layer(index+1).transform.opacity == 0) 100;

else 0;

 

Thank you all very much for your help! 

2 replies

Inspiring
February 6, 2025

Hi Emma, Try this

Notice that the controller comes right after the randomized layers so it dynamicly count the layers above, by using it's own index in the random() function.
If you have layers above the randomized layers you may set the first random parameter manually or by pickwipping the first layer and get it's index
for example : rand = random(thisComp.layer("Medium Royal Blue Solid 2").index, index);
here are both expressions
the Controller layer first

posterizeTime(effect("Posterize")("Slider"));
rand = random(thisComp.layer("Medium Royal Blue Solid 2").index,index);
hide = Math.floor(rand);

then the opacity

hide = thisComp.layer("Controller").effect("Hide")("Slider");
if (hide == index) {value} else {0}
emma_7029AuthorCorrect answer
New Participant
February 6, 2025

Hi @Hani ElTiby ,

thank you very much for your help and quick response!

 

Unfortunatly I think I didn't quite understand your code so I wasn't able to make it work.
But I found another solution that seems to work with:

seedRandom(index, true); 

minFrames = 100; 

maxFrames = 200; 

frameHold = Math.round(random(minFrames, maxFrames));

currentFrame = timeToFrames(time); // Aktuelles Frame der Komposition

 

switchTime = Math.floor(currentFrame / frameHold);

 

(switchTime % 2 == 0) ? 100 : 0;

and:

if (thisComp.layer(index+1).transform.opacity == 0) 100;

else 0;

 

Thank you all very much for your help! 

Kevin-Monahan
Community Manager
Community Manager
February 6, 2025

Hi @emma_7029,

Thanks for the question. Sorry you were not able to get n answer from an expert or team member. I did a little searching and found that if you add a couple more lines of code for the first data set, you may be able to randomize objects more easily. Try:

rate = 1/1;
posterizeTime(rate);
seedRandom(index, false);
switchTime = random(1, 6); // Random time between 1 and 6 seconds
timeToSwitch = Math.floor(time % switchTime) == 0;
timeToSwitch ? 0 : 100;

I hope that helps. Let me know if it does.

Take Care,
Kevin
 

 

Kevin Monahan - Sr. Community &amp; Engagement Strategist – Pro Video and Audio
emma_7029Author
New Participant
February 6, 2025

Hi @Kevin-Monahan ,

thank you so much for your quick reply and help!

 

Unfortunately this did not seem to be the solution. All the layers are still switching at 1s/2s/3s/... 
I was just wondering whether it could be due to the rate that is set. Is there a way to randomize that?

 

Thank you so much and take care,

Emma