Tutorial

Tutorial: After Effects Expressions 101

by
Joey Korenman

In this tutorial we'll learn how to use expressions in After Effects.

Expressions are possibly the most powerful feature in After Effects, but a lot of animators don’t use them because they seem intimidating. It takes a little bit of time to really get the hang of using them, but it’s worth the time investment. Once you learn how to set up behaviors and controls without having to do tons of manual keyframing, you’ll be a much faster and more efficient motion graphics artist.

This lesson is a quick overview of how expressions work in After Effects. You’ll learn what expressions are, why you may want to use them, and how to apply them to properties in your comps. We’ll use a simple example, animating the hands on a clock, to work through the basics. Once you’ve watched this video, you should have enough knowledge to move on to some of the more advanced lessons that use expressions.

RESOURCES

Here are some amazing sites to check out that will help you get the hang of expressions.

-------------------------------------------------------------------------------------------------------------------------------------------

Tutorial Full Transcript Below 👇:

Joey Korenman (00:11):

Hey there, Joey here for school of motion. And in this lesson, we'll be taking a look at the basics of expressions in after effects. If you've never used expressions before, or even if you have, but maybe need a little refresher, this lesson will get you going in the right direction. We'll check out what expressions are, how to use them and why as a MoGraph artist, you need to know about them. Don't forget to sign up for a free student account so you can grab the project files and expressions from this lesson, as well as assets from any other lesson on the site. And now let's jump in.

Joey Korenman (00:42):

So if you look at this comp here, uh, you'll see that I've set up a really, really simple scene, um, with a background in a clock and I've, uh, already done the Photoshop work of separating out the hour and minute hands of the clock. Um, and I've also set the anchor point appropriately, um, to the center of rotation of the two hands. Um, so if I rotate them in at hand, you'll see that it rotates exactly as it should. So, uh, if you were asked to animate a scene that had a clock in it, um, there's an easy way to do it. Um, but there's some pitfalls with that easy way, and I'm gonna show you that right now. So obviously the easiest way would be to set a key frame on the rotation property at the beginning. So let's do that and then to go forward in the comp and change the value.

Joey Korenman (01:36):

So now you have two key frames. All right. So you could see that this works. Um, however let's say that, uh, you want a very exact speed for, for the, you know, for the amount of rotation of the minute hand, let's say that you want, you know, one rotation every two and a half seconds or something like that. Well, now it gets a little bit trickier because now you have to say, okay, well, how long is my comp? Well, this comp is two minutes long. If I'm having one rotation every two and a half seconds, then what does this value need to be? And you have to do math and no one likes to do math. So, uh, when you're using key frames, it's good. If you're doing short little bursts of animation. Um, but when you're doing things like this, where you need a constant motion throughout the length of your comp expressions are a much better way to do it.

Joey Korenman (02:35):

Um, so I'm just going to use this as an example of how to apply an expression and sort of how you can work with expressions in and effects. So let's get rid of this key frame and let's zero this back out. So the first thing we want to do is actually enable an expression on this layer. All right, and expressions are applied to key frameable properties like position, scale, rotation, transparency, or if you had an effect on this layer, you could apply expressions to the properties of the effect. So you can use them a lot of ways. So the, uh, one way you can apply an expression is to click on the property that you want to add the expression to go to the animation menu and hit, add expression. All right. Um, I'm a big fan of hockey's and whenever possible, I'm going to try and teach you guys some really useful hotkeys, because if you're trying to freelance or impress your employer, or just get a job, one of the best things that you can do as a motion graphics artist is get quicker and be able to work faster and build things faster.

Joey Korenman (03:44):

And hotkeys are probably the number one way to do that because when you get the hang of them, you'll find that you start doing them without even thinking about it. And you can work almost as fast as you can think. And that's really important. So the way I add expressions is I hold the option key. And by the way, I'm on a Mac on I'm on a PC, I would imagine that's the alt key. So you hold option and you click rotation. And now we've enabled an expression for that. You can see the value of rotation turned red, and we've got this box over here where we can type our expression in. Now, the fastest way to use expressions is to kind of learn the four or five or six that you use most often and just, just kinda memorize them. And it's not as hard as it sounds after you use an expression two or three times.

Joey Korenman (04:34):

You'll probably remember it, but if you forget, um, or if you're just new to expressions and you're not even sure what expressions are out there, this little arrow can be your buddy, because you can click this arrow. And, uh, after effects has a built in dictionary of all of the different expression, variables and functions that you can use, and it even gives you hints about how to use them. Uh, you'll see, some of these functions have, um, have extra kind of options that are inside of parentheses. And that tells you that if you use, for example, this random function, which returns a random number, you need to add a parentheses and put the minimum value or array, comma, and then the maximum value or array. So you may not totally understand that yet at this point, but, um, just bear with me, this all makes sense.

Joey Korenman (05:28):

