Formulas
Work a setting’s value out as the clip plays: wiggle it, loop its keys, spring it, move it to the music, or have it follow another clip.
A formula works a setting’s value out at every frame. It can wander at random, carry on its keys, spring past them, pulse on the music’s beat, or follow another clip’s setting. Any setting that can have keyframes can have a formula, on top of its keys or without them.
Write a formula
Open the formula editor in any of these ways:
- Alt-click the setting’s diamond in the Inspector.
- Right-click the diamond or the setting’s name and choose Add formula…, or Edit formula… when it has one.
- In the keyframe editor, click the formula button on the property’s row, or right-click the row.
- In Repeat and wiggle…, click Add formula….
Type the formula into the box. It’s applied as you type, so the clip moves in the program monitor while you write. Under the box you see what it gives at the playhead, and at the clip’s start and end. What can’t be worked out is underlined and said, with its line and column; click it, or Show where, to select it. Until it reads, the setting keeps the last formula that did.
Everything you type while the editor is open is one step to undo. Press CtrlEnter or click Close when you’re done, or Esc.
The setting’s value as its keys give it, or as it’s set, is value in the formula: value + 50 puts it 50 further on. Values are in the units the Inspector shows: pixels for position, degrees for rotation, percent for opacity and scale.
Suggestions
As you type a name, the editor offers what it could be: what a formula can use, your own variables, the clip’s settings and other clips’ names. Each says what it does.
| Keys | What they do |
|---|---|
| ↑, ↓ | Pick a suggestion. PageUp and PageDown go further. |
| Enter, Tab | Put the picked suggestion in. |
| Esc | Close the suggestions. |
| CtrlSpace | Show the suggestions for where the caret is. |
| Enter | Without suggestions, a new line, indented as the one before. |
Ready-made formulas
Ready-made puts in a formula written for the setting, with amounts in its units, to use as it is or change. It takes the place of what’s in the box; CtrlZ in the box brings that back.
| Formula | What it does |
|---|---|
| Wiggle | Wanders smoothly about its value, twice a second. |
| Shake | Jitters quickly and finely, as a camera held by hand. |
| Drift | Wanders slowly and far, now one way and now the other. |
| Swing | Swings evenly back and forth, once a second. |
| Keep going | Changes at a steady rate from the clip’s start: a spin, or a slow push in. |
| Flicker | Dips at random, twelve times a second, as a failing light does. |
| Loop the keys | Plays the keys again from the first after the last. |
| Loop the keys back and forth | Plays the keys backwards after the last, then forwards again. |
| Carry on past the last key | Keeps going at the speed it reached the last key at. |
| Spring after each key | Overshoots each key it arrives at and springs back to it. |
| Bounce after each key | Bounces back off each key it arrives at, as a dropped ball does. |
| Stop motion | Moves in steps, eight a second, as if shot a frame at a time. |
| Pulse on the beat | Jumps on every beat of the music and settles back before the next. |
| Follow the sound | Grows with how loud the sequence’s sound is. |
The ones under From the keys need two or more keys on the setting.
Link to another setting
Link puts in another setting, of this clip or another clip in the sequence, for the formula to follow. Pick This clip or a clip, then the setting: clip("Logo").rotation is the Logo clip’s rotation. Change it to follow it another way: clip("Logo").rotation * -1 turns the other way, and clip("Logo").rotation.delay(0.5) follows half a second behind.
When you rename a clip, the formulas that read it by its old name are given its new one.
Seed
Random numbers, wiggles and noise start from the formula’s Seed, so they’re the same every time the project plays. Two clips given the same formula have different seeds, so they move differently. Set the seed yourself to make them move alike, or click New seed to pick another at random.
Switch off, bake or remove
The formula editor has:
- On: switch it off to keep the formula but give the setting its value from before it, its keys or as it’s set.
- Bake into keys: put keys in place of the formula, one a frame across the clip, that give what it gives. Keys that a straight line between their neighbors gives are left out. The formula, keys, repeats and wiggle it had are replaced.
- Remove: take the formula off. The keys, repeats and wiggle stay.
To do it to every formula of the selected clips, open the command palette and choose Switch the selected clips’ formulas off, Switch the selected clips’ formulas on, or Bake the selected clips’ formulas into keys. Each is one step to undo.
Formulas on a locked track can be read but not changed. Unlock the track to change them.
Where a formula shows
- In the Inspector, a setting with a formula has a formula button beside its diamond: lit while the formula’s on, struck through while it’s off. Point at it to see the formula; click it to edit.
- In the keyframe editor, the property’s row has the same button. Keys view draws a dotted line along the row over the clip, and Value view draws the curve the formula gives as a dashed line over the keys’ curve.
- On the timeline, pointing at a clip’s volume or pan line says Formula when it has one.
A value you set in the Inspector, or key at the playhead, while the setting has a formula is worked back through the formula, so the setting shows what you set. With value + 100, setting it to 150 sets it, or its key, to 50.
What a formula can say
Formulas are written as JavaScript expressions are, as in After Effects:
- Numbers,
"text",trueandfalse. +,-,*,/,%(what’s left over) and**(to the power).- Comparisons:
<,<=,>,>=,==and!=;&&(and),||(or),!(not), anda ? b : c(bwhenais true,cwhen not). - Variables:
let size = 20gives a name a value, andsize = size * 2gives it another. Aconstkeeps the one it’s given. if (time < 2) { 0 } else { 100 }, andreturnto end the formula there with a value.- Comments, after
//or between/*and*/.
A formula can have many lines. Its value is the value of the last line it runs. There are no loops, so it always ends, and quickly.
Values
| Name | What it is |
|---|---|
value | The setting’s value before its formula: its keys, or what it’s set to. |
time | The time in the sequence, in seconds. |
clipTime | Seconds since the clip started. |
progress | How far through the clip it is: 0 at its start, 1 at its end. |
frame, fps, frameDuration | The frame it’s on from 0, frames a second, and how long a frame is. |
start, end, duration | When the clip starts and ends in the sequence, and how long it is, in seconds. |
width, height | The frame’s size, in pixels. |
inPoint and outPoint are start and end, as After Effects calls them. timeToFrames(t) and framesToTime(frames) turn one into the other.
Keys and physics
| Formula | What it gives |
|---|---|
loopOut(type, numKeys) | The keys repeated after the last. type is "cycle", "pingpong", "offset" (each time on from where it got to) or "continue" (on at its speed). With numKeys, only the last few repeat. |
loopIn(type, numKeys) | The keys repeated before the first. |
loop(type) | The keys repeated before the first and after the last. |
spring(freq, decay, amount) | After each key, the value carries on the way it was going and springs back: freq rings a second, decay how soon it settles. |
bounce(elasticity, gravity, maxBounces) | After each key, the value bounces back off it as a ball does, each bounce elasticity as high. |
smooth(width, samples) | The value averaged over width seconds around now: smooths out jitter. |
valueAt(t), velocityAt(t) | The value before its formula at time t of the sequence, and how fast it’s changing then. |
velocity, speed | How fast it’s changing now, in its units a second; speed whichever way. |
numKeys, key(n), nearestKey(t) | How many keys it has; its nth key from 1, and the one nearest t, with their .time and .value. |
Math and shaping
| Formula | What it gives |
|---|---|
linear(t, tMin, tMax, from, to) | From one value to another as t goes from tMin to tMax, held beyond them. |
ease, easeIn, easeOut | As linear, easing in and out, in, or out. |
clamp(x, min, max) | x, held between min and max. |
lerp(a, b, u) | From a (u 0) to b (u 1). |
smoothstep(edge0, edge1, x), step(edge, x) | 0 below the edge, 1 above it: smoothly between the two, or at once. |
sin, cos, tan, asin, acos, atan, atan2 | Angles in radians. degrees(r) and radians(d) turn one into the other. |
abs, sign, floor, ceil, round(x, places), trunc, fract | Signs, rounding and fractions. |
mod(x, y) | What’s left from x after taking as many y as fit, never negative: for repeating. |
min, max, pow, sqrt, cbrt, exp, log, log2, log10, hypot | As in math. |
PI, TAU, E | π (half a turn in radians), τ (a whole turn), and e. |
Math.sin, Math.PI and the rest of JavaScript’s Math work too, for formulas brought from elsewhere.
Random and noise
| Formula | What it gives |
|---|---|
wiggle(freq, amount, octaves, ampMult) | The value wandering smoothly, freq times a second, up to amount either way. More octaves add finer wiggles, each ampMult as big. |
noise(x) | Smooth noise from -1 to 1 that changes as x does: noise(time * 2). |
random(min, max) | A random number, a new one each frame. random() is 0 to 1, random(max) 0 to max. |
gaussRandom(min, max) | A random number, most often in the middle of the range, now and then beyond it. |
seedRandom(seed, timeless) | Another seed for what comes after it. With timeless true, random() gives the same number every frame. |
posterizeTime(fps) | Steps time at that many frames a second for the rest of the formula: a jumpy, stop-motion look. |
Sound and beats
| Formula | What it gives |
|---|---|
audioAmplitude(clipName, smoothing) | How loud the sequence’s sound is now, from 0 to 1, or one clip’s by its name, smoothed over that many seconds. |
beat(decay) | A pulse on every beat of the music: 1 on the beat, fading as decay says. |
bass(clipName, smoothing) | How loud the sound’s lows are now, under about 180 Hz: 0 to 1 of how loud they get. |
mids(clipName, smoothing) | How loud the middle of the sound is now, about 180 Hz to 2.8 kHz. |
treble(clipName, smoothing) | How loud the sound’s highs are now, over about 2.8 kHz. |
frequency(hz, clipName, smoothing) | How loud the sound is now around a frequency, an octave wide: frequency(80). |
kick(decay, clipName) | A pulse on every kick drum, as hard as it’s hit: 1 for the hardest, fading as decay says. |
snare(decay, clipName) | A pulse on every snare or clap. |
hats(decay, clipName) | A pulse on every hi-hat or cymbal tick. |
beatPhase() | How far through the beat it is: 0 on the beat, nearly 1 just before the next. |
beatCount() | How many beats have gone by, from 1. |
bpm | The music’s tempo now, in beats a minute: 0 without beats. |
Each takes a clip’s name to hear only that clip, and leaves it out to hear the whole sequence as it’s mixed. The sound and its beats are found once its media is analyzed. Until then they give 0, and the editor says so. See Music and beats. To move a setting with the sound without writing a formula, see React to sound.
Other clips and settings
| Formula | What it is |
|---|---|
clip("Logo") | Another clip in the sequence, by its name. Where two have the name, it’s the one there at the time, or nearest it. |
clip("Logo").x | That clip’s setting, its formula and all, with its .value, .velocity, .speed, .valueAt(t), .velocityAt(t), .numKeys and .key(n). |
.delay(seconds) | The setting as it was that long ago: clip("Logo").x.delay(0.2). |
effect("Box blur").radius | A setting of one of this clip’s effects, by its name or its place in the list from 1: effect(1).amount. Where it has two of an effect, effect("Box blur 2") is the second. clip("Logo").effect("Box blur").radius is another clip’s. |
thisClip, thisProperty | This clip, and this setting: thisProperty.key(1).value. |
thisComp.layer("Logo") | clip("Logo"), as After Effects writes it. thisComp.width, thisComp.height and thisComp.duration are the sequence’s. |
A formula can’t read itself, through other formulas or straight. Where a setting it reads reads it back, that setting has its value from before its formula, and the editor says so.
Examples
| Formula | What it does |
|---|---|
wiggle(2, 30) | Wanders 30 either way, twice a second. |
value + time * 90 | Turns a quarter turn a second, from where it’s keyed. |
loopOut("pingpong") | Plays the keys forwards and backwards for as long as the clip lasts. |
value * (1 + beat(6) * 0.1) | Grows by a tenth on every beat. |
value + audioAmplitude() * 50 | Rises with the sound. |
value * (1 + kick(10) * 0.2) | Jumps a fifth bigger on every kick drum. |
clip("Logo").rotation * -1 | Turns as the Logo turns, the other way. |
linear(progress, 0, 1, 0, 360) | One whole turn across the clip. |
posterizeTime(8) then wiggle(3, 20) on the next line | A jumpy wiggle, eight steps a second. |
round(value / 10) * 10 | Snaps to tens. |
For scripts, the agent API gives, checks, switches and bakes formulas with setExpression, checkExpression, enableExpressions and bakeExpression.