## Feb 20, 2011

### Understanding Expression in After Effects

Hello everyone. Here's another After Effects post on "Understanding Expressions". Here, I'm going to be explaining a few things about expressions and using them.

Before I start, I'd like to explain some words I'll be using:
Property refers to something that defines a layer, like opacity, scale, position, rotation, orientation etc.
Value is basically the value of the property.

If you don't know yet, expressions are like formulae that calculate what the value of a property is going to be. They work like formula in Microsoft Excel. You can do all the basic operations (add, subtract etc) and work with some functions (sin, cos, tan etc) and also refer the expression to another property, like, for example, if you were to link the Scale of one layer to the Scale of another.

However, expressions aren't there so you can go in and just type "100" to set the property. They're better at linking properties to one another and especially adding some sort of variable that will change this layer in a certain effect compared to another.
Simply put, it's the way I can tell layer 1 to always be half the opacity of layer 2. This is a pretty simple expression that would look something like this:

thisComp.layer("layer 2").opacity/2

Now, to a beginner the above probably doesn't explain exactly what's happening, but I'll get into a breakdown of how the above code works at some point (not in this post, though)
Before we get into that, I'd like to point out the most important tool when it comes to writing expressions....

THE PICK WHIP
First off, to apply an expression, simply ALT+CLICK on the stopwatch. The expression box appears and you can type all the stuff you want. Usually it comes with a short statement describing the current property.
So, in the case of Scale, you'd get "transform.scale". This, however, also means the same if you just typed "scale" But that's not the point.

The pick whip is the swirly button 3rd from the right. It's a great tool because it saves you having to do too much typing.
To use the pick whip, simply click and drag onto a property that you'd like to link this property to. So, if I had two layers and I pick whipped the position of the top to the position of the bottom, it'll automatically write for me this expression.

Even better, if I was to take a property with 2 values (like 2D position) and pick whip a property with 1 value (like opacity), the whip will automatically create an expression where both values will be represented by the one:

temp = thisComp.layer("Solid 2").transform.opacity;
[temp, temp]

A variable named "temp" is created with the value being the opacity, and then an array is created with both values as temp. I talked about variables in this post, so check that out if you don't understand.
Another interesting thing that happens is that if you're already working on the expression, pick whipping will just add the string referring to that property to where the cursor is:

For example, if I was working in the position expression and had written this:

Xposition=
Yposition=
[Xposition,Yposition]

I can then set the cursor at the end of the first line and pick-whip what I want X position to be. So If I pick whipped opacity for the current layer, it'd write this:

Xposition=transform.opacity
Yposition=
[Xposition,Yposition]

If I moved down to line 2 and pick whipped the opacity of a different layer, this is what would happen:

Xposition=transform.opacity
Yposition=thisComp.layer("Solid 1").opacity
[Xposition,Yposition]

In this case, it's added the complicated referencing for me. Same goes for if I decided to use the expressions menu to insert a control. This would be the last button on the right.

Now that we know we can get away with pick whipping to refer to stuff, we don't have to worry about how to write the thisComp.layer stuff! :-D

ARRAYS
An array is... well, it's best to use an example to explain an array.
What we had above (with the temp and Xposition and Yposition) is a good example of an array. It's those parameters we describe using [square brackets].
Some properties in AE have various dimensions. 2D position has 2 values/dimensions (X,Y), 3D position has 3 (X,Y,Z). Colors have 4 (red,green,blue and transparency aka alpha), scale has 2 values (or 3 if layer is 3D) while opacity has 1.
So, arrays are used to define which dimension we'd be referring to.
When we start out in a 720 x 480 document, we have position of a new solid as 360 and 240 respectively. As an array, this would be [360,240].
So, what if we wanted to add 10 pixels to the Y position only?
If we went and typed "transform.position + 10", the result would be [370,240], because AE will assume you're doing this to the first dimension. We're not, so what do we do?

We'd simply tweak the expression so it looks like this: transform.position + [0,10]

This means that the it'd add 0 to X and 10 to Y. See what's going on?
Same goes for multiplication and any function and operation.

But, what if we wanted to REFER to a certain value in an array? What if I wanted to refer to the Z position only?
Well, this is where things get a little confusing if you're not careful. Array values look like this (in 3 dimensions):
[0,1,2]
Basically, X would be 0, Y would be 1, Z would be 2.

So, here's how we'd refer to these:
X = transform.position
Y = transform.position
Z = transform.position

The fact that the first is 0 tends to confuse me. That's how it works, though. So if you want a specific operation to refer only to a specific dimension, this is how you would write it. So if you wanted the opacity to be equal to half the Z position, you'd write it like this:

transform.position *.5

or /2 depending on what you prefer.
This is how arrays work. Arrays tend to become quite confusing but once you get the hang of it, it's very powerful.
I'd like to point out that wiggle doesn't work inside an array. You can't write this:

[value,wiggle(8,10)]

because the wiggle would produce an array of two dimensions, while in the above scenario it should produce one value to complete the array. Think of it like producing this at a particular frame:
[120,[10,20]]

AE would be like "What the heck is this?"
I don't know exactly how to fix this, but I usually just create a new slider control, apply the wiggle to the slider then pick-whip the slider to the array:

[value,effect("Wiggle")("Slider Control")]

If you know how to fix this problem in a different way, please drop a comment below.

WHEN TO USE AND NOT TO USE EXPRESSIONS
Expressions are useful, very useful, but they can sometimes be a nuisance. Have you ever tried pressing U on a layer which has expressions on most of its properties? The buggas fill up the entire timeline window and you can't get anything done.
So, please, only use expressions when you have to. If you can achieve an effect with keyframes, by all means do so. If you can't, use expressions. Remember, you can go Animation > Keyframe Assistant > Convert Expression to Keyframes to do exactly that.
Expressions can be slow to calculate sometimes, so keep that in mind when working with them.

Enjoy!

1. 2. 