| ---A Little Background--- |
In parts 1 3 of this tutorial series, we saw how we can construct cogs and gears in After Effects using the Path Text effect. Although there are a variety of methods of constructing cogs and gears, using the Path Text effect means that we can create cogs of different sizes which will animate together.
Parts 1 3 were about the creation of the cogs; this tutorial is about the animation.
More specifically, this tutorial is designed to make you start thinking about expressions, which are incredibly powerful but often overlooked.
Expressions were added in version 5, but theyre not obvious - you wouldnt notice the addition of expressions unless you specifically went looking for them. And because they havent changed the way you have to use After Effects, many animators have continued to work exactly as they have for years, and not even had a go.
Expressions are an alternative to keyframes instead of setting a definite number (for example, a rotation keyframe of 30 degrees), an expression is a formula or rule which calculates the value automatically. If youve used Excel then youll understand the difference between typing in an actual value, and having Excel calculate it for you with a formula.
The aim of this tutorial is to make you appreciate how useful expressions can be, and by showing that something which appears quite complicated is actually simple when you go to do it yourself.
Parts 1,2 & 3 of this series used the same After Effects project file but with Part 4, were beginning again from scratch.
| ---Part Four: Moving and Grooving Our Gear!--- |
Begin by opening a new Project, creating a new composition and importing the necessary files. There are a few image files well be using in the project, including the Worm Gear 1 second loop from tutorial 3, and a range of cogs which I created using the techniques in tutorial 2. You will need to complete Tutorial 3 and render the Worm gear movie. Also download the project files from above.
I used the PAL defaults of 768 x 576, at 25 fps.
Drag the Worm Gear loop into the timeline and use the loop function to extend the duration to 1 minute detailed in Part 3.

In this case, the X position of the Worm Gear is 50.
The first thing well add is the big cog we made in tutorial 2. Its been rendered out as a still image and named Cog 3 36 teeth. Drag it into the composition and position it so the teeth mesh nicely with the worm gear. I used the position values of 219, 127.

Because the worm gear moves upwards, I made the teeth of the cog rest on top of the thread. If the worm gear moved in the opposite direction, I would have made the teeth of the cog rest against the bottom side of a thread.

The idea is to make the Cog rotate as the Worm Gear turns. We could manually keyframe the rotation, but this is a good opportunity to demonstrate a simple expression. The rotation of the cog relates to the rotation of the worm gear. We can use an expression to calculate this for us, and this way well never have to change the settings if we extend the duration of the composition.
Select the Cog layer, and press R to reveal the rotation settings.

Click on rotation to select the property, and go to Add Expression, in the Animation menu.

After Effects now turns the numbers red, and has added a line of text to the timeline. This line of text is our expression. At this stage, its saying that rotation = rotation. It might be zen, but its also a bit redundant.

What were going to do is set up a rule which determines how the cog relates to the worm gear. We should probably start thinking about what this rule is going to be.
The worm gear is an image of a worm gear turning the movie itself doesnt rotate or change position. So we will use the time of the movie as the basis for our expression.
We designed the worm gear so that it revolves once per second. Each time it revolves, the teeth move up until after one second, they look like theyve moved up one whole tooth. All of this was done in Part 3. The point is that 1 second = 1 tooth.
Our cog has 36 teeth again, we designed it this way in Part 2. So for our cog to do a complete revolution, the worm gear has to turn 36 times 1 revolution of the worm gear for every tooth on the cog. We can use this information to create a relationship between the time of the worm gear movie, and the rotation of the cog.
We know that 36 revolutions of the worm gear equal 1 revolution of the cog.
We know that 36 revolutions of the worm gear will take 36 seconds.
We know that 1 revolution of the cog is 360 degrees.
Therefore, our cog has to turn 10 degrees every second 360 degrees divided by 36 seconds.
Thats what our expression will do- it will tell After Effects that for every second of the Worm Gear animation, the cog has to turn 10 degrees.
Notice these three icons, because theyll become your new best friends:

In simple terms, an expression is a formula. But formulas sound complicated and also imply lots of typing, and typing can be tedious.
These icons are our saviours they are designed to let us build up expressions with the minimal amount of typing and hassle.
Well get to the middle icon later, but for now well use the arrow.
The arrow is the shortcut to a menu of common commands. Instead of having to learn complex programming terminology, all we have to do is click on the one we want from a list. Its really simple.
In this case, were going to use the time command so click on the arrow to reveal all the options, and from the Global menu select time

After Effects has now inserted the word time where it used to say rotation.

