studio.tue
after effects expressions

expressions worth keeping

the short answer

how do i add an expression in after effects? alt-click (option-click on mac) the stopwatch next to the property, paste over the text that appears, and click anywhere outside it. the property's value turns red and follows the expression from then on.

the ones that end up in almost every project here. find what you want it to do, paste it on that property, and change the numbers at the top. the rest you can leave alone. each one has a small example, drawn in your browser with that expression's own math and numbers (wiggle's randomness won't match after effects exactly).

what i want it to douseproperty
squash wide and short, then tall and skinny, after a keyframejelloscale
go past a keyframe and spring backovershootanything
rock side to side after a keyframewobblerotation
repeat my keyframes foreverloopanything
shake randomlywiggleanything
spin foreverspinrotation
grow and shrink foreverpulsescale
follow another layer, a bit latefollowposition
type on letter by lettertypewritersource text
count up a numbercountersource text
keep the anchor in the middlecenter anchoranchor point
flicker on and offflickeropacity

keyframe bounces

keyframe the property like normal, then paste on that same property. the bounce kicks in right after every keyframe. change the top 3 lines, leave the rest alone.

jello

scale

after each scale keyframe it squashes wide and short, then tall and skinny, back and forth until it settles.

scale
amount = 15; // how much it squishes, in %
speed = 3; // squishes per second
settle = 5; // how fast it calms down (higher = shorter)

n = numKeys ? nearestKey(time).index : 0;
if (n > 0 && key(n).time > time) n--;
if (n == 0) {
  value;
} else {
  t = time - key(n).time;
  w = amount * Math.sin(speed * t * 2 * Math.PI) * Math.exp(-settle * t);
  value + [w, -w];
}

tall and skinny first: change the last line to value + [-w, w];. on a 3d layer, add a 0 inside the brackets on the last line: [w, -w] becomes [w, -w, 0].

overshoot

anything

goes past the keyframe and springs back. the faster it was moving, the bigger the overshoot.

any property
amount = 0.05; // how far past it goes (0.05 = a little, 0.2 = a lot)
speed = 4; // springs per second
settle = 6; // how fast it calms down (higher = shorter)

n = numKeys ? nearestKey(time).index : 0;
if (n > 0 && key(n).time > time) n--;
if (n == 0) {
  value;
} else {
  t = time - key(n).time;
  v = velocityAtTime(key(n).time - 0.001);
  value + v * amount * Math.sin(speed * t * 2 * Math.PI) * Math.exp(-settle * t);
}

wobble

rotation

wobbles side to side after each rotation keyframe, then settles.

rotation
amount = 15; // how far it rocks, in degrees
speed = 3; // rocks per second
settle = 5; // how fast it calms down (higher = shorter)

n = numKeys ? nearestKey(time).index : 0;
if (n > 0 && key(n).time > time) n--;
if (n == 0) {
  value;
} else {
  t = time - key(n).time;
  value + amount * Math.sin(speed * t * 2 * Math.PI) * Math.exp(-settle * t);
}

jello on a marker

scale

no keyframes needed. drop a layer marker wherever you want it to jiggle.

scale
amount = 15; // how much it squishes, in %
speed = 3; // squishes per second
settle = 5; // how fast it calms down (higher = shorter)

n = marker.numKeys ? marker.nearestKey(time).index : 0;
if (n > 0 && marker.key(n).time > time) n--;
if (n == 0) {
  value;
} else {
  t = time - marker.key(n).time;
  w = amount * Math.sin(speed * t * 2 * Math.PI) * Math.exp(-settle * t);
  value + [w, -w];
}

on a 3d layer, add a 0 inside the brackets on the last line: [w, -w] becomes [w, -w, 0].

jello, different on every keyframe

scale

one row per keyframe, top row = keyframe 1: how much, how fast, how quick it settles. each hit keeps going when the next one lands, so nothing snaps.

scale
// JELLO PER KEYFRAME (on Scale)
// One row per Scale keyframe. Top row = keyframe 1.
// amount = how much it squishes, in % (0 = none, minus = tall first)
// speed = squishes per second (your fps / frames between keys: 30 fps, 10 f apart = 3)
// settle = how fast it calms down (higher = shorter)

jello = [
// amount speed settle
  [ 3, 3, 4 ],
  [ 3, 3, 4 ],
  [ 3, 3, 4 ],
  [ 3, 3, 4 ],
  [ 3, 3, 4 ]
];

// every keyframe's jello keeps going and they add up, each one eases in
s = 0;
for (i = 1; i <= numKeys && key(i).time <= time; i++) {
  row = jello[i - 1];
  if (!row || row[0] == 0) continue;
  t = time - key(i).time;
  s += row[0] * Math.sin(row[1] * t * 2 * Math.PI) * Math.exp(-row[2] * t) * (1 - Math.exp(-20 * t));
}
value + [s, -s];

if the motion is fighting, set speed to your frame rate divided by the frames between keys. 30 fps with keys 10 frames apart = 3. then each new key pushes with the squish instead of against it. on a 3d layer, add a 0 inside the brackets on the last line: [s, -s] becomes [s, -s, 0].

tilt paired with jello

rotation

