1. What Expressions Are
The After Effects expression language is a powerful
set of tools with which you can control the behavior of a layer’s
properties. Expressions can range in complexity from ridiculously simple
to mind-numbingly complicated. At the simple end of the spectrum, you
can use expressions to link one property to another or to set a property
to a static value. At the other extreme, you can create complex linkages, manipulate time, perform calculations in 3D space, set up tricky procedural animations, and more.
Sometimes you’ll use expressions instead of keyframes
(most properties that can be keyframed can be controlled by
expressions). In other cases you’ll use expressions to augment the
keyframed behavior. For example, you could use keyframes to move a layer
along a specific path and then add an expression to add some randomness
to the motion.
Close-up: Expressions Have Limitations
Although the After Effects
expression language presents you with an impressive arsenal of powerful
tools, it’s important to understand the limitations of expressions so
that you can avoid making assumptions that lead you astray. An
expression may generally be applied only to a property that can be
keyframed, and it can affect only the value of that property. That is,
an expression can affect one and only one thing: the value of the
property to which it is applied. This means there are no global
variables. This also means that although an expression has access to
many composition and layer attributes (layer width and height, for
example) as well as the values of other properties, it can only read,
not change, them. Expressions can’t
create objects. For example, an expression cannot spawn a new layer, add
an effect, create a paint stroke, change a blend mode—the list goes on
and on. Remember, if you can’t keyframe it, you can’t create an
expression for it. Expressions can’t access information about individual mask vertices. Expressions
can’t access text layer formatting attributes, such as font face, font
size, leading, or even the height and width of the text itself. Expressions
cannot access values they created on previous frames, which means
expressions have no memory. If you’ve had a little Flash programming
experience, you might expect to be able to increment a value at each
frame. Nope. Even though you can access previous values of the property
using valueAtTime(), what you get is the pre-expression value
(the static value of the property plus the effect of any keyframes).
It’s as if the expression didn’t exist. There is no way for an
expression to communicate with itself from one frame to the next. Note,
however, just to make things more confusing, the postexpression value of
a property is available to any other expression, just not the one applied to that property. In fact, the postexpression value is the only
value available to expressions applied to other properties. To
summarize: An expression has access only to the pre-expression value of
the property to which it is applied, and it only has access to the
postexpression values for other properties with expressions. It’s
confusing at first, but it sinks in eventually.
|
2. Creating Expressions
The easiest way to create an expression is to simply Alt-click (Opt-click)
the stopwatch of the property where you want the expression to go.
After Effects then creates a default expression, adds four new tool
icons, changes the color of the property value to red (indicating that
the value is determined by an expression), and leaves the expression
text highlighted for editing (Figure 1).
At this point you have a number of options. You can
simply start typing, and your text will replace the default expression.
Note that while you’re in edit mode, the Enter(Return) key moves you to a new line in the expression (this is how you can create multiline expressions) and leaves you in edit mode.
Another option while the text is highlighted is to
paste in the text of an expression that you have copied from a text
editor. This is the method I generally use if I’m working on a multiline
expression.
Instead of replacing all the default text by typing
or pasting, you can click somewhere in the highlighted text to create an
edit point for inserting additional text.
Alternatively, you can drag the expression’s pick
whip to another property or object (the target can even be in another
composition), and After Effects will insert the appropriate text when
you let go. Note that if an object or property can be referenced using
the pick whip, a rounded rectangle appears around the name as you drag
the pick whip over it. If this doesn’t happen, you won’t be able to pick
whip it.
Finally, you can also use the Expression Language menu to insert various language elements.
After creating your expression, exit edit mode by clicking somewhere else in the timeline or pressing Enter
on the numeric keypad. If your expression text contains an error, After
Effects displays an error message, disables the expression, and
displays a little yellow warning icon (Figure 2). You can temporarily disable an expression by clicking on the enable/disable toggle.
Working with existing expressions is as easy as creating them. Some common operations include
editing.
Click in the expression text area to select the entire expression; you
now have the same options as when creating a new expression. If your
expression consists of multiple lines, you may need to expand the
expression editing area to be able to see all (or at least more) of it
by positioning the cursor over the line below the expression text until
you see a double-ended arrow and then clicking and dragging.
deleting. Simply Alt-click (Opt-click) the property’s stopwatch, or you can delete all the text for the expression and press Enter on the numeric keypad.
exposing. Select a layer in the Timeline and press EE to expose any expressions applied to that layer.
copying.
In the Timeline panel, select a layer property containing an expression
and choose Edit > Copy Expression Only to copy just the property’s
expression. You now can select as many other layers as you’d like and
Edit > Paste to paste the expression into the appropriate property of
the other layers.