You might be thinking that this is an anti-climax, because you could have typed the word time faster than it took to read that last section but trust me - not having to type in commands is great. Also, if you dont know the correct terminology for a command, you can usually guess by looking at the options. But lets get back to it.
The time command works in seconds, and as we need our cog to turn 10 degrees every second, all we have to do is add a *10.
Click in the text box and type *10.

Click outside the box and youre done. Thats an expression. The rotation of the cog equals the time (in seconds) multiplied by 10.

Try scrubbing in the timeline and youll notice that the cog now rotates in perfect synchronization with the worm gear. We dont have to think about it any more.
Expressions really are simple, so lets set up another one.
Drag the other cog the one with 12 teeth into the composition and place it at 383,64. At frame 0, the teeth should mesh nicely with the larger cog.


Its obvious that the large cog will turn the smaller cog, so once again we can easily set up an expression to do this for us.
Select the Cog layer with 12 teeth, and press R to reveal the rotation properties. Click on the word rotation to select it, and add expression just as we did earlier.

This time, the rotation of our cog will be directly related to the rotation of the first cog, so adding the expression will be even easier.
Remember our new best friends:

The centre icon is the pick whip and allows us to click on anything we want to include in our expression. It saves us lots and lots of typing.
All we have to do is click on the pick whip icon, and drag it to the rotation of our first cog. If you cant see the rotation of the 36-tooth cog, select the layer and press r so the rotation expression which we just typed is visible.
You can drag the pick whip to either the word rotation or to the numbers, even though theyre red and say 0.
Youll notice that when you drag the pick whip over the word rotation, After Effects draws a box around it, showing that youre selecting it. When you release the pick whip, After Effects adds the text to our new expression for us:

It might look complicated, but remember we didnt have to type a letter. The text which After Effects has inserted is telling it where to find the rotation of the 36-tooth cog. From left to right, the text this_comp means that were looking as this composition (and not a pre-composition, for example). The layer(Cog 3 36 teeth.pct) is telling After Effects which layer were dealing with, and the rotation at the end is telling After Effects that were copying the rotation value. It might look like a jumble, but theres a simple underlying structure.
Basically, this line of text is now an expression that makes the rotation of our 12 tooth cog equal that of the layer Cog3 36 teeth.pct.
Because our first cog has 36 teeth, and this one only has 12, our 12 tooth cog should rotate 3 times as fast as the big cog (36 / 12 is 3) but in the opposite direction. To adjust our expression to do this automatically, all we have to do is click in the text box and add *-3.

And there we have it our second expression.
You can scrub in the timeline and youll see how the worm gear is turning the big cog, and the big cog is turning the smaller cog.
All without keyframes.
Lets do another one just for fun, to emphasise how simple it is.
Drag the Cog 7 into the composition it has 16 teeth. Position it at 236,316. At frame 0, it will mesh with the 36 tooth cog nicely.

Just as we did with our second cog, well add a simple expression so the large cog with 36 teeth will turn it.
Because we want to link the two rotation properties, we need to be able to see both of them in the timeline. If you cant see the rotation of the 36 tooth cog (as in the screenshot above), select the 36 tooth cog layer and press r to reveal the rotation, then do the same for our new cog, with 16 teeth. This means the properties we want to link are visible.
You should know the deal by now click on the word rotation for our 16 tooth cog, select add expression from the menu, then use the pick whip to drag a link to the 36 tooth cog. Notice that you can drag the pick whip to either the word rotation or to the red numbers they both work.

The big cog has 36 teeth, and the smaller one has 16, so the ratio of turns will be 36:16.
Do you know what 36 / 16 is? I wouldnt know without a calculator, but After Effects will figure it out. So click in the expression text and add *-(36/16). The minus sign makes the cog turn in the opposite direction.

Wow. Now weve added three expressions. Have a quick scrub in the timeline to see how everything is coming along. If we dont slow down a bit we might turn into programmers.
The pick whip lets us make simple expressions very easily. If you want, you can import the other cogs which Ive already made and have a ball creating your own drive trains, and use expressions to link them all together (I did, and Ive saved it as a separate project file.)
Weve added three really simple expressions that arent complicated, were easy to do, and work really well.
But you might be thinking that they werent really necessary. You could have added rotation keyframes just as easily, even though theyre not as flexible.
So what well do now is use expressions to create a piece of animation which would be very, very difficult to animate manually and even if you did would be really difficult to adjust afterwards.
Were going to add a piston to our composition, and have it linked to our 16 tooth cog in the centre. The expression will automatically calculate the position and rotation of the piston rod for us.

