Expression Reference

An expression is a short piece of JavaScript on a property that works out the property's value on every frame, instead of - or on top of - its keyframes. wiggle(2, 30) on Position makes a layer shake; time * 90 on Rotation turns it a quarter turn a second; thisComp.layer("Ball").transform.position makes one layer follow another. The language is After Effects': its objects, its functions and its arithmetic on arrays, so expressions from After Effects projects, tutorials and forums work as written.

Adding an expression

  • Alt+click the stopwatch beside a property in the Timeline, or select properties and choose Animation ▸ Add Expression (Alt+Shift+=). The expression starts as the property's own name, transform.position, which gives the property's value unchanged.
  • Click the text under the property to edit it. Enter starts a new line; Ctrl+Enter, Enter on the numeric keypad or a click elsewhere keeps the edit, and Esc drops it. Names of the language are offered as you type, and Tab takes the first.
  • Beside the text are the = switch, which turns the expression off and on without losing it; the graph button, which shows the expression's result in the Graph Editor; the pick whip, which you drag onto another property to write a reference to it; and the ⓕ button, the Expression Language menu listed below.
  • Alt+click the stopwatch again to remove the expression.

A property's value shows in red while an expression drives it. Press E twice quickly (EE) with layers selected to show only the properties that have expressions.

Each property's Parent & Link column also has a pick whip of its own, which links the property to another one in a single drag, writing the expression for you.

When an expression fails

An expression with an error leaves the property at its keyframed value. A warning sign appears beside the property's stopwatch, and the error with its line number appears on a bar along the bottom of the Composition panel. Fix the text and the warning goes. Edit ▸ Preferences ▸ Scripting & Expressions turns the bar off, and sets the editor's font size, colours, line numbers, auto-complete, bracket matching and word wrap.

Values and arrays

A number property's value is a number; a Position, Scale or Anchor Point is an array, [x, y] or [x, y, z]; a colour is [red, green, blue, alpha] from 0 to 1; an angle is in degrees. Arrays add, subtract and multiply as in After Effects - value + [100, 0] moves a position 100 pixels to the right, and value * 2 doubles a scale - and the expression's last line is its result. An expression can read effects' controls too: effect("Slider Control")("Slider") reads a Slider Control on the same layer, which is how an animation gets a control of its own.

The Expression Language menu

The Expression Language menu - the ⓕ button on an expression's row in the Timeline - has 159 entries in 21 groups, and choosing one types it into the expression at the cursor. They are below in the menu's order. Where an entry has arguments, they are named for what they take, and an argument with = may be left out.

Global

Where an expression starts from. thisComp, thisLayer and thisProperty are the composition, layer and property the expression belongs to; comp("Name") and footage("Name") reach another item of the project by its name. time is the composition's current time in seconds and value the property's own value before the expression, so value + [10, 0] moves a position ten pixels right of where its keyframes put it. posterizeTime(12) makes the rest of the expression change only twelve times a second.

  • comp(name) - types comp("name")
  • footage(name) - types footage("name")
  • thisComp
  • thisLayer
  • thisProperty
  • thisProject
  • time
  • colorDepth
  • posterizeTime(framesPerSecond)
  • value

Vector Math

Arithmetic on arrays such as positions and colours. Arrays also add, subtract and multiply with +, - and * as they do in After Effects, so add and mul are seldom needed. length(a, b) is the distance between two points, normalize makes a vector one unit long, and lookAt(from, at) gives the orientation that points a 3D layer at a point.

  • add(vec1, vec2)
  • sub(vec1, vec2)
  • mul(vec, amount)
  • div(vec, amount)
  • clamp(value, limit1, limit2)
  • dot(vec1, vec2)
  • cross(vec1, vec2)
  • normalize(vec)
  • length(vec)
  • length(point1, point2)
  • lookAt(fromPoint, atPoint)

Random Numbers

random() is a new number between 0 and 1 on every frame, random(10, 20) one between 10 and 20, and both take arrays. Each property draws its own sequence; seedRandom(n) picks another, and seedRandom(n, true) makes the number the same on every frame. gaussRandom clusters round the middle, and noise changes smoothly as its input does.

  • seedRandom(offset, timeless=false)
  • random()
  • random(maxValOrArray)
  • random(minValOrArray, maxValOrArray)
  • gaussRandom()
  • gaussRandom(maxValOrArray)
  • gaussRandom(minValOrArray, maxValOrArray)
  • noise(valOrArray)

Interpolation

Map one range onto another. linear(time, 0, 2, 0, 100) goes from 0 to 100 as the time goes from 0 to 2 seconds and holds outside them; ease, easeIn and easeOut do the same with a gentle start, end or both. With three arguments, t runs from 0 to 1.

  • linear(t, value1, value2)
  • linear(t, tMin, tMax, value1, value2)
  • ease(t, value1, value2)
  • ease(t, tMin, tMax, value1, value2)
  • easeIn(t, value1, value2)
  • easeIn(t, tMin, tMax, value1, value2)
  • easeOut(t, value1, value2)
  • easeOut(t, tMin, tMax, value1, value2)

Color Conversion

Convert colours, which are arrays of red, green, blue and alpha from 0 to 1, to and from hue, saturation and lightness, and read a hexadecimal colour such as "#FF8800".

  • rgbToHsl(rgbaArray)
  • hslToRgb(hslaArray)
  • hexToRgb(hexString)

Other Math

Convert between the degrees properties use and the radians JavaScript's Math functions take.

  • degreesToRadians(degrees)
  • radiansToDegrees(radians)

JavaScript Math

JavaScript's own Math object, whose functions all work in expressions; these are the ones the menu offers.

  • Math.cos(value)
  • Math.sin(value)
  • Math.atan2(y, x)
  • Math.sqrt(value)
  • Math.abs(value)
  • Math.round(value)
  • Math.floor(value)
  • Math.min(value1, value2)
  • Math.max(value1, value2)
  • Math.PI

