Hello, this is Mathias Möhl for mamoworld.com
and welcome to the third class of our After Effects expressions course. I named today’s
class “Expressions as a pocket calculator”, since this is the best way to think about
expressions when you want to get started writing your own expressions code. Let’s write some expressions to place those
dots here. The first thing I do is to right-click on the position and choose separate dimensions,
such that X and Y position are two independent properties. Because then we can apply an expression
to only the X or only the Y position, such that the expression only needs to describe
a single number. Expressions for 2D or 3D position values are slightly more complicated,
therefore we will cover them in a later class. Let’s place this dot here exactly in the
middle of the comp. As usual, we alt-click on the top watch to activate expressions for
this property. Since our comp is 1920 pixels wide, the middle is at 1920/2. As you can
see, you can simply enter this as an expression and the layer is placed accordingly.
Let’s place the second layer 300 pixels left of the center. So we simply enter 1920/2
– 300 to create the 300 pixels offset. You can also put parenthesis around the 1920/2
to make clear that the 300 is subtracted after the division is calculated, although this
is not necessary here, since multiplication and division are always calculated first and
then addition and subtraction. This third dot has a keyframed animation.
We can disable it temporarily by entering the most simple expression ever: a simple
number. Let’s enter 170 and as you can see, the layer is not animating anymore but stays
at this value although the keyframes are still present. By turning the expression on and
of using this equality symbol, we can toggle between the expression and the keyframe value.
Adding the expression also kind of locks the value. So when we move the layer in the viewer,
you can see it only moves left and right but not up and down anymore. Note that the keyframes
are still changed when moving the layer but the layer does not move up or down because
the y value is controlled by the expression and not by those keyframes.. In the next example, we want to place this
“plus” symbol here in the middle of the two other symbols. The left one has a x position
of 500, the right one of 1500. So the middle is (500+1500)/2 which is 1000. Now instead of hardcoding the numbers, we
can also use the pick-whip to link to the positions of the other layer dynamically.
So we select the “500” and then pick-whip to the 500 here on the position of the first
point. As you can see, the 500 has now been replaced by some code dynamically linking
to the position of point 1. The code looks a bit complicated, but is actually
not too hard to read. It says “from this comp, from layer “point 1” please use
in the transform section the property xPosition. Now we also select the 1500, and pick-whip
it to the position of the second point. To make it a bit more interesting and complicated,
for the second point I didn’t separate the dimensions. So it does not have an X Position
value to link to, but only a Position value including both X and Y.
If we try to pick-whip to this property, we get an error saying “expression result must
be of dimension 1 not 2” which means that we need to have a single number here but this
position is actually two numbers, namely x and y. To fix this, we first undo, then select
the 1500 again and this time don’t link it to the entire property, but drag the pick-whip
directly to only the x-value here. Now the generated expressions code looks a little
bit different. This zero in square brackets at the end says that we don’t want to use
the entire position value, but only the first component, namely the x component of it. One
thing you need to get used to is that programmers start counting with 0, so 0 is the x component
and 1 is the y component. But we will cover this in more detail in a later class. We can move the two points freely now and
due to the expression the plus symbol always stays in the center. Of course, currently
it only stays centered in x direction. If you need it in both directions, you could
of course apply an expression to the yPosition in the same way. So far we learned that expressions are like
little pocket calculator formulas with the super cool extra feature that you cannot just
use fixed numbers in the formulas, but also link to other properties with the pick-whip.
The code snippet you create with the pick-whip is called a variable. And in addition to the
variables you create with the pick-whip, there are lots of other predefined variables that
you can use. One of the most useful ones is called “value”.
Value gives you the keyframe value at the current time of the property that the expression
is applied to. This allows you do let the expression calculate something relative to
this keyframed value. Here, we’ve got this keyframed bouncing
ball, for example. If we enter on the yPosition the expression “value + 100”, you can
see that this offsets the motion path by 100 pixels, since at each frame it adds 100 to
the keyframed motion path value. If we enter “value * 2” you can see that
the bounces are now twice as high. maybe let’s combine this with an offset
of -80 such that it ends up again on the correct height. So with simple multiplication and
addition you can scale and offset a motion path. And all of this just with the help of
the value variable. Another example for a useful variable that
you cannot access with the pick-whip is “thisComp.width”. As you might expect it represents the width
of the composition. So if we enter “thisComp.width” as expression for the xPosition, the xPosition
becomes 1920 because this is the width of the comp. To center it, we can use thisComp.width/2.
Like all build-in expressions variables, this value updates dynamically. So if we cut and
paste the layer to a new composition of a different size, it updates automatically. Ok, so now you know the value variable and
the thisComp.width variable, and there are lots of other ones. But do you have to learn
all of the by heart? No, the great thing is that you find all of them in the expressions
language menu here at this icon that looks kind of like a play button. To use this menu,
you need to understand that variables have a hierarchical structure.
So in the “Global” section, for example, you find thisComp, but you don’t find “thisComp.width”.
But in the “Comp” section we find all properties that a composition has. Here you
also find “width”. The different parts of a variable name are always connected with
a dot, so you know that with “thisComp.width” you can access the width of the current composition.
If you want to know the width of any other composition, in the global section you can
see that you can also get comps by name. So with “comp” and then in parenthesis the
name of the composition, you can access any comp in your project. Actually, you can also
get such a comp with the pick-whip, by pick-whipping to a comp in the project panel. As you can
see that created exactly what we’ve just found in the expressions menu. “Comp”
and in parenthesis the name of the composition. Note that those names always must be put in
quotes. So to access the width of this other comp,
we now can add .width. Let’s do one more practical example. Let’s
say this text should always stay right of the dot, even if we replace the dot with a
bigger or smaller image. So we could say “please use as my x position my keyframed value plus
the width of the dot layer”. So as expression we type “value” to get the keyframed position
and then “+” and now we need to get the width of the dot layer. So we use the pick-whip
and drag it onto this layer which gives us “thisComp.layer(“dot”)”. Now we need
to find the width of the layer. So in the expressions menu we look in the layer category
what properties all layers have. In the “General” section you find “width” so we know that
with “.width” we can access the width of this layer”. Now the text jumped to the
right of the dot. If we replace the footage of the dot layer with this arrow here, as
you can see, the text moves to adjust to the new size. Summary!
Today you learned that expressions are like a pocket calculator. That you can enter formulas
and in those formulas you can access almost everything in your project using variables.
Those variables have a hierarchical structure, so you have, for example, variables for each
comp and each layer, and by connecting everything with dots you can dive into those variables
and access any properties like the width of the comp, or layer, for example. Finally,
the pick-whip is a tool to generate variables for properties quickly without the need to
type everything manually. And it cannot only be used to link to properties, but can also
link to layers, comps or other project items. Next time we look at a very practical example
and use everything you’ve learned today to let the wheels of a car rotate fully automatically. Again, this is Mathias Möhl for mamoworld.com
– see you next time.