Heres a sneak preview of what were about to do
To keep things simple, well begin by using a white solid as the rod we can change it later.
Add a new solid to the composition and call it Drive Rod 1. Make it 200 pixels wide and 6 pixels high. I made it white for visibility.

In the timeline, set the anchor point to 0,3. We will be animating the rod from the left side, not the centre which is the default.

The first thing well do is use an expression to set the position of the rod. We want the left side of the solid to be connected to the solid part of the 16 tooth cog.
We begin as we did before press p to reveal the position properties, and click on the word position. Then add an expression.

Our Drive Rod needs two values for its position an X value, or horizontal position, and a Y value, or vertical position.
These positions will have a direct relationship to the number of degrees that the cog its attached to has turned - the relation of the position of the rod to the cog is determined by using trigonometry. This might sound scary, but its nothing more than we learned in high school and in actual fact, I just did a Google search for trigonometry and found the information which I needed.

The rotation of the cog will determine the x& y position of the rod. I learned this on the internet, showing that its useful for more things than rude pictures.
Remember our best friends?

Were going to use the menu (the arrow on the right) to add the commands, so we dont have to type much. Youll be amazed at how easy it is to create an expression that looks complicated without even typing anything.
The X position of the rod is related to the Cosine of the cogs rotation. I learned this on the Internet so it must be true.
Click on the arrow to show all our commands, and under the JavaScript Math menu, choose the first one Math.cos(val).

Notice how After Effects adds the command to our timeline.

The word val is short for value, and this is where we put in the bit that relates to us. Highlight the word val, because were going to replace it.

Now that its highlighted, we can use the pick whip to select what we want to replace it with we dont have to type anything. In this case, its the rotation of the 16 tooth cog.
If you cant see the rotation value of the 16 tooth cog, youll need to select the Cog layer and press r. This will give you an error message because we havent finished our expression yet. But you can ignore it, because were coming right back to it.

Drag the pick whip down to the rotation of the 16 tooth cog, and when you release it After Effects will insert the correct text into your expression for you.

Theres one little catch. The JavaScript based commands work in Radians, while After Effects works in Degrees. In order for the Cosine command to work properly, we need to convert the degrees to radians first. But once again, we dont have to worry about the format of the command or even type anything just use the menu.
If there isnt, you need an opening bracket after the s in Cos then position the cursor after the bracket, and select Degrees to Radians.

After Effects has inserted the correct command for us.

However, where it says degrees in brackets is where we want our rotation value, so move the text up as follows, and close the brackets at the end:

This expression is now giving us the Cosine of the Cogs rotation.
Whoever invented Cosines, and it was probably the Ancient Greeks, worked them out with circles that had a radius of 1. However, our circle (the cog) has a larger radius than 1, so we need to multiply our Cosine by the size of the cog. Theres a hole in the cog specifically designed to connect to the Drive Rod, and its 30 pixels out from the centre. So click after the last brackets and add *30.
At this stage our Drive Rod is in the wrong position. The Drive rod is rotating around a centre of 0,0, so we need to add the position of the Cog to the Position of the Drive Rod.
Once again, we dont need to type stuff, we just use the pick whip.
You will need to click on the 16 tooth Cog layer and press p, to reveal its position.
Because a position value needs two dimensions an x and a y but weve only written one so far, you will receive an error message from After Effects when you click outside the layer.

Just ignore the error message, well fix it up in a minute.
Once you can see the position values for the 16 tooth cog, click back in our expression, type a + after the 30, and drag the pick whip to the X position of the Cog layer.

After Effects does the dirty work for us, and were halfway there.
What weve written so far is for the X dimension, and we only need a small modification for the Y dimension. Enclose the entire line in another set of brackets, then select the entire line of text, and press Apple-C to copy it.

Press return to start a new line, and press Apple-V to paste.

All we need to do is tweak the second line to make it apply to the Y value.
The first thing to do is to change the word cos into sin. Then change the [0] to [1], because the X value is dimension 0, and the Y value is dimension 1. If youre really lazy, you dont even have to change the [0] to a [1] yourself just highlight the text to be changed and use the pick whip to select the Y position of the cog.

I took the lazy option Im about to use the pick whip.
Our expression is basically complete, we just need to format it so After Effects knows where to look.
A Position value has two dimensions X & Y. In an expression, these values must be enclosed in square brackets, separated by a comma like this [x,y].
We should enclose our formulas in square brackets, and separate the two lines by a comma:

