Page 1 of 1

Need help with an Expression please.

PostPosted: Fri May 02, 2025 11:32 pm
by _noirs_
I want to insert an Expression to make this go from 0 to 1 within 1 second, and once it reaches 1 go back from 1 to 0 within another second. I would greatly appreciate your help! Thanks in advance.
Image here:

https://imgur.com/ClC3D7c

Re: Need help with an Expression please.

PostPosted: Sat May 03, 2025 12:59 pm
by Christoph Schmid
Sinus Curve (half period)
Code: Select all
: start = 0;
fps = comp:GetPrefs("Comp.FrameFormat.Rate");
if time > start and time <= (start+fps*2) then return sin((time-start)/fps*(pi/2));
else return 0;end


Linear
Code: Select all
: start = 0;
fps = comp:GetPrefs("Comp.FrameFormat.Rate");
if time > start and time <= (start+fps) then return (time-start)/fps;
elseif time > (start + fps) and time <= (start+fps*2) then return 2-(time-start)/fps;
else return 0;end


Cosinus (smooth | complete period)
Code: Select all
: start = 0;
fps = comp:GetPrefs("Comp.FrameFormat.Rate");
if time > start and time <= (start+fps*2) then return cos((time-start)/fps*pi+pi)/2+0.5;
else return 0;end


Change the start value to the frame you want to start the animation.
Do not use “Select all” to copy the code as this will add an indent.

Re: Need help with an Expression please.

PostPosted: Sat May 03, 2025 2:26 pm
by Sander de Regt
If you don't use an expression, but looped keyframes for this, there's a chance that caching will work a little better.

Re: Need help with an Expression please.

PostPosted: Sat May 03, 2025 3:16 pm
by KrunoSmithy
+1 for loop keyframes.

Re: Need help with an Expression please.

PostPosted: Sat May 03, 2025 4:40 pm
by Christoph Schmid
The OP didn't write that he wanted to do a loop.
But if I think about it, it doesn't really make much sense to use an expression for a simple up and down...
And if you're saying it's faster to use loop keyframes, the OP should do that.
If for some reason the expression is still needed:

Code: Select all
:start = 0;
fps = comp:GetPrefs("Comp.FrameFormat.Rate");
duration = fps * 2;
loopTime = (time - start) % duration;
if time >= start then return (cos(loopTime / duration * 2 * pi - pi) + 1) / 2;
else return 0; end

Re: Need help with an Expression please.

PostPosted: Mon May 05, 2025 8:28 pm
by _noirs_
Christoph, thanks so much for your elaborate reply, super appreciate it!