After Effects Expressions Simplified
COW Library : Adobe After Effects Expressions Tutorials : Andrew Kramer : After Effects Expressions Simplified
Using expressions, anyone can link filters and transformations, as well as many other parameters, to drive powerful new effects and streamline job processes. The operative word here is `anyone' - and in this case, I do mean just about anyone. Don't let the idea of expressions scare you.
Admittedly, After Effects expressions can be complicated but for the everyday After Effects user, powerful expressions are well within reach and they are easier than you might think. A simple example of using expressions lays in the pickwhip function.
The pick-whip allows users to easily create expressions that can accomplish techniques which would take a lot more work using After Effects' other methods.
Suppose I'm doing some motion graphics and I want 20 pictures to all be tinted a certain color. Easy enough, I can tint one and then copy and paste the effect to each of the 19 other photos. But if you're like me, you are constantly making changes and don't want to keep copy and pasting all of these changes so that your images look consistent. To solve this problem, we're going to use expressions to link parameters together without any actual scripting, just by using the pickwhip.
A SIMPLE EXPRESSION:
Here's how to control the tint color for multiple layers at once:
1. Add the tint effect to your first layer or your `control' layer.
Expressions like this are great for keeping everything in sync and making multiple changes at once. Using the pick whip is a great way to link similar effects. But what about linking different effects together?
Let's study the original example, linking the blur amount to the opacity to create a smooth fade-in or out; essentially we want to change the opacity of a layer and drive the blur amount at the same time.
1. Add the fast blur effect to your layer
2. Alt-click the stopwatch for "blurriness"
Now a month ago if I had seen this expression I would have thrown my hands up and just made extra keyframes. But let me explain the last line in a way that has helped me to understand expressions.
Inside the parenthesis are five values separated by commas. The first is the specific property; After Effects has to know what is to be considered - in this case, the transform.opacity. Remember you can pick-whip any parameter to automatically type a parameter's property name.
The next two values indicate the parameter range to be considered, in this case the opacity amount from 0-100.
The last two are what values are to be executed - or in this case the blur amount. In other words the opacity of 0-100 generates the values for the blur of 50-0. Why 50-0 and not 0-50?
When the opacity is at 100 percent, we want the blurriness to be 0 and when the layer is completely transparent we want it to be blurred by 50. So if I wanted more blur I might use this expression...
...increasing the max blur to 200 or dropping it to as little as 20.
`Linear' is a common method in which to remap these values across time so that the change is gradual but direct. Using this method you are able to link non-uniform values like the opacity with a layer's rotation, where rotation values span from 0-360 degrees compared to opacity which is 0-100.
If you were to use an expression on the rotation it might look something like this:
Using this expression, as the layer fades out, it will also rotate 360 degrees.
I remember back in high school when I had to figure out what `y' equaled if y=x+4 and x=6. Well, for those of you that have been out of school for a while, y=10. If I remember correctly, they referred to these as "variables." One way to think of a variable is a value that is "to-be-determined." In other words, a variable is a dynamic value or number based on specified or notyet- specified values. Once all the non-specified values are determined, the variables value is generated and integrated into the expression or simply executed. I may not have paid much attention in math class but some of it has come back to me.
Using variables with expressions allows After Effect to create incredibly more effects.
AN EXAMPLE THAT MAKES USE OF VARIABLES
An example of using variables is found when creating a realistic camera shake. This effect, which we'll start and later modify with a variable, is best implemented through expressions. Possibly the most famous simple expression is:
When applied to the position of a layer, this expression will create random values - or in this case, random movement. In this example, wiggle is the function and A is the number of changes per second, with B representing the distance of change. wiggle(5,10) when applied to the position of layer would cause the position to change direction 5 times per second and move 10 pixels at a time.
The Wiggle function is a great way to add random movement to layers, creating more realistic animations - but what if I wanted to generate an earthquake that comes and goes gradually? Currently, this expression uses exact values that don't change but using a variable that is linked to a slider control we can keyframe this earthquake effect over time.
If you've tried to use an effect from the expression controls category, you may have noticed that nothing happens; you may have thought something was wrong and deleted it. In order to work properly, these expression controls must be linked to actual operators like position or opacity.
So let's get started, we want to create a camera shake on the position of a layer...
1. Alt-click on the stopwatch to set layers position
Notice the layer shakes a bit. Let's continue:
3. Add the effect "Slider Control" from the expression control category in the effects list
4. Rename the effect in the effects control to "shakeAmount"
5. Return to the position expression and change the number 10 to shakeAmount
It should now read:
ADDING THE VARIABLE
Now we need to define the variable so After Effects knows what is being referenced by the word "shakeAmount".
6. above the line with the wiggle script [or below] type:
7. Then with the blinking type cursor positioned after the `
8. The property info should print something like this:
9. Then add a semicolon to the end to define it as a variable.
Now if you change the value in the effects control to 25, you'll see the clip shake and the position value should show up in red (instead of blue), indicating an expression is driving the value. So if you haven't guessed, the shakeAmount variable is getting the value from the shakeAmount slider - and the shakeAmount variable is defining the wiggle amount.
Now you can easily keyframe the amount of shake using the slider control. You may also change the number 5 to create rapid or smoother movement. But when you're tired of going back into the expression editor to make changes to the frequency you may want to set up another variable.
Follow the steps above with a new Slider control named `frequency' and add it to the current expression to create this:
Now you can keyframe or adjust both expression sliders dynamically without editing code. But wait, there's more! You now have it shaking, but couldn't it also rotate?
Simply copy everything in the `position' expression and paste it into the rotation property - everything is already linked to the sliders, so we'll use the current variables to drive a rotation and the position.
Now you may notice that the rotation expression is creating relatively large values - turning a thing 15 or 20 degrees is jarring - but if you lower the shake amount to soften the rotation, the layer's position no longer shakes enough. If the shakeAmount slider is set to 20 we're probably getting rotation changes around 20 so we need to make this value lower but we don't want to make new sliders. Here is an easy solution; take the current expression and divide it by 10.
Copy expression from the position:
Paste to rotation and simply divide by ten by adding this:
Now when a value of 20 is created by the wiggle expression it is then divided by 10 and now this new value is executed so instead of rotating 20 degrees it only rotates 2.
Imagine linking multiple layers to one control layer so that you could globally make adjustments to entire compositions with variables and various expression controllers. Once you get beyond the basic fundamental uses of expressions, start thinking about creative ways to drive unique effects with math equations and random movement.
I've created some expression based effects that help to simplify creating light sabers as well as generating three dimensional falloff and even an accurate frame rate converter for PAL to NTSC conversions as well as 29.97FPS to 24FPS or vise versa.
For me it was Dan Ebberts on the Creative Cow forums that opened my eyes. I've always been a visual person and was told that you're either a creative person or a technical person. With Dan's help the concept of expressions is no more complicated than the things I already do. And while I'll never be an expert code writer I have found creative ways to use expressions to streamline unique effects. I've also learned one more thing, writing code is creativity squared.
If you need help with expressions, check out the Creative COW's After Effects Expressions forum, dedicated to those wanting to dig deeper into the power of After Effects expressions.
Andrew Kramer is a visual effects artist and filmmaker. He is one of the leaders you'll find in Creative COW's popular After Effects forum, as well as our Photoshop forum. As a Creative Cow contributing editor, his free tutorials have been downloaded hundreds of thousands of times and in them he has demonstrated popular effects like creating the Ipod Nano Light Streaks and Simulated 3D reflections in AE.
Find more great Creative COW Magazine articles by signing up for the complimentary Creative COW Magazine.