ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Opacity fade in/out based on speed

FAQ   •   VIEW ALL
 Opacity fade in/out based on speed on Jul 16, 2020 at 11:46:01 amLast Edited By Dominique Parisi on Jul 16, 2020 at 11:46:52 am

Hello !

I'm trying to figure out to put a fade in and a fade out on the opacity of a layer depending of the speed of differents layers.

So, there is a layer A moving, when it's speed hit 0, the opacity of the other layer goes to 100 until it's own speed goes to 0, then it's fade to 0 again.

I managed to have the 0 to 100 and back to 0 (see the expressioon below the post)

But the fade in fade out do not work. I naively thought that adding somthing la "ease (time, 0,100,0,100)" would get me a fade in. Obviously not.

I tried a variation with this, but no luck either.

alphaVelo = comp("Game_Spin 2").layer("Ref_Reel_Alpha").transform.yPosition.speed;
suspensVelo = comp("Game_Spin 2").layer("Ref_Reel_2").transform.yPosition.speed;

if ((alphaVelo == 0) && (suspensVelo != 0));
if (suspensVelo != 0);
{(ease(time,0,100,0,100))};
if (suspensVelo == 0)
{(ease(time,100,0,100,0))};
else {0};

I'm still trying to plug my mind into that coding univers and I m slowly breaking into it, but still need help ☺

Kind Regards to all of you :)

```// kind of working expression. alphaVelo = comp("Game").layer("A").transform.yPosition.speed; suspensVelo = comp("Game").layer("B").transform.yPosition.speed; if ((alphaVelo == 0) && (suspensVelo != 0)) {100} else (0) ```

 Re: Opacity fade in/out based on speedon Jul 16, 2020 at 9:17:26 pm

Yeah this is a bit of a tricky one.

Expressions only know about the "now", so if you want an expression to tell opacity it should be fading because half a second ago something happened… it doesn't work like that.

Sometimes that means you have to loop back in time and look at every speedAtTime() or valueAtTime() that has occured in every frame since the start of the timeline, which is very inefficient, and gets slower and slower to calculate as the composition gets longer.

Sometimes You can get away with only looking back as much as your fadeTime needs to be:
- if the "trigger" happend in the last 30 frames, we might be in a 1s fade, if it happened longer than 30 frames ago: I don't care, I'm at 0.

In your case, you also want fadeins, so that makes the code a bit harder still, because it would need to look at two different conditions and flip the values in the ease() around.

There is a kind of workaround though: think about the "hard condition" that would cause the opacity to be on or off, and then "soften" that by averaging out what the condition was in every frame within a window: if A was at velocity 0 and B was not for 15 frames of the last 30: then we'll be at 50%, the effect is that you get an automatic smooth fade.

For keyframes, that is what the smooth() method does, so we mimic that behaviour with a for-loop because we can't just smooth() an expression.

```a=thisComp.layer("A").transform.yPosition; b=thisComp.layer("B").transform.yPosition; // "hard" condition is a==0 and b!=0 fd = thisComp.frameDuration; sampleWindow = 0.8; // fadetime in seconds; samples = sampleWindow / fd; // smooth out the condition by comparing samples in a timewindow smoother=0; for (s=0; s<samples; s++) { t=time - s*fd; if (a.speedAtTime(t)==0 && b.speedAtTime(t)!=0) (smoother+=100) } smoother/=samples; // average```

 Re: Opacity fade in/out based on speedon Jul 23, 2020 at 12:24:47 pm

Hello Filip !

Sorry for the delayed come back, but I was on vacation.

Thanks for the explication and the expression!
I'll try it as soon as possible and be back with a feedback!

Thank you again :)