Notice the square brackets at the start and end, and the comma at the end of line 1.
And thats it.
It might look complicated, but we certainly didnt have to type it out by hand, or learn any complex programming.

And now for the trick no matter where you move the 16 tooth cog, the drive rod will move with it, and will always rotate with it. The expression we just typed doesnt have to be altered no matter how fast the circle rotates, or where we position it.
Try doing that with keyframes.
But were only half way there. The next step is to angle the rod, or in other words, have the rotation of the drive rod relate to the rotation of the cog.
Once again well use high-school trigonometry, which I actually learned on the internet and not at high school (I did learn it at high school, but I forgot it).

In this diagram, sin(x) = b/c. The Ancient Greeks were good at this sort of stuff.
Select the Drive Rod layer and press r to reveal the rotation properties. Select the word rotation and add expression. You should be getting good at this by now.

Its possible to condense our formula into single line, but that would look very confusing. What well do is break down the expression line by line, so you can see how simple the underlying structure is.
To make everything clear and easy to follow, well use some variables. The standard in trigonometry is to label the sides of a triangle a,b & c. Well build our formula using this convention, so everyone can understand what were doing.
The value of c is the hypotenuse of the triangle, in this case its the width of our Drive Rod. This is easy to do just type in c=width. I didnt even type in the word width, I selected it from the menu. To separate our commands we have to use semi-colons, so add a semi-colon and press return.
The b side of a triangle is its height. In our case, the height is the difference between the y-position of the centre of the cog, and the y-position of the drive rod.
In the text box, type in b=position[1]. The 1 in square brackets means we only want the y value if we had used a [0] we would have the x value.

We can use the pick whip to select the position of the cog, so select the 16 tooth cog layer (ignoring any error message) and press p.

In this shot, I can see the position values of the 16 tooth cog, and am about the drag the pick whip down to the number 316.
Click back in our text box and type a minus (-), then drag the pick whip down to the Y value of the 16 tooth cog in this case its the number 316.
After Effects does our work for us:

Just like in high school, weve now set up the letter c to equal the hypotenuse, and the letter b to equal the height of our triangle.
Continuing the high school tradition, well use the letter x as our unknown value, although we do know from trigonometry that sin(x) = b/c. Remember that After Effects wants degrees, but the expression command will give us an answer in radians.
Our expression should look like this:

But dont think you have to type it all, remember the menus

The last line in the expression gives us the angle we need at the top corner of the triangle which is opposite the angle which is calculated by the Sin command, which is at the bottom corner of the triangle. This is demonstrated in the earlier diagram.
Your composition should look like this.

We need to make another slight adjustment to rotate the drive rod in the direction we want it. This is because in trigonometry, a horizontal line is 0 degrees, while in After Effects, a horizontal line is 90 degrees.
Adding a simple line x=x-90 fixes this difference.
I also added the letter x in square brackets at the end of the expression, this isnt needed but is tidy. It shows that the final outcome of the expression is the value of x, and by placing it in square brackets After Effects knows that this is the value to use.

Our completed expression
Your composition should look like this:

Now scrub in the timeline. Notice how the drive rod moves with the Cog.
Once again, the absolute beauty of this expression is that the Drive Rod will ALWAYS be in the perfect position, regardless of the position of the cog or the speed it rotates. You can move the cog anywhere you want, you can even remove the cogs rotation expression and insert your own keyframes to make it spin as fast or as slow as you want the Drive Rod will still work perfectly.
Its wonderful (pause to wipe away tears of pride).
If youve followed the tutorial this far then youll be all fired up for the next bit. Well add another drive rod to the first one, but this will only move horizontally.
Our second drive rod will attach to the end of the first one and the position can be deduced by the famous Pythagoras theorem.

The second drive rod has an X position equal to the a of the triangle.
This isnt as daunting as trigonometry, so we can breeze through this part:
Create a new solid and call it Drive Rod 2 make it 100 pixels wide and 6 high.

Change the anchor point to 0,3 again were animating it from the left side and add an expression for the position,

This is the expression we need to use, hopefully you can see the bits which you dont need to type yourself:

Lets go through it step by step.
Pythagoras says that c squared = a squared plus b squared. I think he was Greek too.
We know that c is the width of Drive Rod 1, and that b is the Height of Drive Rod 1 (the difference between its Y position and the Cogs Y position).
These two values are exactly the same as in our last expression.
So in this Expression, we can set up a simple equation to determine the value of a- which is the distance from the centre of the 16 tooth cog to Drive Rod 2.
The first line assigns c the width of Drive Rod 1.
The second line calculates the Height of the Drive Rod 1, equal to b.
Then we square c, and square b, and find the square root of the difference.
Then we just add the value of a, which is the distance between the Cog and the Drive Rod to the X position of the Cog.
The final line is the actual position notice how it is in square brackets. The format of position[x,y] is being followed, with x equal to a, and y = to the y position of the cog.
Now we have this:

