• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit Search
0

"while" in expression causing crash - alternate suggestions?

Contributor ,
Dec 19, 2022 Dec 19, 2022

Copy link to clipboard

Copied

Hi, I'm trying to have my text type on, but I want it to seem more natural with the appearance of each next character taking a different amount of time. I want this same effect across many different versions and pieces of text (of varying length) in a large project, so I want to do this using an expression rather than keyframes.

I wrote the following expression on the start range selector of an opacity text animator (set to character index rather than %), but as soon as I leave the expression editor, After Effects crashes.

PhiDef_0-1671486038054.png


I've got a slider with a wiggling speed value, and the idea was that an additional character would appear on screen if the speed value at any time would require it. I think the problem is the while command. Unfortunately, I can't just multiply the speed value by time, because then as the wiggling speed value decreases, some characters that have already appeared disappear.

Any suggestions? Thanks!

TOPICS
Crash , Error or problem , Expressions , How to

Views

189

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 19, 2022 Dec 19, 2022

Try this:

minSpeed = 2;
maxSpeed = 7;
seedRandom(index,true);

t = 0;
count = 0;
while (t < time){
  cps = random(minSpeed,maxSpeed);
  sec = 1/cps;
  t += sec;
  count++;
}
count

Votes

Translate

Translate
Contributor ,
Dec 19, 2022 Dec 19, 2022

Copy link to clipboard

Copied

I thought maybe this would work (getting the value from the previous frame and adding one), but it doesn't. The resulting character index never gets above 1.

PhiDef_0-1671488019383.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2022 Dec 19, 2022

Copy link to clipboard

Copied

Try this:

minSpeed = 2;
maxSpeed = 7;
seedRandom(index,true);

t = 0;
count = 0;
while (t < time){
  cps = random(minSpeed,maxSpeed);
  sec = 1/cps;
  t += sec;
  count++;
}
count

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 20, 2022 Dec 20, 2022

Copy link to clipboard

Copied

Hey Dan, Thanks for that! It's great.

I've just been analyzing it to figure out how it works, and it appears that the expression must remember the count value from the previous frame, is that right? So, it doesn't re-execute the count = 0 with each new frame, right? I'm surprised by this. Is there a simple way of understanding which parts of the code are run fresh at each new frame, and which aren't?

Thanks again!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 20, 2022 Dec 20, 2022

Copy link to clipboard

Copied

Expressions have no memory (no variables survive from frame to frame) so it has to run the whole thing at each frame.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 20, 2022 Dec 20, 2022

Copy link to clipboard

Copied

I see. So that means the sequence of random numbers generated by the while loop must be the same every frame, is that right?

I was assuming that the random numbers would be different each time, which would mean that on frames when the while loop happened to generate a series of high random numbers (after previously having generated a series of low random numbers), characters already on screen in the previous frame would disappear. I guess I just don't know how random number generation works.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 20, 2022 Dec 20, 2022

Copy link to clipboard

Copied

>So that means the sequence of random numbers generated by the while loop must be the same every frame, is that right?

Correct. That's what seedRandom(index,true) does (the 'true' is for the timeless parameter, which means the same random sequence will be generated each frame).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

Hey again,

I'm needing this same kind of expression again, this time to generate a feed scrolling up a phone screen with a wiggling speed (ie. smoother than new random speeds on each frame).

Since, as you noted, specifying the random seed at the beginning of the expression means the same random results are generated on every frame, I assumed the following code would generate the correct results with the scrolling always moving upwards and never downwards. Unfortunately, that doesn't seem to be the case; it seems as though the wiggle returns different results on every frame. Any idea why?

 

 

 

seedRandom(effect("Random Seed")("Slider"),true);

t = 0;
count = effect("Scroll Start yPos")("Slider");
while (t < time){
  pps = effect("Scrolling Animation Speed")("Slider").wiggle(3,effect("Scrolling Animation Speed")("Slider")/2);
  sec = 1/pps;
  t += sec;
  count++;
}
count

 

 

 

 

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

Try changing this line:

pps = effect("Scrolling Animation Speed")("Slider").wiggle(3,effect("Scrolling Animation Speed")("Slider"));

to this:

pps = effect("Scrolling Animation Speed")("Slider").wiggle(3,effect("Scrolling Animation Speed")("Slider"),1,0.5,t);

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

Awesome! Thanks again!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 28, 2022 Dec 28, 2022

Copy link to clipboard

Copied

LATEST

I think it's still possible for it to go backwards with that set up because wiggle() will sometimes go plus or minus more than 100% of the amplitude. So you might want to multiply the amplitude parameter by .9 or so, just to make sure it doesn't go negative.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines