Skip to main content

After Effects Expressions

Expressions in After Effects is basically in-line scripting (language = JavaScript) within an effect or transform. It's incredibly powerful and useful. 

Expressions Resources

Sample Expressions

This is where I dump some useful AE expressions that I use often. 

Remember that you can copy and paste expressions by selecting the expression, then going to File→Copy Expression Only and then pasting it onto a layer that has the same variables - it will automatically paste that expression on the same property!

Layer Controls

Using another Layer’s visibility (eye) to turn off/on another layer (apply to opacity)
target = thisComp.layer("MyComp") ;
o1 = [100] ;
o2 = [0] ;
if (target.active == true) {o1} else {o2} ;
Using a Checkbox Control to toggle an effect on or off
thisComp.layer("FX CONTROLS").effect("FX ON / OFF")("Checkbox")*100
Using a Checkbox Control to toggle a layer on or off
if (comp("CompName%%%").layer("GRID CONTROL").effect("CheckboxName%%%")("Checkbox") > 0 ) {100} else {0};

Text

Source text populates from timecode in current timecode format 

(numbers or 0:00:00:00)

 timeToCurrentFormat()
Source text populates in frame number format
timeToFrames()
Source text populates in frame number format with leading digits
frames = timeToFrames(time, 60); // Replace fps with your actual frames per second value
framesString = pad(frames, 5); // 5 is the desired # of digits, adjust as needed

function pad(num, size) {
	var s = num + "";
	while (s.length < size) s = "0" + s;
	return s;
}

framesString;
Source text populates frame number in number format with prefix
timeX = timeToFrames()
"frame: "+timeX
Source text for a text layer is comp name
compName = thisComp.name
Source Text Parses From Comp Name

(ie, if comp name = “FARTS//Jeff Bezos”, it will populate “Jeff Bezos” in the text field).

Learned this from ukrmedia (source)

compName = thisComp.name
thisComp.name.split("//")[1]

Position & Scale

This will automatically scale, and keep proportional, a layer to fit a comp based on width

scaleX = thisComp.width*(100/thisLayer.width);
scaleY = thisComp.width*(100/thisLayer.width);

[scaleX, scaleY]

This will automatically position a 200x200px box (shape layer) to fit into a 200x200 (per box) grid and place it in the third box from the closest corner. The “-1” multiplier determines which corner in polar terms (X,Y). -1, -1 for top left; 1, -1 for top right; -1, 1 for bottom left; 1, 1 for bottom right. 

positionX = -1*[Math.floor(thisComp.width/2)-(Math.floor(thisComp.width/2)%200)-300];
positionY = -1*[Math.floor(thisComp.height/2)-(Math.floor(thisComp.height/2)%200)-300];
[positionX, positionY]
Using the size of an object to set the anchor point as the top left of that object 
mathX = content("Rectangle 1").content("Rectangle Path 1").size[0];
mathY = content("Rectangle 1").content("Rectangle Path 1").size[1];
x=value[0]+mathX/2-mathX;
y=value[1]+mathY/2-mathY;
[x,y]