Expressions

A line of JavaScript that drives a value: links between layers, loops and procedural motion, with every command to look up.

Expressions

When a move should follow a rule, write it once. Alt-click a property’s stopwatch and type an expression in the line that opens below it.

  • Link properties across layers and comps, loop keyframes, wiggle, smooth, remap ranges, or work with time, random numbers and noise.
  • Arrays add and multiply like numbers, so position math stays short.
  • Drive your rigs from Expression Controls: slider, checkbox, angle, point and color.
  • A mistake shows in red under the line. The property keeps its last good value while you fix it.
  • Not sure how a command is written? Expression Reference lists them all.

Expression Reference

Every command an expression can use, from time and keyframes to wiggle, loops, random numbers, vector math and color: how it is written, what it does and a working example. Search it right here, and click an example to copy it.

In the app it is one click away under Tools ▸ Expression Reference, the same list, ready while you write.

Expression Reference

Every command an expression can use. Click an example to copy it.

Time & Value

time

The comp's current time in seconds.

value

The property's own value at this moment - its keyframes, before the expression.

thisProperty

The property the expression is on: its keyframes, velocity and values at other times.

velocity

How fast the property changes right now, in its units per second (one value per dimension).

speed

How fast it changes in any direction: the length of the velocity.

valueAtTime(t)

The property's value at time t (seconds) - lag, echo and delay effects.

velocityAtTime(t)

The velocity at time t.

speedAtTime(t)

The speed at time t.

posterizeTime(fps)

Runs the rest of the expression at only fps pictures a second: choppy, stop-motion movement.

colorDepth

Bits per color channel the comp works in (Hope Creative: 32).

Keyframes

numKeys

How many keyframes the property has.

key(index)

The keyframe at index (from 1): its .time, .value and .index.

nearestKey(t)

The keyframe closest to time t (same fields as key).

Wiggle, Smooth & Loop

wiggle(freq, amount, octaves = 1, ampMult = 0.5, t = time)

Random, smooth shaking around the value: freq times a second, up to amount units. More octaves add finer detail.

temporalWiggle(freq, amount, octaves = 1, ampMult = 0.5, t = time)

Wiggles the time instead of the value: the animation runs a little ahead and behind, keeping its shape.

smooth(width = 0.2, samples = 5, t = time)

Averages the value over width seconds around t: takes the jitter out of keyframes or a track.

loopOut(type = "cycle", numKeyframes = 0)

Repeats the animation after its last keyframe. type: "cycle", "pingpong", "offset" (adds up) or "continue" (keeps the last speed). numKeyframes: loop only the last ones (0 = all).

loopIn(type = "cycle", numKeyframes = 0)

The same before the first keyframe.

loopOutDuration(type = "cycle", duration = 0)

Like loopOut, the loop given as the last duration seconds instead of keyframes.

loopInDuration(type = "cycle", duration = 0)

Like loopIn, by duration seconds.

Interpolation

linear(t, tMin, tMax, value1, value2) · linear(t, value1, value2)

Maps t from tMin…tMax onto value1…value2 in a straight line, held at the ends (without tMin / tMax: t from 0 to 1). Works on numbers and arrays.

ease(t, tMin, tMax, value1, value2)

Like linear, eased in and out (smooth start and stop).

easeIn(t, tMin, tMax, value1, value2)

Like linear, eased at the start only.

easeOut(t, tMin, tMax, value1, value2)

Like linear, eased at the end only.

Random & Noise

random() · random(max) · random(min, max)

A random number (or array, with arrays given) - a new one every frame unless seedRandom says timeless.

gaussRandom(min, max)

Like random, but most results near the middle (a bell curve).

seedRandom(seed, timeless = false)

Picks which random numbers come: the same seed gives the same ones. timeless = true: the same on every frame.

noise(value)

Smooth noise between −1 and 1 for a number or point: neighbouring inputs give neighbouring results.

Layer

thisLayer

The layer the expression is on.

index

The layer's number in the timeline (from 1, at the top).

name

The layer's name.

inPoint

When the layer starts in the comp (seconds).

