Tutorial
No items found.

Tutorial: Create a Gear Rig Using Expressions After Effects

by
Joey Korenman

Here's how to rig a gear.

In this lesson we'll be using some expressions that might look a bit complicated, but we promise that you'll get the hang of them. Joey is going to walk you through the whole process of making this gear rig turn using just a bit of math. Don't worry! It's not that bad.Check out the resources tab for the expressions that Joey used in this lesson if you don't want to type them all in by hand, or if you want to check your work if you're typing them out as you go along.

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

Tutorial Full Transcript Below 👇:

Joey Korenman (00:21):

What's up Joey here at School of Motion and welcome to day three of 30 days of after effects. Today, we're going to talk about one of my favorite topics, expressions. It's also one of the dorkiest things that a grown man can talk about today. We're going to take a look at how to animate some gears because they're kind of a perfect example of something that moves in a mathematical way. And that's something that you don't want to necessarily have to key frame, especially if you have tons and tons of gears to animate, I'm going to show you some strategies on how to deal with multiple gears. And also don't forget to sign up for a free student account. So you can grab the project files from this lesson and the expressions as well as assets from any other lesson on the site. Now let's dive into after effects and get started.

Joey Korenman (01:04):

So more expressions for you guys, and those of you who have not watched intro to after effects expressions, you should probably watch that one first because that's going to let this tutorial make a whole lot more sense for you. Um, I'll link to that in the description for this tutorial. So what I want to show you, um, is just another cool way to use expressions. Um, and this is actually going to get slightly advanced just because when I started building this thing out, um, you know, as happened so often you, you think that this is a simple problem to solve and it ends up being more complicated than you thought. So what I want to show you guys is specifically how to create a system of interlocking gears that really work like real gears. They actually turn correctly and accurately, and they don't intersect. Um, and you can control exactly how fast they're turning and they all turn you know together.

Joey Korenman (02:05):

Um, so let's dive right in and get started here. So I have a, here's what I did. I, um, I went into illustrator and I made four gears, right. So I made this one and then a little bit smaller, a little bit smaller and a little bit smaller. All right. Um, and so let's bring those into a comp and take a look at them. So I'm just gonna make a new comp, we're going to call this, uh, gear vid. Um, and I am going to just make this a light colored background so we can take a look at it. All right. So let's just drag all these in there, one by one. So you got gear one, like you're two or three and gear four. Okay. So when I started out, um, creating this tutorial, what I thought I would do is just sort eyeball, you know, the speed of these gears and come up with a, an expression rig that would let me just kind of keep nudging and adjusting the speed of each gear until it looked right.

Joey Korenman (03:10):

And it turns out that's actually very tricky. Um, because if this gear, let's say this big one spins around six times, then this little one has to spin the exact correct. Number of times, otherwise the teeth are going to start intersecting with each other and that's not what I wanted. So, uh, I kinda beat my head against my desk for awhile, and I did some Googling. And what I found out is that the right way to do this is a, you have to make sure that all of the teeth of these gears are the same size. And what I mean by that is even though this little guy is much smaller than this big guy, if you look at the actual size of the teeth, right. They're the same. Okay. So when I made these in illustrator, I just made sure, um, to use the exact same size and I can get into exactly how I did that in a different tutorial, if anyone's curious about how I made the gears.

Joey Korenman (04:06):

Um, so now that I've got them set up so that they can actually work like real gears, I had to figure out the math involved, uh, in making gears work together. And it actually wasn't as complicated as I thought. So let me start building this rig. And then I'll get into the math behind the way gears work. Um, and I hate that there's so much math in my tutorials, but unfortunately motion design is really full of, of math and kind of sneaky ways. So let's start out by making a Knoll and this is going to be the gear controller. Okay. So this is actually going to have the property on it that I will key frame to rotate these gears. So to do that, I'm going to add an expression control, uh, specifically an angle control. All right. And so what I want is to be able to rotate this and have all of the gears rotate correctly.

Joey Korenman (05:00):

And, you know, there's some other ways you could animate these where they sort of animate themselves, you know, maybe I could use, um, a time expression so that they're constantly rotating, but the good way, the good thing about doing it this way is that I can have them kind of jerk when they start, maybe have them overshoot speed up, slow down, and I can really just control it very nicely with that. So let's start with this first gear and let's think about what kind of controls you're going to need for a gear. Um, so if I'm rotating this right, and let me just put a key frame on it, put key frame here, move forward three seconds. And why don't we have that? Just do one rotation. Okay. So that control is just rotating. All right. And it's not driving anything yet. Um, so what I could do is I could, I could, you know, bring up the rotation property of this gear, right.

