Skip to main content
Mugen777
Inspiring
February 12, 2021
Answered

How can I trigger ease or linear expression with an if-else statement? (and one more question)

  • February 12, 2021
  • 1 reply
  • 2566 views

Hello everyone. 

What I am trying to do is creating a little script to automate a process. I have an if-else statement like this

if(value=keyframe1.value) -> 0
else -> 5

What I am trying to do is instead of instantly goes from 0 to 5 or the opposite, I want it to change slowly. But how can I change in or out points according to the if-else results? Maybe I need to use something other than ease/ linear? 

And one more question. 

Is it possible to get the value of a keyframe's first position when it added? I know most likely this is not possible but wanted to ask anyway. 

This topic has been closed for replies.
Correct answer Martin_Ritter

Hey, this is my expression. 

if(thisLayer.effect("ADBE LIQUIFY")("Distortion Mesh").key(1).value==thisLayer.effect("ADBE LIQUIFY")("Distortion Mesh").value) 
{ 
0; 
}else{ 
5; 
}

 

Currently, it switches from 0 to 5 (and opposite) instantly. What I want to do is make it switch slowly. 0 to 5 in half second for example. 


For a smooth transistion, you need linear() or ease() function.

 

The key to both is a useable time-value and there are several ways to get one - however, we need more details for a better answer.

 

You can read-out a given time-point (a marker, a keyframe, in- or out-point of a layer) and use this as trigger.

For example:

tstart = key(1).time

tend = tstart + 0.5 // half a second

linear(time, tstart, tend, value1, value2)

 

Trigger is the time position of the first keyframe.

 

You can read out the time-point dynamically. There are several ways from iterating through all keyframes and find the fitting one (fitting is defined by your own conditions), or use the built-in function nearestKey() (which will switch to the next key automatically, when this key is closer to the CTI than the previous one - this behaviour can be unwanted, for example, if keyframes are close together an the transistion isn't finished yet).

 

You can also make this completly autopilot. You can iterate through all frames (starting from 0 and going forward, as well as starting from current time and going backward/forward) and check a value of your choice at this time-point againt the trigger-value.

 

for (i = 0, i <= time, i+oneFrameInSeconds){

if (someLayer.opacity == 45){

tstart = i

tend = tstart + 0.5

linear(...)

break

 

However, such an expression is a heavy loader (I use to call this the Doom-Loop), because it iterates all frames for every frame. The longer your comp is (the later your trigger value appears), the more iterations necassary and the slower everything will be.

 

*Martin

1 reply

Mylenium
Brainiac
February 12, 2021

It's all possible, but much more complicated. You have to use valueAtTime() and retrieve the values in a loop and then use the ease() function as a filter on the output.

 

Mylenium

Mugen777
Mugen777Author
Inspiring
February 12, 2021

Looks like I forgot to add if( at the start of the code. It doesn't let me edit it for some reason so I am writing another reply instead. 

Mugen777
Mugen777Author
Inspiring
February 17, 2021

It's difficult to see exactly what you want to achieve — can you post the entire Expression you are currently using and what it is that you want to achieve?


Hey, this is my expression. 

if(thisLayer.effect("ADBE LIQUIFY")("Distortion Mesh").key(1).value==thisLayer.effect("ADBE LIQUIFY")("Distortion Mesh").value) 
{ 
0; 
}else{ 
5; 
}

 

Currently, it switches from 0 to 5 (and opposite) instantly. What I want to do is make it switch slowly. 0 to 5 in half second for example.