outPoint

When the layer ends (seconds).

startTime

Where the layer's own time 0 lies in the comp (seconds).

width

The layer's width in pixels.

height

The layer's height in pixels.

hasParent

Whether the layer has a parent.

parent

The layer's parent (an error when it has none: ask hasParent first).

transform

The layer's Transform: transform.position, .scale, .rotation, .opacity …

text.sourceText

A text layer's text.

audioLevels

The layer's Audio Levels (dB).

layer.active

Whether a layer is on and shows at the current time.

layer.enabled

Whether a layer is switched on (its eye).

effect(name)(parameter)

A parameter of one of this layer's effects, by the effect's and the parameter's name.

thisComp.layer(nameOrIndex)

Another layer of the comp, by its name or number.

Transform

position

The layer's Position (x, y).

anchorPoint

The layer's Anchor Point.

scale

The layer's Scale in %.

rotation

The layer's Rotation in degrees (on a 3D layer: Z Rotation).

zRotation

A 3D layer's Z Rotation.

xRotation

A 3D layer's X Rotation.

yRotation

A 3D layer's Y Rotation.

zPosition

A 3D layer's depth (Position Z).

opacity

The layer's Opacity in %.

pointOfInterest

A camera's or light's Point of Interest.

Comp

thisComp

The comp the layer is in.

comp(name)

Another comp of the project, by name.

thisComp.numLayers

How many layers the comp has.

thisComp.duration

The comp's length in seconds.

thisComp.frameDuration

One frame's length in seconds (1 / frame rate).

thisComp.width

The comp's width in pixels.

thisComp.height

The comp's height in pixels.

thisComp.name

The comp's name.

thisComp.bgColor

The comp's background color [r, g, b, a].

thisComp.pixelAspect

The comp's pixel aspect (always 1).

thisComp.displayStartTime

The time the comp's timeline starts at (always 0).

Space

toComp(point, t = time)

A point of the layer (its own pixels) where it lies in the comp.

fromComp(point, t = time)

A point of the comp in the layer's own pixels.

toWorld(point, t = time)

The same as toComp here.

fromWorld(point, t = time)

The same as fromComp here.

sourceRectAtTime(t = time)

The box of what the layer shows: .left, .top, .width, .height (text, shapes).

Vector Math

add(a, b)

a + b, number by number for arrays (what + does in expressions too).

sub(a, b)

a − b, number by number for arrays.

mul(a, b)

a × b (an array times a number scales it).

div(a, b)

a ÷ b (an array divided by a number).

dot(a, b)

The dot product of two arrays.

cross(a, b)

The cross product of two 3D arrays.

length(a) · length(a, b)

An array's length, or the distance between two points.

normalize(a)

The array scaled to length 1 (its direction).

clamp(value, min, max)

The value held within min … max (arrays: each number).

lookAt(from, to)

The rotation (3D, degrees) that points from one point at another.

degreesToRadians(degrees)

Degrees in radians (for Math.sin / Math.cos).

radiansToDegrees(radians)

Radians in degrees.

Color

rgbToHsl([r, g, b, a])

A color (0 … 1) as hue, saturation, lightness, alpha.

hslToRgb([h, s, l, a])

Back from hue, saturation, lightness to a color.

hexToRgb("#rrggbb")

A hex color as [r, g, b, a].

Time Conversion

timeToFrames(t = time, fps, isDuration)

Seconds as frames (fps: the comp's).

framesToTime(frames, fps)

Frames as seconds.

timeToTimecode(t = time, fps, isDuration)

Seconds as timecode HH:MM:SS:FF (a counter in a text).

timeToCurrentFormat(t = time, fps, isDuration)

Seconds as the comp shows time (timecode).

Expression Controls

effect("Slider Control")("Slider")

A number to keyframe and read in expressions (Effects ▸ Expression Controls).

effect("Checkbox Control")("Checkbox")

On or off (1 or 0).

effect("Angle Control")("Angle")

An angle in degrees.

effect("Point Control")("Point")

A point [x, y].

effect("Color Control")("Color")

A color [r, g, b, a].