Joey Korenman (05:55):

And bring up this angle control. Right. I can just hit E bring up the angle control effect and then open that up. So now if I hold, if I hold option and I click stopwatch on rotation, right. It opens up an expression for the rotation property on this layer, and I can pick whip to that angle control. All right. And so now that gear is rotating based on what this angle control is doing. That's wonderful. Okay. So now what about this gear? Well, there's one problem is this gear is going to have to spin the opposite direction. Okay. So I know that I'm going to need the ability to tell the gear, which way it's rotating on top of that. Um, if I let's just do this real quick, so you can see, um, if I just copy this expression, I can just hit command C come up to gear two and hit command V and it'll paste it.

Joey Korenman (06:48):

And obviously it's not rotating the right way. So I'm going to double tap you. Um, this is kind of a new thing with, um, the creative cloud version of after effects. If you hit you, it will not bring up any expressions. You have to hit you twice. Um, it'll bring up key frames, just not expressions. If I open this expression and I put a negative symbol in front of it, it will now rotate backwards, but you can see that it looks okay here. But if I scrub forward a few frames, it's going to start, I'm going to scrub backwards, actually, right there. You can see it's actually intersecting the gears or the teeth are intersecting because this gear has less teeth. So it has to spin at a different speed. Okay. Um, so I'm also going to need to be able to tell each gear how much faster or slower than the first gear in this chain, uh, how, you know, how fast or slow it should go.

Joey Korenman (07:46):

So that's two pieces of information I know I'm going to need, so why don't I start? Um, I'm going to just say, and this is the way actual gear systems work. You have one gear that is sort of the primary moving gear. All right. And so I'm going to say gear one is that gear. This is the gear that everything else moves based off of. So I'm gonna make it a different color, just so I can remember that. Um, and I could maybe even lock it. All right. So in this gear control, um, I need to add one more, uh, expression here or expression controller. And this is, this is what I discovered. So in order to figure out how slow or fast this gear needs to move, what you're supposed to do is divide the number of teeth in the main gear, by the number of teeth in the next gear.

Joey Korenman (08:35):

Okay. So I counted this gear has 18 teeth in it. Okay. So what I'm going to do is I'm going to add a slider control. Slider controls are just handy because they let you just type in a number and I'm going to rename this gear teeth count. Okay. And I'm gonna put 18 there. And the reason I'm not hard coding this 18 in somewhere, just in case, you know, you ever decided to make this the main gear. Right. Um, it, it just makes everything easier if you kind of future-proof it. So gear teeth count is 18. And again, this is referring to the main gear, this first gear, uh, so on the next gear, I'm going to need two controls. One control is going to be the number of teeth on this gear. So I'd just say number of teeth, then the next thing I'm gonna need to tell it is, uh, is it rotating clockwise or counter-clockwise okay.

Joey Korenman (09:42):

So to do that, um, I could just add another expression control called a checkbox control. All right. And this just lets you turn something on or off like this. So I could say clockwise question mark. And there you go. There's my controls. So now let's wire these things together and figure out how the heck this is going to work. So when I do this, I'm going to use more expression code than you actually need, because I find it's better to do that. It makes it easier to read sometimes. Okay. Um, when you start writing a lot of expressions and I use a lot of expressions, probably every project they use them. Um, it's very easy to forget what the expression is doing or why you did something a certain way. So it's really nice to make it a little bit easier to read. Okay. So let's open up the rotation, property of gear to delete the expression that's in there and let's start with a new expression.

Joey Korenman (10:40):

Okay. So I'm going to option, click on the stopwatch. And first thing I want to do is define the variables that I'm going to be dealing with here. Um, and again, you don't need to do this, but it just makes it easier to think about and easier to read. So first thing I want to know is the number of teeth in this gear. So I'm just going to make a variable called numb teeth. Okay. And you can see the way I'm typing this, where I have lowercase. And then on a new word, I just do a, an initial paps. That's very common way. If you ever see code or, you know, talk to a programmer, that's, that's how a lot of them do it. Um, so I've kind of adopted that. So the number of teeth equals whatever this slider is set to. Okay. So I'm just pick whipping, uh, every line in your expression needs to end with a semi-colon.

Joey Korenman (11:32):