Comp

A composition's layers, by number from the top or by name, its markers, size, duration, frame duration and background colour, and the camera in use.

  • layer(index)
  • layer(name) - types layer("name")
  • layer(otherLayer, relIndex)
  • marker
  • numLayers
  • activeCamera
  • width
  • height
  • duration
  • frameDuration
  • pixelAspect
  • name
  • bgColor
  • displayStartTime

Footage

A footage item's size, duration and name, and for data footage (JSON, CSV and text files in the project) its contents: footage("data.json").sourceData is the parsed file, and dataValue reads one value by its place.

  • width
  • height
  • duration
  • frameDuration
  • pixelAspect
  • name
  • sourceText
  • sourceData
  • dataValue(dataPath)
  • dataKeyCount

Layer ▸ Sub-objects

What a layer holds: its source item, an effect by name or number (effect("Slider Control")("Slider") reads a slider), and a mask.

  • source
  • effect(name) - types effect("name")
  • effect(index)
  • mask(name) - types mask("name")
  • mask(index)

Layer ▸ General

A layer's size, number, parent, In and Out points and start time, and whether it is shown at the current time.

  • width
  • height
  • index
  • parent
  • hasParent
  • inPoint
  • outPoint
  • startTime
  • hasVideo
  • enabled
  • active

Layer ▸ Properties

A layer's transform and its markers, read from another layer: thisComp.layer("Ball").transform.position.

  • anchorPoint
  • position
  • scale
  • rotation
  • opacity
  • marker
  • name

Layer ▸ Space Transforms

Convert points between a layer's own space, the composition's and the 3D world's, taking parents and the camera into account: toComp(anchorPoint) is where a layer's anchor point is in the composition.

  • toComp(point, t = time)
  • fromComp(point, t = time)
  • toWorld(point, t = time)
  • fromWorld(point, t = time)
  • toCompVec(vec, t = time)
  • fromCompVec(vec, t = time)
  • toWorldVec(vec, t = time)
  • fromWorldVec(vec, t = time)

Layer ▸ Other

sourceRectAtTime is the box round what a layer draws, such as a text layer's words, so a shape can be sized to them; sampleImage reads the colour of a layer's pixels round a point.

  • sourceRectAtTime(t = time, includeExtents = false)
  • sampleImage(point, radius = [.5, .5], postEffect = true, t = time)

Property

A property's value at other times, its speed and velocity, its keyframes, and the functions that animate it from them. wiggle(2, 30) shakes a value twice a second by up to 30, loopOut("cycle") repeats its keyframes after the last one (also "pingpong", "offset" and "continue"), valueAtTime(time - 0.5) follows half a second behind, and smooth averages the value round the current time.

  • value
  • valueAtTime(t)
  • velocity
  • velocityAtTime(t)
  • speed
  • speedAtTime(t)
  • wiggle(freq, amp, octaves = 1, amp_mult = .5, t = time)
  • temporalWiggle(freq, amp, octaves = 1, amp_mult = .5, t = time)
  • smooth(width = .2, samples = 5, t = time)
  • loopIn(type = "cycle", numKeyframes = 0)
  • loopOut(type = "cycle", numKeyframes = 0)
  • loopInDuration(type = "cycle", duration = 0)
  • loopOutDuration(type = "cycle", duration = 0)
  • key(index)
  • key(markerName)
  • nearestKey(t)
  • numKeys
  • name
  • propertyIndex

Path Property

A mask's or shape's path as arrays of points and tangents, and createPath, which makes a path an expression can return.

  • points(t = time)
  • inTangents(t = time)
  • outTangents(t = time)
  • isClosed()
  • createPath(points, inTangents = [], outTangents = [], is_closed = true)

Text ▸ Style

The style of a text layer's Source Text, read and changed from an expression on Source Text: text.sourceText.style.setFontSize(80) sets the text's size to 80 whatever the Character panel says, and setFillColor and setText change its colour and its words.

  • text.sourceText.style
  • getStyleAt(charIndex, t = time)
  • createStyle()
  • setFontSize(value)
  • setFillColor(value)
  • setText(value)

Text ▸ Expression Selector

In a text animator's Expression Selector, textIndex is the number of the character, word or line being shaped and textTotal how many there are; the expression returns how much of the animator applies to it, from 0 to 100.

  • textIndex
  • textTotal
  • selectorValue

Key

A keyframe, from key(n) or nearestKey(time): its value, its time and its number.

  • value
  • time
  • index

Marker Key

A marker, from marker.key(n) or marker.key("Name"): its time, duration, comment and the rest of what Marker Settings holds.

  • duration
  • comment
  • chapter
  • url
  • frameTarget
  • eventCuePoint
  • cuePointName
  • parameters
  • protectedRegion

Time Conversion

Turn seconds into frames, timecode or feet and frames, and back, as the composition counts them.

  • timeToFrames(t = time + thisComp.displayStartTime, fps = 1.0 / thisComp.frameDuration, isDuration = false)
  • framesToTime(frames, fps = 1.0 / thisComp.frameDuration)
  • timeToTimecode(t = time + thisComp.displayStartTime, timecodeBase = 30, isDuration = false)
  • timeToFeetAndFrames(t = time + thisComp.displayStartTime, fps = 1.0 / thisComp.frameDuration, framesPerFoot = 16, isDuration = false)
  • timeToCurrentFormat(t = time + thisComp.displayStartTime, fps = 1.0 / thisComp.frameDuration, isDuration = false)

See also

  • Expression Controls - sliders, checkboxes, colours and points for expressions to read.
  • Scripting - scripts, which change the project once rather than a value on every frame.