Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Trigger Expression at Certain Value and Loop/Cycle

  • Trigger Expression at Certain Value and Loop/Cycle

    Posted by Matthew Leonard on July 31, 2014 at 6:14 am

    I’ve been practicing expressions, and tried making this code work. I’ve nearly got everything working except for one thing, the flicker won’t trigger without keyframes. If I tell it what the keyframes are(0 at 0 seconds, 100 at 5, 0 at 10) it works perfectly. It fades from 0 to 100, and it flickers at 100 for a short while until it fades back to 0. I’m guessing this has something to do with expressions not being able to recall past frames(so I’ve heard)? How would I do this instead of copy and pasting 3 keyframes multiple times? I have a script that allows me to copy/paste keyframes dozens of times, but that would defeat the purpose of practicing expressions :-).

    // Variables

    opacity1 = opacity
    loopOut()
    minVal = 80
    maxVal = 100
    seed = 100 + time+2

    // Statements

    if (opacity1 >95) {
    seedRandom(seed,true);
    random(minVal,maxVal);
    }
    if (opacity < 90) {
    (loopOut()) // Since I had keyframes, I included loopOut.
    }

    Matthew Leonard replied 12 years, 1 month ago 2 Members · 10 Replies
  • 10 Replies
  • Matthew Leonard

    July 31, 2014 at 7:12 am

    Forgot to add one more thing. I’ve been looking over the internet trying to figure out how probability functions, but I cannot figure out how it works or how to make it affect a certain property. If I wanted the probability of it flickering at 100% opacity happening 1/100 times, how would that work? If anyone can, would you mind explaining it? Thank you.

  • Dan Ebberts

    July 31, 2014 at 4:33 pm

    In general, to have a probability of 1/100 you could do this:

    if (random(100) < 1){
    // do stuff
    }

    or,

    if (random() < .01){
    // do stuff
    }

    For your other question, I’m thinking you may have gone down the wrong path. What’s the effect you’re trying to create?

    Dan

  • Matthew Leonard

    July 31, 2014 at 6:32 pm

    Many thanks for clarifying that. The main effect really is to have a value(opacity in this case) to go from 0 to 100, then to 0 again(gradually, as if I keyframed 3 keyframes over a span of 10 seconds using ease keyframes). When it hits 100, it would flicker between my two set values, and this will happen ONLY when the value is at 100(or between any other values). It will always hit 100 at the same place(starts at 5 seconds, so every 10 seconds since the next 100% keyframe would be on the 15th second), so I’m hoping it’s not too complicated to create just using expressions. It would go from 0 to 100 over a span of 5 seconds, then back to 0 at 10 seconds. The flicker duration would be about 1 or 2 seconds, then it would ease out continuing to 0.

    I want to do the above, but with expressions that will loop it. Kind of in a pingpong type manner. I tried doing it with my code and the flickers only triggered at the 100% keyframe, but it only triggered when there was a keyframe there. When those 3 keyframes looped for a second time(using loopOut) and it hit 100% without a second set of those keyframes, it didn’t flicker according to my values set.

  • Dan Ebberts

    July 31, 2014 at 6:49 pm

    I’d start with something like this:


    minVal = 80;
    maxVal = 100;
    minFlickerDur = 1;
    maxFlickerDur = 2;

    t = (time - inPoint)%10;
    n = Math.floor((time - inPoint)/10)
    if (t < 5){
    ease(t,0,5,0,100);
    }else{
    seedRandom(n,true);
    flickerDur = random(minFlickerDur,maxFlickerDur);
    if (t < 5+flickerDur){
    seedRandom(index,false);
    random(minVal,maxVal);
    }else{
    ease(t,5,10,100,0);
    }
    }

    Dan

  • Matthew Leonard

    July 31, 2014 at 8:12 pm

    Thanks Dan, that works fantastically. I understand most of it, but would you mind explaining these for me if you have the time?

    “n = Math.floor((time – inPoint)/10)” What exactly is this code doing? I understand that Math.floor kind of rounds in a sense, but what would it be “rounding”?

    What part/s would I change if I wanted to change the amount of time(it’s at 10 seconds now, so maybe 5,15, or 30 seconds).

    How would I include probability in that code? I’m not sure if you can encase an if statement around a bunch of if/else statements.

    Creator of many visual effects since 2010.

  • Dan Ebberts

    July 31, 2014 at 9:47 pm

    The expression breaks time-since-the-layer’s-in-point into 10-second segments. The calculation for n determines which segment this is (the first one is 0, the second is 1, etc.). The calculation for t determines how far we are (in seconds) into the current segment. This is what drives the DIY looping.

    If you wanted to change the cycle time, you’d change all the 10’s to something else (I think it occurs in 3 places), or better yet, make it a variable (I probably should have done that).

    How would you want the probability to work?

    Dan

  • Matthew Leonard

    July 31, 2014 at 10:04 pm

    Alright, got it. The probability would be something simple like: there’s a 1/100 chance of it flickering, else it doesn’t flicker at all. So each time it hits 100, the probability would run.

    Creator of many visual effects since 2010.

  • Dan Ebberts

    July 31, 2014 at 10:53 pm

    Here’s a revised version that should only flicker half the time (probability = .5):


    minVal = 80;
    maxVal = 100;
    minFlickerDur = 1;
    maxFlickerDur = 2;
    cycleTime = 10;
    probability = .5;

    t = (time - inPoint)%cycleTime;
    n = Math.floor((time - inPoint)/cycleTime)
    if (t < cycleTime/2){
    ease(t,0,cycleTime/2,0,100);
    }else{
    seedRandom(n,true);
    flickerDur = random(minFlickerDur,maxFlickerDur);
    if (t < 5+flickerDur && probability < random()){
    seedRandom(index,false);
    random(minVal,maxVal);
    }else{
    ease(t,cycleTime/2,cycleTime,100,0);
    }
    }

    Dan

  • Dan Ebberts

    July 31, 2014 at 10:57 pm

    Oops–that one doesn’t completely use the new cycleTime variable. This should work better:


    minVal = 80;
    maxVal = 100;
    minFlickerDur = 1;
    maxFlickerDur = 2;
    cycleTime = 9;
    probability = .5;

    t = (time - inPoint)%cycleTime;
    n = Math.floor((time - inPoint)/cycleTime)
    if (t < cycleTime/2){
    ease(t,0,cycleTime/2,0,100);
    }else{
    seedRandom(n,true);
    flickerDur = random(minFlickerDur,maxFlickerDur);
    if (t < cycleTime/2+flickerDur && probability < random()){
    seedRandom(index,false);
    random(minVal,maxVal);
    }else{
    ease(t,cycleTime/2,cycleTime,100,0);
    }
    }

    Dan

  • Matthew Leonard

    July 31, 2014 at 11:20 pm

    Dan, you truly are the man. Thanks a ton.

    Creator of many visual effects since 2010.

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