Okay. That's very important. It's like the period at the end of the sentence, the next thing I need to know is is this clockwise checkbox checked? So I'm just gonna say clock wise equals this. Okay. Now what the heck does that mean? This first expression makes sense, right? The number of teeth equals whatever this number is, but the second one doesn't really make sense. What this check box actually does is it returns a zero. If it's not checked and a one, if it is checked. So this clockwise variable will either be zero or one. Okay. And I'll show you what to do with that in a minute. So the next thing we need to know, uh, is we need to know I'm just going to hit enter for a minute, coming back up here. So we also need to know what this angle control is set to and what this main gear teeth count is set to.

Joey Korenman (12:29):

In fact, let me rename that. So it's a little bit clear. This is the main gear teeth count. Okay. So what I'm going to do is I'm going to make sure that both of these, uh, properties are opened up on the timeline so that I can access this layer, but still pick what to them. Okay. So let's go back to our expression and keep adding stuff. So we need to know the main gear teeth. Okay. So the number of teeth in the main gear is going to equal this slider. Okay. Semi-colon and then the last thing we need to know is the, uh, the control angle, right? So what is this gear controls, angle control set to, and I'll just call it a main control equals that. Okay. So now in this expression, and this is one of the things that bothers me about after effects, I wish it would do a better job of giving you more room for expressions when you need, um, if you're running out of room, you can just sort of move your mouse over the bottom, uh, sort of boundary of that box, and then you can stretch it out.

Joey Korenman (13:37):

You get a little more room. Okay. So we've got our variables now. So let's think about the way this works. So to, in order to find out how much faster or slower this gear is going to turn than the main gear, we divide this number of teeth by this number of teeth. Okay. So we're, we're going to try to figure out the ratio of, you know, of the speed, you know, that we want to basically multiply times the speed to get the new speed for, for our smaller gear. So let's make a variable called ratio. We're going to say ratio equals and it's going to be this number, right? The number of teeth in the main gear. So main gear teeth divided by the number of teeth in this, which is this variable num teeth. Okay. You type that in. Semi-colon great. So that's the ratio.

Joey Korenman (14:35):

Okay. Now there's another part to this, which is, is it going to turn clockwise or counter-clockwise? So now there's there's, this is where it gets a little bit more complicated. And again, with expressions, once you use an expression twice, uh, you're gonna remember it and it's, and it's going to work for you. Um, the first time you try to do it, you're going to type something in wrong. You're going to screw it up and you're going to have to spend an hour figuring that out. Um, and I'm sorry, but that's just the way it works. Once you do it the second time, then you'll remember it. At least that's how it works with me. So we sort of have two cases here if it's turning clockwise. Okay. Let's say that, you know, the angle of this gear here is 90 degrees. Well, this, this gear needs to be slightly less than that because it's got less teeth, so it's turning slower.

Joey Korenman (15:24):

Okay. So that means that, you know, we have to basically multiply this angle times the ratio. Okay. If that makes sense. If it's turning counterclockwise, though, it's actually got to go backwards. So it has to turn any negative direction, which means we need to multiply the ratio by negative one to get this, to turn the right way. All right. So when you have any kind of situation where if one thing happens, do this, otherwise do something else. Um, the way you do that with expressions is you use an if statement and these are pretty simple. Logically the only tricky thing about them is that you have to remember the syntax and print the C's and brackets and, and making sure everything is formatted correctly. Otherwise it won't work. So I'm going to show you how to do that. So first thing we're going to do is we're going to say if, okay, that's easy.

Joey Korenman (16:20):

Now we need to put in parentheses the, the thing that we were testing and what we're testing is, is the clockwise variable. So is clockwise equal to one. Okay. Now you'll see, I put two equal signs in there. Um, when you use an if statement, uh, and you want to see if something's equal to, um, equal to a specific number, you have to use two equal signs. There's some programming reasons why it's not one equal sign. I'm not going to get into that. Just remember it has to be two equal sides, right? If clockwise equals one, okay. Meaning is this checkbox checked? All right, now we're going to tell it, what do you do if clockwise is one and the way you do this is you open a bracket. All right. And now, whatever I put after that bracket is what will happen if clockwise is one, oh, excuse me.

Joey Korenman (17:20):

Um, and you can have multiple lines. You can have a whole bunch of things happen. Um, and generally when you're coding, um, it's just sort of common practice to go to the next line. So you, you open up this bracket, here you go to the next line and you hit tab to go over a little bit. It makes it a little bit easier to read. Okay. Now, if clockwise is one what's going to happen is we're just going to multiply the ratio times the main control. Okay. So we're going to say if clockwise equals one, then the, the answer to this, right? The, the actual number we want to feed into rotation is ratio this ratio, variable times main control. Okay. That's it. So that's the end of this part. So I'm going to close the bracket. Okay. Now you can, you can stop there if you want to, or you can add another little piece, which is else.