Um, just know that once you start using expressions, if you suspect that an expression exists to do what you want, you can look through this menu and you might just find it. Um, so what we want to do is, uh, is find a way to have the rotation increase at a constant rate throughout the comp. All right, now, the best trick I know to do that is to tie the rotation to something that's already naturally moving, you know, at a constant rate. And that would be the time of the comp. All right. And you can find the, uh, the variable for the comp time in the global section here, it's just time, or we can type it in this. One's pretty easy to remember. So we type in time, um, and a good habit to get into is adding a semi-colon at the end of all of your expressions.

Joey Korenman (06:21):

If you have a one-line expression like time, you technically don't need to use the semi-colon, but once you get into multiline expressions, after effects will require every line to have a semi-colon, otherwise it sort of doesn't know where one line ends and the next one begins. Um, when you're editing your expressions to commit them, uh, on the Mac, you can hold command and hit return, or you can hit the return that's on the number pad, um, and on a PC, it would be whatever the equivalent key stroke is for command it's probably control. Um, so now as we scrub through this, you'll see that the minute hand is moving at a constant speed. It's moving very slowly, but it's moving, you know, at the same speed, the entire length of the comp. And this is great because if all of the sudden I needed to make this cop 10 minutes long, I wouldn't have to change anything.

Joey Korenman (07:12):

It would still be animating at that same speed. Um, now the reason it's going so slow is because this time variable, uh, it returns the comp time in seconds. So if we go to five seconds, for example, you'll see that, uh, the rotation is set to five. Um, and the reason that I know this is because I have used the expression reference, uh, many, many times, and this is where you want to go if you want to use an expression, but you're not sure how there's actually pretty good documentation for it. Um, and there's also some great websites out there that will help you learn to do this stuff. And there's a lot of great sort of pre-built expressions that people have put out there for free, and you can use them. And, uh, they're very helpful and I'll put some links to those in this blog post.

Joey Korenman (08:04):

So back to our expression, uh, time is not doing it for us. It's making the minute hand move too slowly. Um, so with expressions, you can do simple math very easily. So what if we took that value time? And we multiply it by a number to increase the rate at which the minute hands turning. So let's say that we wanted the minute hand to make one complete rotation every three seconds. So what that means is we would need it to turn 360 degrees in three seconds. So hopefully you can do simple math because it is helpful. So if you divide 360 into three seconds, it's 120 degrees per second. All right. So every second we want to multiply whatever time it is, times one 20, um, to multiply things. And after facts you use the asterisk, uh, when you're doing math, you use the plus sign, the minus sign, the asterisk for multiplication and a forward slash four division.

Joey Korenman (09:11):

Um, and those are the four operators that you'll use most of the time. So if we type in time, times, one 20 hold command and hit return, you'll see. Now that minute hand is moving much faster and you can see every three seconds. If we go to three, we have had one rotation in three seconds. All right. So now let's focus on the hour hand. So for the hour hand, I'm gonna hit our to open up the rotation properties and when hold option, click rotation to add an expression. So now the, our hand, we need the, the same idea. We just needed to move slower. So let's type in time times. Um, so now, uh, you know, you just got to kind of use a little bit of logic here. If the minute hand rotates once around this, how far does the hour hand go?

Joey Korenman (10:04):

Well, it goes from one number to the next, right? So there's 12 numbers. So that means basically divide that circle into 12 pieces. Right? So really, I just need to divide this number one 20 by 12. Uh, so that's how far it will move. So one 20 divided by 12 is 10. That's pretty easy. Um, and so now you can see here we're at nine o'clock and that minute hand is going to go all the way around. And now we're at 10 o'clock. All right. So we have now set up an accurately functioning clock with no key frames. This clock could go, you know, as long as we make this comp, we could make this an hour in this clock would just keep running indefinitely and it would always tell the right time, which is great. Um, so this is just a quick example of how expressions can be useful and can save you a lot of time.

Joey Korenman (11:00):

If you had to key frame this, it would be a pain. Um, and before we end the tutorial, I want to show you one other quick thing about tutorials. That's prob what, sorry about expressions. That's probably my favorite thing about expressions, and this is the ability to set up controls for things like this. And I'll show you what I mean by that. So what I'm going to do is I'm going to add a Knoll object, right? And if you don't use Knoll objects, uh, you should get in the habit of using them. They are just objects and after effects that have no image associated with them, they don't look like anything, but they're sort of like a utility layer and you can use them to parent things, you know, for, I can get into this in a later tutorial, but a lot of times they would be used, for example, to parent the minute hand, our hand and clock to a Knoll so that now you can move the Knoll and it moves the entire clock.

Joey Korenman (11:54):

So you don't have to move all three layers. Um, but what I'm gonna use it for here is to add some controls for the clock. So what I'm going to do is rename this controls. And what I want to be able to control is the speed that the clock is turning. And I want to be able to control the minute hand in the hour hand with one control. So with expressions, this is pretty easy. If you go up to the effect menu, you'll see this expression controls area. And if you've never used used expressions, you've probably never used these controls. Uh, they are effects that on their own, don't do anything, but you can use them to drive other layers and other properties. So what I'm going to do is pick the slider control, um, and you can go through and try out all of these controls and you'll be able to see what they look like.

