CMG Hidden Gems: Chapter 37 - Expressions
Another selection of "hidden gems" (and essential advice), this time from Chapter 37 of Creating Motion Graphics with After Effects.
By Chris and Trish Meyer | August 27, 2011
We're going through our book Creating Motion Graphics with After Effects 5th Edition (CMG5) and pulling out a few "hidden gems" from each chapter. These will include essential advice for new users, plus timesaving tips that experienced users may not be aware of.
Creating expressions - the ability to tell one parameter to follow another, stay at a constant value, or create new values as the result of ingenious calculations - is one of the most powerful features in After Effects. For many, it is also one of the most daunting:?To get the most out of it, you have to deal with math and what looks suspiciously like computer programming. But it's not that bad!?In this chapter of CMG5, we show you how to make expressions work for you with minimal effort; here are a few choice tips culled from that chapter.
Convert Audio to Keyframes
Continuing on from the audio theme of the last two installments of Hidden Gems, a popular trick is to make an animation seem to follow the beats in the music. There is not yet an expression that can read audio directly, but there is a keyframe assistant - Convert Audio to Keyframes - that can. Combine this with expressions, and many animation possibilities open up. (In the interest of full disclosure, third party solutions such as Trapcode SoundKeys are more powerful and our preferred choice - but we know that for many of you, what comes with AE is all you've got.)
To use this assistant, open a composition that features audio, and choose Animation > Keyframe Assistant > Convert Audio to Keyframes. There is no need to select a layer first; After Effects mixes the audio for all of the layers in the composition, and then creates keyframes based on this final mix. If you have more than one layer with audio in a comp, and don't want to convert the audio levels of all of them, turn off the Audio switch for the unwanted layers before running this assistant.
The result is raw keyframes with a potential value range of 0 to 100, depending on the amplitude of your audio from frame to frame. You may need to perform some simple math to convert them to the range required by the parameter you plan to hook up via expressions, or you can use one of our favorite expression methods: Linear Interpolation (discussed next).
Interpolating This to That
After Effects features a group of interpolation expression methods (a "method" refers to a bit of expression code that takes a set of values inside parentheses, and comes up with a new value for you). Not to be confused with keyframe interpolation, these methods make it easy to match one range of numbers to another. Take this expression:
linear(transform.rotation, -360, 360, 0, 100)
This says "take the rotation value, and as rotation varies between -360 and +360, output a number between 0 and 100." After Effects will then perform any scaling needed to match together the two value ranges. Obvious, you can replace any of the numbers or properties inside the parentheses with the values you need.
You can find the interpolation methods under the Expression Language Menu - the little arrow to the right of the expression pick whip:
We've found it handy to remember the basic format of this expression with the saying "as that parameter goes from A to B, I want to go from Y to Z". That reminds us what five values go inside the linear() method.
The Value of value
Quite often, you want to take a parameter's original value, and use an expression to add onto it or offset it. To do that, all you need to do is tack on the bit of code + value at the end. You can also use value to pass a parameter's current value - even if it's keyframed - into an expression for further calculation.
The code value also works with arrays. For example, sometimes you may want to wiggle only one dimension of position or scale - but wiggle normally randomizes every dimension in sight! Let's say you wanted to keep X - the first dimension (notated by  in expressions - the same, but wiggle the second dimension - Y () - by 100 pixels at a pace of 1 wiggle per second. One way to do that would be to enter the following code:
temp = wiggle(1,100);
The above says store off the two-dimensional wiggle result in a "variable" called temp, then use only the temp-saved wiggled value for Y, and the original underlying value for X.
Note that wiggle is the one expression where you don't need to do + value in order to have the expression add its results onto a parameter's underlying value. (By the way, if you're not already familiar with the wiggle expression, click here to watch an introductory video from our After Effects Apprentice video series.)
So what was that, three or four gems in one?
Saving Expressions as Animation Presets
Expressions cannot be saved by themselves. Instead, you will need to copy and paste them between After Effects and a text document, or import projects that contain expressions you want to reuse.
A better way to save expressions is to use Animation Presets. Expressions applied to effects - or ordinary parameters - can be saved along with the effects as Presets. Simply apply your expressions to effects or parameters, select the relevant effects or parameters in either the Timeline or Effect Controls panel, and use Animation > Save Animation Preset to store the result as a preset. Select a new layer and use Animation > Apply Animation Preset to recall the parameters effects with their settings, keyframes, and expressions intact. (Note that if these expressions referred to other layers or comps, then you will need to re-enter the new layer and comp names).
We particularly like to use Expression Controls and the Distort > Transform effect (which gives you a second set of Position, Scale, and Opacity controls for a layer) to write self-contained expression modules. The Transform effect is particularly useful for presets as it allows you to scale, rotate, move, and fade a layer without touching the layer's normal Transform properties. And then Expression Controls can provide a simplified "user interface" to a complex expression or effect.
What Were We Thinking?!?
We're a big fan of adding comments to our After Effects projects - whether it be to inform a client we hand the projects off to, or just to remind ourselves what the heck we were thinking at 3 AM - or three years after we thought the project was done. Therefore, we use layer and comp markers throughout a project, as well as the Comment column in the Project panel. We also like to add comments to our expressions.
Comments need to go at the end of an expression line. After the expression code (and if it's a multiline expression, the semicolon at the end of an intermediate line), type // (two slashes), then whatever comment you want. After Effects will not execute anything after // and before the next Return. When you enter a Return, this tells After Effects the line is over, and it can start executing the expression again from the start of the next line.
If you want to write a long comment that requires more than one line, or sneak a comment into the middle of a line, surround your comment with the characters /* and */. To wit:
// this is a comment
/* this also works
as a comment*/
Trish and Chris Meyer share seventeen-plus years of real-world film and video production experience inside their now-classic book Creating Motion Graphics with After Effects (CMG).
The 5th edition has been thoroughly revised to reflect the new features introduced in both After Effects CS4 and CS5 (click here for free bonus videos of features introduced in CS5.5). New chapters cover the new Roto Brush feature, as well as mocha and mocha shape. The 3D section has been expanded to include working with 3D effects such as Digieffects FreeForm plus workflows including Adobe Repouss©, Vanishing Point Exchange, and 3D model import using Adobe Photoshop Extended. The print version is also accompanied by a DVD that contains project files (CS5-only) and source materials for all the techniques demonstrated in the book, as well as over 160 pages of bonus chapters on subjects such as expressions, scripting, and effects.
We will be pulling a few "hidden gems" out of each chapter to share on ProVideoCoalition.com roughly every week. These will give you a taste for the multitude of time-saving tips, not-obvious features, little gotchas, and other insider knowledge you will find in CMG.
The content contained in our books, videos, blogs, and articles for other sites are all copyright Crish Design, except where otherwise attributed.
Get articles like this in your inbox: Sign Up