After Effects expressions library


Jump to: navigation, search

For general information and resources about expressions, go to the After Effects expressions page.


One-sided layers

Apply this expression to the opacity attribute and it'll disappear whenever it's flipped away from the camera. You need to have an active camera in your composition for this one to work.

a = toWorldVec([0,0,1]);
b = thisComp.activeCamera.position - toWorld(anchorPoint);
c = dot(a,b)/length(b);
if (c > 0) 0 else value

Object bouncing on a floor

This will make your layer/object bounce along an imaginary 'floor'. Paste it into the 'position' attribute for your layer.

Vy0 = 500; //initial y velocity (pixels/second)
Vx0 = 100; // initial x velocity (pixels/second)
g = 2500; // gravity (pixels/second/second)
floor = 400;
e = .85; //elasticity
b = floor - position[1];
h = b + Vy0*Vy0/(2*g);
T = Vy0/g + Math.sqrt(2*h/g);
if (time < T){
 y = Vy0*time - g*time*time/2 + b;
 Vy = -(Vy0 - g*T);
 while (true){
   Vy *= e;
   t = T;
   T += 2*Vy/g;
   if (time < T){
     t = time - t;
     y = Vy*t - g*t*t/2;
   }else if (T - t < thisComp.frameDuration){
     y = 0;
[position[0] + Vx0*time, floor - y]

Translate 3D coordinates to 2D

src = comp("3D Data Comp").layer("3D Layer"); src.to_comp(src.anchor_point);

Bouncy scale 'IN'

Apply this expression to the 'scale' parameter of your layer.

k=16; // final scale
a=5; // how quickly the bounce ends
b=20; // how many bounces it'll do (that is, the bounce speed) 

Auto Layer Flip

Apply this expression to your 'Scale' attribute and it'll automatically 'flip' the layer in 3d space when the camera goes behind it. This way, your text will never be seen from behind, for instance. Watch the image below and you'll see that the kid is always on the left side of the frame as the camera orbits around it.

[(toCompVec([0, 0, 1])[2] > 0 ? 1 : -1) * scale[0], scale[1], scale[2]]

Scale relative to distance from camera

This expression maintains the objects apparent size as it moves away from camera.

It's helpful for taking a flat layout and making a "multiplane" layout from it for animation.

cam = this_comp.active_camera;
distance = length(sub(position , cam.position));
s = distance / cam.zoom;
mul(scale, s);

Or you can make the layer a child of the camera and use this simpler expression:

s = 100*(position[2]/parent.zoom);
[s, s, 100]

In this case, if you wanted to move the camera in relation to the multiplane layers, you'd simply create a new camera to animate.

Formatting a number with commas

  1. Create a comp. Name it anything you like.
  2. Create a new text layer and name it "dummy" (this layer can be turned off; it feeds a number to the final text layer. I haven't found a way around this; please let me know if there's another way) - (this can be achieved without the dummy layer by referencing a slider control directly. i.e. copy the script below directly into the source text of a text layer with a slider control on it and change "var num = ThisComp.layer("dummy").text.sourceText;" to "var num = effect("Slider Control")("Slider");" this removes the need for an extra layer.)
  1. Add a slider to the "dummy" layer (Effects > Expression Controls > Slider Control). After Effects should automatically name the slider control "Slider"
  2. Add an expression to Source Text of "dummy" (Animation > Add Expression).
  3. Pickwhip the expression to the "Slider" control from step 3.
  4. Create a second text layer. This will be the layer that displays the formatted number.
  5. Add an expression to Source Text of the new text layer. Replace the code with the following:
 var num = thisComp.layer("dummy").text.sourceText;
 num = Comma(num);
 function Comma(number)
    number = '' + Math.round(number);
    if (number.length > 3)
       var mod = number.length % 3;
       var output = (mod > 0 ? (number.substring(0,mod)) : '');
       for (i=0 ; i < Math.floor(number.length / 3); i++)
           if ((mod == 0) && (i == 0))
                output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
                output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
       return (output);
    else return number;

Now try changing the value of the "Slider" control. Your visible text layer should reflect that number, now formatted with commas. Two catches: it only works for integers, and the slider tops out at 1,000,000. You should be able to pickwhip to any propery that contains a number (decimals are rounded off automatically).

Personal tools
Add to directory