1. The Language of Expressions
The
After Effects expression language is based on a subset of JavaScript.
JavaScript is a scripting language used largely for Web page design and
includes many features specifically aimed at that task. The JavaScript
implementation for expressions includes the core features only. That
means there’s a lot about JavaScript that you won’t need to know, but it
also means that any JavaScript reference you pick up (and you’re going
to need one if you really want to master expressions) is going to have a
lot of content that will be of little or no use to you.
The rest of the expression language consists of
extensions that Adobe has added specifically for After Effects. This
means that in addition to a good JavaScript reference, you’ll also be
frequenting Adobe’s After Effects Expression Element Reference.
The most up-to-date version of this reference can be found at Adobe’s
Help on the Web. The After Effects Help menu will take you there: Help
> After Effects Help, or you can go to www.adobe.com/support/aftereffects.
2. Linking an Effect Parameter to a Property
Here’s the scenario: You want to link an
effect to an audio track. Specifically, you want to link the Field of
View (FOV) parameter of the Optics Compensation effect to the amplitude
of an audio layer. Expressions can’t access audio levels directly, so
first you have to use a keyframe assistant (Animation > Keyframe
Assistant > Convert Audio to Keyframes) to create a null layer named
Audio Amplitude with Slider Controls keyframed for the audio levels of
the Left, Right, and Both channels (for a stereo source). Next, you just Alt-click (Opt-click)
the stopwatch for the FOV parameter of the Optics Compensation effect
and drag the pick whip to the Both Channels Slider property of the Audio
Amplitude layer (Figure 1). Doing so generates this expression:
thisComp.layer("Audio Amplitude").effect("Both
Channels")("Slider")
Take a closer look at its syntax: From JavaScript,
the After Effects expression language inherits a left-to-right “dot”
notation used to separate objects and attributes in a hierarchy. If your
expression references a property in a different layer, you first have
to identify the composition. You can use thisComp if the other layer happens to be in the same composition (as in this example). Otherwise, you would use comp("other comp name"), with the other composition name in quotes. Next you identify the layer using layer("layer name") and finally, the property, such as effect("effect name")("property name") or possibly transform.rotation.
In addition to objects and properties, the dot
notation hierarchy can include references to an object’s attributes and
methods. An attribute is just what you would guess: a property of an
object, such as a layer’s height or a composition’s duration. In fact,
in JavaScript documentation, attributes are actually referred to as
properties, but in order to avoid confusion with the layer properties
such as Position and Rotation (which existed long before expressions
came along), in After Effects documentation (and here) they’re referred
to as attributes. For example, each layer has a height attribute that
can be referenced this way:
comp("Comp 1").layer("Layer 1").height
Methods
are a little harder to grasp. Just think of them as actions or
functions associated with an object. You can tell the difference between
attributes and methods by the parentheses that follow a method. The
parentheses may enclose some comma-separated parameters.
It’s important to note that you don’t have to specify
the full path in the dot notation hierarchy if you’re referencing
attributes or properties of the layer where the expression resides. If
you leave out the comp and layer references, After Effects assumes you
mean the layer with the expression. So, for example, if you specify only
width, After Effects assumes you mean the width of the layer, not the width of the composition.
Let’s forge ahead. You linked the amplitude of your
audio layer to your effect parameter, but suppose you want to increase
the effect that the audio level has on the parameter. You can use a
little JavaScript math to multiply the value by some amount, like this
thisComp.layer("Audio Amplitude").effect("Both
Channels")("Slider") * 3