Using Expressions in After Effects

Steven Harley

After Effects (AE) is a great tool for prototyping UI animations, but we’re always looking for ways to speed up our workflow. AE comes with support for expressions, which create relationships between layer properties or keyframes so the designer can animate layers without defining each keyframe by hand. We’ll go through some basics of using and defining expressions, as well as how to modify them to best suit your needs. We have a great primer on AE if you want to become more familiar with the terms and tools before jumping into expressions.

So, what is an expression

Expressions will look pretty familiar to most of the readers that frequent the Giant Robots blog. They’re very similar to scripts you would use on the web, but rather than acting on the application itself, they define how a property should behave. Adobe based their expression language on JavaScript, so writing and editing the code should also feel very familiar. Expressions are by no means a requirement for prototyping animations, but I’ve found them very helpful when trying to recreate effects like inertia or bouncing without specifying a bunch of additional keyframes.

Lots of expressions exist already, and have for years, but they aren’t always easy to find if you aren’t sure what you’re looking for. Dan Ebberts has done a great job explaining expressions and how to use them effectively, laying the ground work for what we’ll be going over today. His site, motionscript.com, has a lot of additional resources for those that want to dive even deeper into AE.

We’ll be working with a popular inertia expression today. It works by plugging variables for amplitude, frequency, and decay into a sine function that overshoots its target and “settles” on the final value. All you’ll need to do is copy and paste the following in the right place and edit the variables to change the look and feel of the animation.

nearestKeyIndex = 0;

if (numKeys > 0){
  nearestKeyIndex = nearestKey(time).index;

  if (key(nearestKeyIndex).time > time){
    nearestKeyIndex--;
  }
}

if (nearestKeyIndex == 0) {
  currentTime = 0;
} else {
  currentTime = time - key(nearestKeyIndex).time;
}

if (nearestKeyIndex > 0 && currentTime < 1) {
  calculatedVelocity = velocityAtTime(key(nearestKeyIndex).time - thisComp.frameDuration / 10);
  amplitude = 0.1;
  frequency = 2.0;
  decay = 4.0;
  value + calculatedVelocity * amplitude * Math.sin(frequency * currentTime * 2 * Math.PI) / Math.exp(decay * currentTime);
} else {
  value;
}

Adding expressions in After Effects

I’ve set up a simple two-keyframe animation where the position is animated, similar to those in our Accessible After Effects post. Once you’ve created your keyframes, it’s very simple to add an expression. First, right-click on the final keyframe, open the Keyframe Assistant menu, and choose Easy Ease Out. This last step is optional, but specifying Easy Ease Out will slow the animation down towards the end, which works well with the inertia expression we’re working with.

Keyframe Assistant

Next, hold ‘alt’ and left click the stop watch icon next to “Position”. This should enable expressions on this layer and open a textarea-style field where you’ll put the expression.

Expression Input

Go ahead and copy and paste the above script, replacing what’s already in the expression input. Playing the animation should look something like the following:

Feel free to add motion blur at this point, too. It’s a toggle on each layer, just click the box below the motion blur icon to enable it for that layer. It simply smoothes out the animation and makes it look less jarring.

Motion Blur

Now you’re free to jump into the code to change how your animation looks. Below is a reference for what changing the different variables will do to your animation, with the other variables left the same.

As you can see, you can add a lot of life to your animations with just two keyframes and an expression. We also only animated the position property here, but you can add this same expression to other properties as well. With just a few additional keyframes and a few tweaks to the variables for each property, the animation really begins to come to life.

How do you use expressions in After Effects? Do you have any favorites? Leave a comment or let me know on twitter.