on rotation of the same layer. it rocks on the scale keyframes, one number per scale keyframe, in degrees.

rotation
// Tilt on every Scale keyframe. One number per Scale keyframe, in degrees.
// 0 = no tilt, minus = rocks the other way first.
tilts = [0, 5, 2, -1.5, 2];
speed = 2.5; // rocks per second (a bit slower than the jello)
settle = 4; // how fast it calms down

sc = transform.scale;
r = 0;
for (i = 1; i <= sc.numKeys && sc.key(i).time <= time; i++) {
  a = tilts[i - 1] || 0;
  t = time - sc.key(i).time;
  r += a * Math.sin(speed * t * 2 * Math.PI) * Math.exp(-settle * t) * (1 - Math.exp(-20 * t));
}
value + r;

loops and wiggle

repeat what you keyed, or shake it.

loop

anything with keyframes

repeats your keyframes after the last one. swap the word in the quotes for the kind of loop.

any property
loopOut("cycle");
  • "cycle"plays the keyframes over and over
  • "pingpong"plays forward, then backward, forever
  • "offset"keeps adding on each time, like a staircase
  • "continue"keeps going at the last speed, no repeat

for a smooth cycle, make the last keyframe match the first.

wiggle

anything

the first number is how often, the second is how far.

any property
wiggle(2, 30);
position, only side to side
w = wiggle(2, 30);
[w[0], value[1]];
choppy, stop-motion style
posterizeTime(8);
wiggle(2, 30);

wiggle that loops

anything

ends exactly where it started, so it loops clean. change the top 3 lines; loopTime is your loop length in seconds.

any property
speed = 1; // wiggles per second
amount = 50; // how far
loopTime = 3; // loop length in seconds
// --- leave the rest ---
t = time % loopTime;
w1 = wiggle(speed, amount, 1, 0.5, t);
w2 = wiggle(speed, amount, 1, 0.5, t - loopTime);
linear(t, 0, loopTime, w1, w2);

motion

spin, pulse, follow and stagger, mostly with no keyframes at all.

spin

rotation

90 = degrees per second. make it negative to spin the other way.

rotation
value + time * 90;

pulse

scale

grows and shrinks forever, on top of the size you set. change the top 2 lines.

scale
amount = 5; // how much it grows, in %
speed = 1; // pulses per second
s = amount * Math.sin(time * speed * 2 * Math.PI);
value + [s, s];

on a 3d layer, add a 0 inside the brackets on the last line: [s, s] becomes [s, s, 0].

follow

position

copies another layer's movement, 0.15 seconds late. change "Leader" to that layer's name.

position
thisComp.layer("Leader").transform.position.valueAtTime(time - 0.15);
position, the layer right above
thisComp.layer(index - 1).transform.position.valueAtTime(time - 0.08);

the second one follows whatever layer is right above this one. paste it on a stack of layers for a snake trail.

stagger

anything with keyframes

animate one layer, paste this, then duplicate it. each copy starts 0.05 seconds after the one above.

any property
valueAtTime(time - (index - 1) * 0.05);

text

all of these go on source text. the first two need a slider control on the layer (effect → expression controls → slider control).

typewriter

source text

types on letter by letter. keyframe the slider from 0 to the number of letters.

source text
value.substr(0, Math.round(effect("Slider Control")("Slider")));

counter

source text

counts up a number. keyframe the slider from the start number to the end number.

source text
Math.round(effect("Slider Control")("Slider"));
source text, with commas (1,250,000)
Math.round(effect("Slider Control")("Slider")).toLocaleString();

copy another layer's text

source text

change "Title" to the layer's name. edit that one and this updates.

source text
thisComp.layer("Title").text.sourceText;

extras

small ones that save a lot of fiddling.

center anchor

anchor point

keeps it in the middle, even when the text or shape changes size.

anchor point
r = sourceRectAtTime(time, false);
[r.left + r.width / 2, r.top + r.height / 2];

flicker

opacity

flickers on and off at random, up to 12 times a second.

opacity
posterizeTime(12); // how often it can flip, per second
random() > 0.3 ? 100 : 0; // 0.3 = off about 30% of the time

random per layer

anything

every duplicate gets its own random value that doesn't change. here it's a rotation from 0 to 360.

rotation
seedRandom(index, true);
random(0, 360);

common questions

why doesn't my bounce do anything?

the keyframe bounces kick in right after a keyframe on the same property they're pasted on, so with no keyframes there, nothing happens. jello on a marker needs a layer marker instead, and the tilt reads the scale keyframes.

why does the typewriter or the counter show an error?

they read a slider called Slider Control on the same layer. add one with effect → expression controls → slider control, and keep its name.

how do i turn an expression off without deleting it?

click the = switch next to the property. click it again to turn it back on.

which expression engine are these for?

javascript, the default since after effects 16.0. the engine is under file → project settings → expressions.

how do i turn an expression into keyframes?

select the property and choose animation → keyframe assistant → convert expression to keyframes. it bakes one keyframe per frame and switches the expression off.

will a project with these open for someone else?

yes. they're plain after effects expressions, with nothing to install, so the project opens and renders the same on any machine.

by hand, step by step

all guides →