Joey Korenman (12:47):

And it may give you an idea of what they could control for you. The slider control just gives you one value that you can change and you can slide it, or you can type in a number. Um, and this doesn't do anything on its own, however, it could drive something else. So let's rename this slider control clock speed. So now, if we, if we open up this layer, go to effects and go to clock speed and just, you know, the hot key for that would be E and he brings up any effects that are on a layer. Uh, and we open up this arrow here so we can see the slider. And the reason we need to do that is because we are actually going to link the speed of the minute hand to this slider. So I'm going to click the minute hand layer, and I'm going to hit you.

Joey Korenman (13:37):

You is probably the most used hot keen after effects. You should definitely know that one, it opens up any key framed or, uh, or properties that have expressions on them. Um, so we can see our expression on here, and it says time times one 20. So instead of hard coding the number one 20 into this expression, I want this control to actually set what number we're multiplying by times by. So what I'm going to do is erase this, and we are now going to use the pick whip button here. Um, this pick button, you can tell, it looks exactly like the one in the, uh, in the parent column. And it works the same way. Uh, you know, the parent column lets you parent this layer to this layer by just picking it and letting go. Um, so what we're going to do is say, we want to multiply time times and we're going to use make sure you've used this pick whip in the expression area, and we're going to pick whip it to this slider.

Joey Korenman (14:40):

And it doesn't work. If you try to connect it to the effect itself, it has to be to the value within that effect. And you can see that after effects automatically typed all this stuff in. And this is basically saying time times, this comp meaning this composition that we are in the layer named controls the effect named clock speed slider. So you can see it basically just starts very wide, the entire comp and it gets narrower, narrower and narrower until it's literally pointing to this exact slider in this effect on this layer. Um, this is the way after effects, uh, writes out sort of the address of this, if you will. So we're going to add the semi-colon hold command and hit return. Um, and the slider sets zero right now, but let's say I set that back to one 20.

Joey Korenman (15:35):

So now the minute hand is moving again at exactly one rotation every three seconds. But if I change this to say 10, now it's moving very slowly. See, so now what's great about this is I can basically copy this expression. I can just click in the box and hit command C or control C on a PC. I'm going to hit you on the hour hand. I'm going to erase this expression and paste the other expression in there. Now we don't want the, our hand to move exactly the same speed as the minute hand. Obviously we've decided we, we need it to move 12 times less than the minute hand. So what we're going to do is have this expression say time times all of this stuff, basically pointing to this slider value, and then we're going to divide by 12. So you can see I, you can have multiplication and then division.

Joey Korenman (16:35):

And, uh, you can basically type in a long string that will sort of do all this math for you and arrive at the value and give you what you want. So command enter. And now you can see that the, our hand is moving at the right speed. So here we are at 12 o'clock, I'm going to hand goes all the way around now we're at one o'clock two o'clock. And the great thing is that. Now, if we want the clock to go faster, all we have to do is set this number to something else. If we wanted it to go faster, we could say 200 and it will go faster, but it will still work like the, the amount that the hour hand moves relative to the minute hand will always stay correct, because we've set up the expressions to do that for us automatically.

Joey Korenman (17:27):

So this is, this is a very small example of the power of expressions, but hopefully it'll, it'll get your brain thinking a little bit about ways to speed up your work by using this. Um, again, if you were going to key frame this and have it be precise, it would be pretty tricky. And then if your client came to you and said, yeah, the clock looks great, but can it go about 15% faster? Well, that would be really tricky if you had key frames and you'd have to do some math and you'd have to do some trial and error. And you know, with this, all you have to do is change this number, change it to something else. And now the clock goes slower or faster, however you want it to work. So that was the introduction to expressions. Uh, hopefully now you know how to apply expressions, you know how to look for expressions in the dictionary, uh, and you know, that you can always go up to help and hit effect, uh, expression, reference, um, and read about the expressions there. And I hope moving forward, uh, this provides a foundation you need to start using expressions in after-effects. Thank you guys so much, and I'll see you guys soon.

Joey Korenman (18:35):

Thanks for watching. I hope you learned a ton from this lesson and that you're now way more comfortable with expressions than when you started. If you have any questions or thoughts, let us know. And we would love to hear from you if you use expressions on a project. So give us a shout on Twitter at school emotion and show us your work. And if you learned something valuable from this video, please share it around. It really helps us spread the word about school motion, and we truly, truly appreciate it. Don't forget, sign up for a free student account to access project files from the lesson you just watched, plus a whole bunch of other awesome stuff. Thanks again. And I'll see you next time.

Success! Check your email (including spam folder) for your download link. If you haven't yet confirmed your email with us, you'll need to do that one time.
Oops! Something went wrong while submitting the form.
Featured