Join Angie Taylor for an in-depth discussion in this video Using variable values, part of After Effects Expressions: Getting Started.
There are times where it's useful to have a variable value to feed into your expression. I'll show you one of those examples here. Here I have an expression, which is basically taking the angle of this effect here, the cog speed effect. And it's using that to control, this position value. So, you'll see I've got value one just returning itself. So, we're keeping the value that was originally there before the expression for the Y value.
And then, the X value is coming from the angle value of the controller layer. And, if I preview that, you'll see I have this layer moving. Okay? Now, it's moving in the wrong direction for a start. Plus I'll probably have to offset it in position. So if I do that just by adding and subtracting numbers first of all. I would think, okay, maybe multiply by minus one to have it go in the opposite direction. And we'll see it's now moving in the opposite direction, and now I need to make it go faster.
So I would say, okay, maybe times minus two would make it go faster. Okay, so it is going faster, still not fast enough, so I'll go minus three. And you can already see how awkward this is. I'm having to, that's almost there but you'll see it's not quite the right speed. So I keep having to adjust this value till I get it exactly right. And then if I needed to adjust the position as well so that the cogs lined up, I would have to go in there and, maybe do, add 50.
Okay, add 60, and there's a lot of trial and error, a lot of selection and typing has to be done. So another way of doing it is to create a new effect that has a variable. Now, I've created those already here. Basically, the way they were created: I went to Effect > Expression Controls, and we've already seen the angle control. There's also a slider control. I added that. I renamed it, hitting return on the keyboard to make it editable and I've created one called multiplier and one called adder.
Now I've already worked out the values for this, so I've typed those values in, but let me show you why they're useful, First of all, let's bring that down to zero. And let's bring this down to zero. That's how it would be normally. They'd both be at zero, these two values. Now, you want to make sure that you lock the Effect control panel for the controller so that you can see all of these while you edit your expression. And then what we'll do is we'll come down here. And instead of saying, times minus three plus 60, we'll remove that.
And what we're going to do is create new variables for these. So I'm going to create a new line on my expression, and for multiplier, I might say, okay, multiply, value. Okay, so I can make it more than one word, but it's good to keep it as single piece of text without any spaces. So, multiply value equals. And then what I'm going to do is pick whip to the multiply sliders value.
So, that's my first variable. My second variable will be add value. And I'm going to say equals, pick whip to the adder slider, and put semi-colon at the end. And then on the last line of the expression, I'm just going to make the field bigger so we can see it. I'm going to say temp* the symbol for multiplication, multiplyvalue, so I'll just copy and paste it from here. So temp*multiplyvalue+addvalue.
Okay. And that just makes it a little bit easier to figure out. Now at the moment it's not doing anything, so it's got multiplying by zero basically. But, what I can do is I can adjust that and interactively see how it's affecting my final animation. Now, I know that this was about minus, three, we had it at, wasn't it? Minus three and it wasn't quite right, so what I can do is I can go up in smaller increments by selecting the value here, holding down the Command key, and just going by one point at a time, just to get it exactly where I want it.
And I might also want to adjust the position as well. So, maybe go in increments of ten to do that. So I'm holding down Shift and clicking the up and down arrow keys to go in increments of ten. So holding down Cmd and arrow keys goes in points. Holding down Shift and arrow keys goes in increments of ten. And, just hitting the up and down arrow keys on their own go in increments of one. And that way, you can adjust the position and the movements of the layer, much easier than just by adding values in one by one.
- What are expressions?
- Creating and applying expressions
- Linking expressions to layers with the pick whip
- Simplifying expressions with variables
- Creating looping animations
- Randomizing values
- Converting expressions to keyframes
- Creating keyframes from audio with expressions
- Controlling 2D effects in 3D space
- Converting color from RGB to HLS
- Formatting strings and text