Expressions: The Basics
A Short Introduction
After Effects allows you to perform amazing tasks on objects within its timeline. In addition to normal timeline-style animation using keyframes and tweening, After Effects supports a feature called expressions.
An expression in After Effects is a script used to control parameters of objects in the timeline. Using this control, you can modify an existing parameter a single time or drive the parameter completely from the expression. The only objects you cannot modify using expressions are Mask parameters, such as Shape, Feather, and so on.
After Effects scripting requires a basic knowledge of the elements of programming. You should at least know what variables are and how they are used, and the basics of control structure and program flow. This sounds intimidating, but if you’ve ever used Javascript you should be able to easily grasp expressions.
Creating an Expression
We will begin by creating a simple expression in the opacity
parameter.
In your project, expand the parameters for the object you want to add an expression to (in this case, opacity
). Hold down , and click the stopwatch next to the opacity
parameter. The parameter itself will expand with a new sub-item labeled Expression: Opacity. In the timeline, the text transform.opacity
should be visible.
You can replace the transform.opacity
text with any number to instantly set the opacity of your object. If you enter the number 20, your object will be at 20% opacity. You can also use mathematical expressions, such as 5+5
, 2*10
, or even (13+4)*18-12.5
. You can (and probably will) use even more advanced math using trigonometric functions and other operators.
Using After Effects Variables
After Effects expressions have a number of global variables available to all expressions. Like other languages, these variables should be considered reserved words and you should not use the same names for your variables.
One of the most important variables in dealing with animation is time
. The time
variable is equal to the current time of the current composition’s playback head, represented in seconds. As the playback head moves from beginning to end, it begins at 0 and increments by 1 for every second that passes.
If we now replace the transform.opacity
code with the following,
time
the opacity of our object will slowly increase as the playback head is moved toward the end of the timeline. It goes pretty slowly (1% opacity change per second), so let’s multiply it by a larger number:
time * 5
You’ll now see that it’s noticeably faster. If your timeline is long enough or you multiply time
by a larger number, you’ll notice the opacity stops changing once it reaches 100%. After Effects will automatically clamp values outside of their proper range, so for a percentage, any number greater than 100 will be 100, and any number lower than 0 will be treated as 0.
Using this knowledge, we can add a short delay to our opacity animation. If we alter our expression to this,
time - 5
our animation won’t start until 5 seconds down the timeline. At the beginning of the timeline, using this expression as a calculation, time
starts at zero. This is handy, but let’s try and combine it with our sped-up expression:
(time * 5) - 5
What’s wrong here? If you run this, you’ll see that it starts at one second instead of five. Why is this? Well, if you remember algebra at all, you’ll know you can’t just multiply single parts of the equation without doing the rest.
We’ll multiply both numbers by 5 this time (our “speed-up” factor), and this is how After Effects will arrive at the opacity:
(time * 5) - (5 * 5) = (5 * 5) - 25 = 25 - 25 = 0
If time is 1:
(time * 5) - (5 * 5) = (1 * 5) - 25 = 5 - 25 = -20
Since -20 isn’t a valid value for opacity
and is below zero, After Effects will clamp it to 0 since it’w within our 5-second delay.
This tutorial will continue at the fantastic Part 2!