Joey Korenman (18:25):

Okay. And then you open another bracket and go to the next line. Now what this is saying, and you can probably figure it out just because it makes sense. If clockwise is one, do this else or otherwise do something else. If it's supposed to go counterclockwise, then what we're gonna do is we're gonna return ratio times main control times negative one. Okay. And that negative one is just going to make that rotation happen backwards. Okay. Go to the next line, close the brackets. And we're getting an error. So let's take a look. Oh I, okay. So this is good. This is great here. Um, so right now, if, uh, let me hit. Okay. Um, what it's telling me is that it's D it's trying to divide something by zero, and obviously you can't divide by zero. And that's because this number of teeth has been left set at zero.

Joey Korenman (19:24):

Now, obviously you're never going to have a gear with zero teeth so that that's always going to have a number in it, but I'm glad you guys saw that expressions are not like Bulletproof software code. If you were programming something, you, you know, if I was really trying to button this rig up and make it so that you would never have errors, I would, I would say if clockwise is one, do this, otherwise do this. I would also check to see if this number is set to zero. Then I, then I need to tell the expression how to handle that. Um, now I'm not going to do that, but, um, just so you guys know, that's why that little error message came up. All right. So let's figure out how many teeth this gear actually has. Um, so let's start with this one, right? That's in between the two gears.

Joey Korenman (20:09):

So you got 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 gears on that one 16 teeth. So we type in 16. Okay. Now you could see that the expression is not turned on because it's got, you've got this little icon, uh, the equal sign with slash through it's. If I click that, now everything should work because we're no longer dividing by zero. So just remember, um, that you need to make sure that this slider is not set to zero, if you want this expression to work. So let's see what's happening now. Okay. So it's going the wrong way. All right, because it sets a clockwise. Now, if we uncheck that, Hey, look at that, it's working. And in fact, if we go through it frame by frame, you can see that the teeth never actually intersect. It's working perfectly on the first try, which is kind of amazing. Um, so let's stretch this key frame out here so we can get a better look at this.

Joey Korenman (21:09):

Okay, cool. Now I want to show you something, um, because there's one other piece that we need to add to this expression to really make it versatile. Um, and so let's say that I had this gear here. Okay. And that's exactly where I want that gear. That's exactly where I want that gear. The problem is the teeth are intersecting. Um, now they're moving at the right speed, but the problem is I just need to offset this rotation a little bit so that it will fit properly in this gear. So now I'm realizing, oh, I also need the ability to, you know, just offset the rotation a few degrees in either direction to make it fit perfectly. So with that gear selected, I'm going to add another slider control, and I'm just going to call this rotation offset. And so now, where is this going to plug in?

Joey Korenman (22:07):

So let's bring up our rotation expression right there. Okay. Um, and let's think about this. So what I need to be able to do first, let me define this as a variable, make a little bit easier to deal with. Uh, I'll just call it offset equals this. Okay. Um, and really all I need to do is add that offset to whatever the result is, and that should do it. Um, because if it's zero, it's not going to change the answer and then I can make it positive or negative to kind of turn it one direction or the other. So why don't we just say if clockwise is one ratio, times main control plus offset, and then I'll just add the same thing down here, plus offset, and let's see if it works. So now if I adjust this expression, you can see, I can just adjust it and then it should work perfectly.

Joey Korenman (23:10):

Okay. And now if I move it back here, I can just adjust it so that it works at that position. So that's pretty much the gear rig. Now we're ready to go. Um, so now the way that you apply this to the other gears, um, is you first copy the slider controls because if you copy the expression first, that expression is looking for slider controls and, and the angle control and the checkbox it's looking for controls that are not there. And it'll give you an error. So it's just a little bit easier to do it this way. First copy the sliders, let's paste them, and then you can just copy the rotation, uh, property. Um, and it will copy the expression that's on there. So let me just paste that here too. All right. And now we can see if it's working on these gears.

Joey Korenman (24:05):

