Expression Library

Some handy expressions to use in after effects to speed up your workflow.

Blink Expression:

Create 3 opacity keyframe values from 100 to 0 to 100.

Turn keyframes into “Hold Key Frame”.

Use loop cycle expression.

loopOut(type="cycle");
 

Round Decimal counts :

To change decimal place in expression generated value there are two methods. To roundup the figures you can use math.round expression.

The below expression will change from 2.234564 to 2.

Math.round(numbers with DecimaleValue);
 

Convert negative value to positive value :

Use Math.abs to convert negative value to positive value or vice versa.

Math.abs(-100) //Any nagative value inside parenthesis will return positive number. 100 in this case.
Math.abs(-100)*-1 //Any positive value inside parenthesis will return negative number. -100 in this case.

 

Limit the decimal point:

You can use toFixed expression to limit the output of decimal points. Below expression will change the value of 2.234564 to 2.23;

var yourValue =2.234564;
yourValue.toFixed(2); //change the number inside parenthesis to control the number of decimal vaue output.
 

sourceRectAtTime():

sourceRectAtTime() will give you the information of a rectangle width or hight at the current time. Tou can refer to a layer width and height to change another layer dynamically.

You can apply below expression to a shape layer size property to dynamically change it according to the size of the text layer.

x=thisComp.layer("Your Text layer").sourceRectAtTime().width; //change “Your Text layer” to the the name of your text layer
y=value[1];
[x,y];

 

Difference between two values:

Use length() to find the difference between two values. length(500,100) will return value of 400. It can be useful to get the length between the two layers.

a=thisComp.layer("layer_a").transform.position[0]; // x position of layer a. change "layer_a" to your layer name. 
b=thisComp.layer("layer_b").transform.position[0]; // x position of layer b. change "layer_b" to your layer name.

length(a,b);
a=thisComp.layer("layer_a").transform.position[1]; // y position of layer a. change "layer_a" to your layer name. 
b=thisComp.layer("layer_b").transform.position[1]; // y position of layer b. change "layer_b" to your layer name.

length(a,b);

 

Output lowest or the highest number between two number:

You can use Math.min(value1,value2) or Math.max(value1,value2) to get the lowest value or highest value between two numbers. Math.min(100,10) will return value 10 and Math.max(100,10) will return 100.

a=100;
b=10;

Math.min(a,b); //returns minimum value between a and b. 10 in this case.
a=100;
b=10;

Math.max(a,b); //returns maximum value between a and b. 100 in this case.

 

Referring to the layer number (Index):

Use the index to refer to the layer number.  It is useful to dynamically offset the value of any property. For example, if you apply value*index in z position of a 3D layer and duplicate you will see the layers automatically offset to z position. What is happening here is, z position value is being multiplied by the index value of the layer ie layer number. So if you duplicate layer once, layer one will be in the same position but the z position of layer two will be multiplied by 2 (ie layer number). you can do any math with an index number like value+ index, value-index, value*index, value/index etc. The index is a value so be creative on how you want to use it. If you apply below expression to the positions of a 3D layer, you will see the layers automatically offset in x-direction as you duplicate them.

x=value*index; // Here we are separating the x, y and z value of the layer position and multiplying x value with index.  
y=value; // existing value of the y position.
z=value; // existing value of z position.

[x[0],y[1],z[2]]; // Reconstructing the position array. x is always reffered as [0], y as [1] and z as [2]

 

Offsetting animation based on time – valueAtTime():

You can apply an animation at a specific time using valueAtTime().  For example, if you have a position animation 2 sec long and starting from frame 1 and if you apply to the position value, the animation will start after 1 sec and end at 3 sec though the keyframe starts from frame 1.

valueAtTime(time-1);  // apply to any property with animation, and it will offset that animation by 1 sec.

Another practical example would be is referring to animation from another layer and offsetting it according to the time. For example, animate the opacity of a layer from 0 to 100, now apply below expression to the opacity of a layer above the animated layer. You will see, the opacity of the layer automatically offset by half a sec.

thisComp.layer(index+1).transform.opacity.valueAtTime(time-.5); // remember index+1 is refering to the layer beneath this layer. You can do index-1 to refer to the layer above this layer or dirctly refer to the layer using pick whip.

Remember “index+1” is referring to the layer beneath the layer you are applying this expression. You can do “index-1” to refer to the layer above this layer or directly refer to the layer using the pick whip.

 

 

 

 

…To be continued (last updated 24.02.2020)