AE EXPRESSION COMPILATION

This is a collection of useful expression presets I’ve either written, copied or modified from elsewhere on the Internet. If you have one to share, please let me know!

Delay inherited parent position

This one has lots of applications, like in this tutorial here. Take any parented layer, apply this to position, and it will follow the parent, but on a delay.

delay = 0.2;
parent.fromWorld(toWorld([0,0,0],time- delay));

Autofade

Great for slideshows or times when you have lots of layers. This fades in and out based on the in point and out point of the layer. Place in the layer opacity.

dur = 0.2;
fadeIn = ease(time, inPoint, inPoint + dur, 0, value);
fadeOut = ease(time, outPoint -dur, outPoint, value, 0);
Math.min(fadeIn, fadeOut);

Fade away from camera

Surprisingly, many light plugins like Lux, Optical Flares and Knoll Light Factory lack a way to diminish the light as it points away from the camera. This can result in really tedious keyframing, unless you have a really awesome expression.

v = -toWorldVec([0,0,1]);
i = v[2] * value;
Math.max(i,0);

Invisible away from camera

After Effects cannot really do a sided object, at least not easily. But, you can have its opacity turn off the visibility when it is facing away from the camera. This way you can even use two objects alternating, creating a fake 2-sided object.

try{
a = toWorldVec([0,0,1]);
b = thisComp.activeCamera.position - toWorld(position);
c = dot(a,b)/length(b);
if (c < 0) value else 0
}
catch(err){value}

Use marker to trigger on for 1 Frame

try{
 dur = thisComp.frameDuration;
 timeToMarker = Math.abs(time - marker.nearestKey(time).time);
 linear(timeToMarker, dur, 0, 100, 0)
 }catch(err){
 100 //or whatevs
 }

Fade Up and Down to/from marker

try{
 dur  = 10 * thisComp.frameDuration;
 marker_time = Math.abs(time - marker.nearestKey(time).time);
 linear(marker_time, dur, 0, value,0)
 }catch(err){
 value
 }

3D Angle between two points

a=thisComp.layer("Null 1").position;
b=thisComp.layer("Null 2").position;
diff = sub(a,b);
lookAt(a,b);

Average position between two points

a=thisComp.layer("Null 1").position;
b=thisComp.layer("Null 2").position;
(a+b)/2;

2D Angle Between Points

a = thisComp.layer("Null 1").toComp([0,0,0]);
b = thisComp.layer("Null 2").toComp([0,0,0]);
diff = sub(a,b);
radiansToDegrees(Math.atan2(diff[1],diff[0]));

Inertial Bounce v1.2

amp = .025;
freq = 2.5;
decay = 2.0;
n = 0;
if (numKeys > 0){
n = nearestKey(time).index;
if (key(n).time > time){
n--;
} }
if (n == 0){
t = 0;
}else{
t = time - key(n).time;
}
if (n > 0 && t < 1){
v = velocityAtTime(key(n).time - thisComp.frameDuration/10);
value + v*amp*Math.sin(freq*t*2*Math.PI)/Math.exp(decay*t);
}else{
value;
}

Auto 2D orient

I know that After Effects has an “orient along path” (which technically should be ‘orient along motion’) but sometimes you need to use it in different parameters, like in a particle rotation. This will set the rotation automatically along its path of movement. Place in rotation. Don’t forget to redefine the layer source ‘L’.

L = thisLayer;
p0 = L.toWorld([0,0,0], time-thisComp.frameDuration);
p1 = L.toWorld([0,0,0]);
v = p1 - p0;
radiansToDegrees(Math.atan2(v[1],v[0]));


Polar Coordinates

angle = 30;
length = 500;
aRad =degreesToRadians( angle - 90);
x=radius*Math.cos(aRad);
y=radius*Math.sin(aRad);
add(position, [x, y]);

Variable Quint Interpolation between a/b

dur = 2; //duration in seconds
a = 0;  //start value
b = 500; //end value
tension = 5;
tensionAdjust =(Math.ceil(tension/ 2) * 2)-1;
function quint(t, b, c, d) {
if ((t/=d/2) < 1) return c/2*Math.pow(t, tensionAdjust) + b;
return c/2*((t-=2)*Math.pow(t, tensionAdjust -1) + 2) + b;
}
t = quint(time - inPoint, 0, 100, dur);
value + linear(t, 0, 100, a, b);

Count with commas

The credit, as usual, goes to Dan Ebberts for applying this, apparently ‘common’ method of using RegEx to create a search pattern for groups of threes, break those groups up and insert a comma.

startCount = 0;
endCount = 10000000;
duration = 5;
n = ease(time, 0, duration, 0, 10000000);
s = "" + Math.round(n);
s.replace(/\B(?=(\d{3})+(?!\d))/g, ",");