After Effects Expressions (JavaScript)

After Adobe made to switch from ExtendScript to full on JavaScript, I grew increasingly interested in them.

A good video tutorial series I found is this one by Parker Young from Animoplex.
And this ancient website from Dan Ebberts also helped me quite a bit.
And of course the official expression language reference from Adobe.

This post is going to serve as my own reference.

width // layer's width
height // layer's height
speed // any value's attribute, similar to differentiating a function

random(min, max)
gaussRandom() // with bell curve/normal distribution
gaussRandom(min, max)
seedRandom(seed, timeLock) //seed is int, timeLock is a Bool locking random methods' values over time

toFixed(n) // rounds to n digits after the decimal point
length(a, b)
clamp(a, min, max) // can also be used as an object method: x.clamp(min, max)

toComp(point) // layer space -> comp space
toWorld(point) // layer space -> world space
fromComp(point) // comp space -> layer space
fromWorld(point) // world space -> layer space

linear(a, n1, n2) // maps a to (n1,n2) from a <- (0,1)
linear(a, aMin, aMax, n1, n2) // maps a to (n1,b2) from a <- (aMin,aMax) 
ease() // similar to linear() but with easing on both ends

substr(a, b) // string trimming
replace(shit i dont know how to use this one) // regex replace
sourceRectAtTime(time) // layer bounding box

eval(string) // evaluates string
$.evalFile(pathToFile) // evaluates pathToFile

To better understand transform spaces: and from Felt Tips
toWorld(point) gives world coordinates (measured from world origin [0,0,0]) to point in the layer’s own coordinate system (point = [0,0,0] would be layer’s top left corner)

3D layers’ positions are measured relative to their anchor point from the world origin.
Parented layers are measured relative to their anchor point from the parent’s origin(top left corner [0,0,0]).
However, toWorld() on a 3D parented layer still gives their position in world space (relative to world origin) and not relative to its parent.
3D layer method fromWorld can convert an absolute position relative to the layer.

World space only exists in 3D.
Comp space only exists in 2D.
World space is the 3D counterpart of comp space and comp space is the 2D counterpart of world space.
Origin in comp space is the top left corner of the comp.
toComp(point) still gives an array in 3 dimensions if point is in 3D, the third dimension is the camera’s zoom.
Do not fuck around with fromComp() and 3D layers, use fromCompToSurface() instead (implying to also not use fromCompToSurface() with 2D layers).

Different ways to declare variables:

x = 1; // always global
var x = 1; // global or function scoped/local
let x = 1; // block scoped (between {})
const x = 1; // similar to let but immutable

There is a difference between using quotes “” and backticks “ when defining strings.

One thought on “After Effects Expressions (JavaScript)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s