One of the most significant features in After Effects is an easy-to-use scripting language called Expressions.
By Chris and Trish Meyer | July 09, 2001
One common use of expressions is to set up automatic relationships between layers, such as having a minute hand rotate 60 times a fast as the hour hand, and the second hand rotate 60 times as fast as the minute hand. Rather than have to keyframe - and edit - three layers, you can keyframe just one layer and have the others follow automatically.
In a recent column we discussed the Parenting feature that was originally introduced in After Effects version 5. Parenting is an excellent way to group objects together, or to have one already-animating object also follow another.
Expressions offer a different approach to making one object do what another does, yielding even more control. Many right-brain artists are scared off by expressions, as they do involve math and programming (very left-brain activities), but learning just the most basic form of expressions will help alleviate a lot of tedium while working on a project - they're like having a sharp, unpaid intern!
An Expression of Value
If you wanted to animate something in After Effects - be it the position of a layer, or a parameter of an effect - you had to use keyframes. A keyframe states "this is the value of this parameter at this point in time."
It is useful to compare and contrast Expressions to Parenting and Precomposing. Parenting imposes the transformations applied to one layer (scale, position, rotation) onto another layer in the same comp, all in one step. However, Parenting does not take into account transparency, nor any effects. Precomposing allows you to alter opacity and apply effects to a nested (grouped) set of layers, but the problem is that those layers are in their own composition - not the one you're currently working in. This can slow you down when trying to tweak the individual elements of a complex animation.
The coolest thing about Expressions is that they are always "live": If you created an expression so one layer's rotation followed another layer's rotation, as soon as you change the master's rotation, your slaved layer would automatically follow along with no keyframes to delete and re-build.
The second coolest thing is that Expressions are very selective: You can apply them to any single parameter you choose.
Third is that an Expression can reference virtually any other parameter you want in an entire project: an identical value from another layer, a completely different value from another layer or effect, and even a parameter from a layer in an entirely different composition in the same project.
Expressions are easy to create and apply. In many cases, After Effects will write the Expression automatically; you literally just have to point at what you want it to do.
Say you wanted one layer to rotate the same as another layer. The first step is to expose the Rotation parameters for both layers in the Timeline panel. Then select the Rotation parameter for the layer to be slaved ("expressed"), and select the menu item Animation > Add Expression or Option+click (Alt+click under Windows) on its animation stopwatch. A new line will appear underneath that says "Expression: Rotation", and next to it in the timeline will appear the phrase transform.rotation (see below). This is the world's simplest (and perhaps least useful) Expression: "my rotation equals my rotation".
When an expression is first applied, it defaults to following itself (in this case it says "my rotation equals my rotation") until you use the pickwhip or otherwise edit the script.
There are two additional clues that an expression is now in force for this parameter:
- The parameter's value will change color (in After Effects CS3, from blue to red).
- There will be a button with an equals sign; this indicates "this parameter equals the result of an expression."
To change this Expression to mean "my rotation equals that other layer's rotation", click on the pickwhip tool that appears on the Expression line - it looks like a small spiral. Drag this pickwhip to the Rotation value for your master layer.
To assign an expression, select the pickwhip and drag it to the master value (top), release the pickwhip to see the new script (above), and press Return to accept it. You can also edit the script before hitting Return to add additional code or simple mathematical instructions such as +, -, *, /.
Let go, and a new Expression will be written:
thisComp.layer("master layer's name").transform.rotation
Press Enter to tell After Effects to accept the new Expression. If you were to now edit or keyframe the master layer's rotation, the newly-expressed layer will obediently follow.
It's possible for a non-programmer to read an Expression; just look for the periods to help break it into understandable chunks. It also sometimes helps to read it backwards. This particular script says: "I equal the transform parameter rotation, of a layer called "master" (or whatever that layer's name is), that resides in this comp."
It's almost as easy to modify an expression as it is to have After Effects write one for you; elementary math is often all that is required. Say you want this slaved layer to rotate twice as fast as the master layer. All you have to do is select the Expression, move the cursor to the end, type * 2 and press Enter. If you want it to rotate one and a half times as fast, but in the opposite direction, type * -1.5.
A number of applications should immediately spring to mind: on a clock, the second hand would rotate 60 times as fast as the minute hand; the minute hand would rotate 12 times as fast as the hour hand.
next page: some advanced tips
Get articles like this in your inbox: Sign Up