Once again, try scrubbing in the timeline to see the results:

Its worth noting again how powerful these expressions are.
Ive already mentioned that you can move the Cog anywhere you want and the Drive Rods will move with it.
Ive also mentioned that no matter how fast the Cog rotates you can even randomly rotate the cog with the wiggler if you want the Drive Rods will always be in the correct position.
But even more impressive you can change the length of the Drive Rods and theyll still be in the correct spot. Drive Rod 1 currently has a width of 200. If you change it to 100, or 300, it doesnt matter After Effects will re-calculate the expression so everything looks perfect.

In the upper pictures, the Drive Rod 1 has been changed to a length of 100 but the expression still works. In the lower picture, the position of the Cog has been moved and the two Drive Rods move with it
all without keyframes.
This is our completed expression:

I decided to add a little round metal join to cover the space where the two Drive Rods join. All I needed to do was add a metal texture, and apply a circular mask.

Drag a texture into the timeline, and apply a mask:

The position of our round metal button is exactly the same as the position of Drive Rod 2 so we just add expression the the position of the texture, and use the pick whip to select the position of Drive Rod 2

The round bit (so small you can miss it
)

Just make sure that the metal texture is above the Drive Rods so we can see it.

Weve just added an elbow joint in less than a minute. Real mechanics cant do that.
The aim of this tutorial has been to introduce you to expressions, and demonstrate how they can be used to automatically generate animations which would be difficult to do with keyframes.
Ill leave you with one more just to give you something to think about.
This time well be using the ratchet which we made in Part 3. For the time being, Ive added rotation keyframes to turn the ratchet, but later on Ill connect it up to some more cogs and use expressions.

The latch is just a metal texture with a mask applied you can look at the project file to see but its very straightforward.
The aim was to animate the rotation of the latch so it appeared to be pushed out by the ratchet teeth as it rotated, but would swing back when the teeth were clear.
This is the expression applied to the latch, not the actual ratchet:

Without going into detail, this is what it does:
We only need to know how many degrees the ratchet has turned between each tooth.
So the variable a is the total number of degrees the ratchet has turned.
By dividing this number by 15 (360 degrees divided by 24 teeth), rounding the answer down, then multiplying the integer-answer by 15 again, we can work out the number of degrees we are between each tooth.
This way, no matter how many degrees the ratchet has turned, well always be working with a number from 0 to 15. This value is returned by the letter b.
As the ratchet turns, the variable b will cycle from 0 to 15, then back to 0 to 15 again as each tooth passes.
At this stage, we can simply make this value the rotation of the latch, but the latch isnt pushed out far enough to clear the teeth. So I multiplied b by 1.5.
Now the expression pushes the latch out as the ratchet turns, but we need the latch to fall back to a resting position once the tooth has passed.
The two if statements basically work out whether the latch is still touching a tooth
if not, then the value of b is decreased so the latch falls back to a vertical position.
The final line (b=-b) just reverses the direction of the latch, because its on the right side of the ratchet.
So thats it.

The Worm Gears connected to the 36 tooth cog, which is connected to the 12 tooth cog and the 16 tooth cog
the 16 tooth cog is connected to two moving Drive Rods, and the Ratchet pushes out the latch as it turns
. ALL WITHOUT KEYFRAMES!
Now that weve looked at the basic principles of expressions, in both simple and more complicated forms, we can go completely nuts and add as many cogs as we can.
Hope you have fun.
| ---Finished here? Check out the Extras? Go back?--- |
In part one, we look at the basic concept behind making cogs in After Effects, using the Path Text effect. This will demonstrate our trick in detail.
In part two, we look at how we can extend this basic technique to produce more interesting looking cogs, using textures easily found on the internet.
In part three, we extend the same techniques to construct a worm gear.
And part four demonstrates how we can use simple expressions to create amazing animations using the parts we created in parts 1, 2 & 3.
Part 4 Extras: From this basic overview, I created a more complete project with more cogs, and a few finishing touches. The project is called Part 4 Extras, and although it isnt a tutorial, there are a few notes about various refinements in it.
--- Chris Zwar
Please discuss this technique in the After Effects forum at Creativecow.net
|