So here's gear three. All right. And I'll put it down here now, gear three. How many teeth does it have? Right. Like if we just hit play, it's obviously not working. Right. Um, but well, first we know it's going the wrong direction, so let's just hit the clockwise checkbox. So now it'll go clockwise and then we just need to count the teeth. So you've got one in there then 2, 3, 4, 5, 6, 7, 8, 9, so nine teeth. So if you just type in nine in there, now that one works perfectly too beautiful thing. And then if you need to nudge it a little bit, if you just want it to be a little bit more perfect, if you actually want it to look like the teeth are touching and it's pushing the teeth just a little bit, you can get, you can get really precise, right. And we could go back and then adjust gear to, and the, and this, this is the power of expressions because it lets you be so precise with stuff like this.

Joey Korenman (25:04):

If you were just trying to manually key frame this, it would be a nightmare. Um, but with expressions is actually pretty easy. Once you wrap your head around, you know, the math and I'm sorry again with the math, but, um, once you wrap your head around it and it's not that difficult, um, you can do all this stuff so fast. All right. So obviously this one it's turning the right direction. It's just not turning fast enough. And it's got six teeth, so we just typed six in there and then we can adjust the offset of it. All right. And actually, I want this to look like it's being pushed by this one. So there we go. All right. So there we go. Right. Gears, turning perfectly teeth, not intersecting. Um, and that's, that's all there is to it. It's really kind of that simple you're done.

Joey Korenman (25:58):

Um, the rest of it is just copying and pasting and, and arranging the gears the way you want. Uh, one good thing to know if I take this gear for example, and just duplicate it, bring it over here. Um, this little, you know, expression, it, it doesn't break. If you scale things down a little bit, um, you can get away with scaling. Lee's just a little bit, all right. You see it still works. It doesn't intersect. Um, so you can, you can get a ton of variety. And of course I only made four little gears here, you know, cause I was, you know, kind of being lazy and it just didn't want to spend that much time making gears. But, um, you can see even with just four gears, um, just, you know, messing with the scale a little bit, of course these are vectors.

Joey Korenman (26:44):

So, um, I can just turn on continuous rasterize and have, you know, perfect shapes every single time. Um, but you can get a ton of variety and of course, you know, you can play with color and all that stuff. Um, but now that you've built this little rig with simple controls, you know, that any, any after effects artists could figure out, if you just, you know, get shot them a little email, now you're good to go. And, uh, and again, the, the beauty of having this gear controller do all the work is that now, instead of having just like a simple move, you know, maybe what you do is you have, it sits still for a few frames and then maybe it's like someone turned the motor on and it kind of like kicks back a little bit like that hangs there for a couple of frames and then it kind of shoots forward.

Joey Korenman (27:35):

You know, it just kinda goes a little too fast, you know, and then kind of catches itself pauses, and then it starts going right. And, and, you know, I don't know how this is going to look, but let's see if I let's see do a little Ram preview. Right. You get like a little, you know, like, like a little sputter, you know, and you need a little sound effect, need a little, you know, or something. Um, and then you, you have all this control, you can go into the curve editor and you could say, okay, once it starts going, I want it to really slowly get going and then I want it to end up being more or less linear. Um, and then you can, let's drop down in here and let's have it really catch on that first. There we go.

Joey Korenman (28:20):

Yeah. See that. And then it starts turning slowly and maybe that's too slowly. So we want to pull that handle back in. Yeah, there we go. Right. So, so now you have all the control with this one key frame, but all of these gears are going to fit perfectly and they're going to work perfectly. Um, and you're gonna, you're just going to have much easier time. So, uh, I hope that this was useful. There was a lot of other stuff I didn't get into, um, that I actually used to make the animation that you saw at the beginning of this tutorial. And if you have any questions about that stuff, um, please leave me a comment. Um, you can find me on Twitter, on Facebook. Um, and, um, I'm definitely, you know, I'm leaving some of this stuff out there because, you know, I want to find out what you guys are interested in learning.

Joey Korenman (29:13):

Um, you know, interesting fact, I actually used an expression to colorize the gears so that I could just sort of pick four colors and it would randomly select a color for me. So I didn't have to do that either. I'm an enormous family guy fan. So I hope you guys, uh, enjoy that little Easter egg there. Anyway. I hope this was, you know, useful, informative. Thank you guys as always, I will see you guys next time. Uh we're in 30 days after effects here, there's a lot more content coming. So stay tuned. Thank you for hanging out. I hope this helps you grasp how much of a time-saver expressions can be. If you have any questions, let us know on the site. And if you learn something valuable from this video, please share it around. It really helps us spread the word about school of motion. And we truly appreciate it. Don't forget to sign up for a free student account to access the project files and the expressions from the lesson you just watched, plus a whole bunch of other awesomeness. I'll see you on the next one.