LIBRARY: Tutorials Reviews Interviews Editorials Features Business Authors RSS Feed

Parenting and Expressions

Parenting and Expressions



Learn Parenting and Expressions in Adobe After Effects

by Dan Ebberts
©2001 Dan Ebberts and CreativeCOW.net. All Rights Reserved.

Dan Ebberts Article focus:
Dan Ebberts demonstrates setting up an animation of a piston, crank, and wheel using parenting and expressions so that we only have to keyframe one of the pieces and the others follow along. In this case we'll be using the wheel to drive the animation so we will have to develop expressions for the piston and the crank. We will then set keyframes for the rotation of the wheel and (hopefully) the crank and piston will animate automatically.





Download the project files here.

O.K. - lets get started. The first thing I did was create the piston, crank, and wheel pieces in Illustrator - each piece on a separate layer. Then I imported this into After Effects as a comp. Then I opened the comp and using the pan-behind tool, I adjusted the anchor points: For the wheel I moved the anchor point to the center; For the crank I moved the anchor point to where the crank attaches to the wheel; For the piston I moved the anchor point to where the piston attaches to the crank.

The concept for the animation is pretty simple - we make the wheel the parent of the crank and the crank the parent of the piston. Then as the wheel rotates, we need to rotate the crank around its new anchor point in the opposite direction by the same amount. This will keep the crank horizontal, which is not exactly what we want. So in addition, we need to add in an adjustment factor that will keep the crank/piston junction level with the center of the wheel. Then we will need to rotate the piston by an amount opposite of the crank adjustment to keep it level. The following pictures will illustrate this:

Starting Position
Wheel rotates, no rotation of crank and piston
Crank rotates amount opposite to wheel rotation
After crank adjustment
After piston adjustment

Pretty simple, right?


Now we want to set up the parenting. Since the wheel is going to be driving the animation, we will set it up as the parent of the crank, which will be the parent of the piston. This part is interesting, so watch closely! Notice the position values for the crank and the piston before the parenting.


Now look at the position values for the crank and piston after we assign the parents.



What the heck just happened? Well, it turns out that the position of a layer is in "comp space" unless the layer has a parent. In that case the position value is in the "layer space" of the PARENT. That means that the child's position is relative to the upper left-hand corner of the parent layer. Why do we care about this? We're going to be calculating the radius of the wheel and the length of the crank so we need to keep this in mind. If we assumed that positions of the layers were relative to the comp (which is normally the case) our calculations will be completely hosed after we do the parenting.

O.K., so what do we do about this? The expression that we're going to develop for the rotation of the crank needs to know the radius of the wheel and the length of the crank. Normally (with no parenting involved) this would be pretty simple. Since the anchor point of the crank is on the edge of the wheel, the formula for the radius of the wheel would be:

r=length(this_comp.layer("wheel").position,position);

And since the anchor point of the crank is at one end of the crank and the anchor point of the piston is at the other end of the crank, we would expect the formula for the length of the crank to be:

l=length(position,this_comp.layer("piston").position);

But as we now know, these formulas won't work. It turns out that we have several options to solve this problem. Key points to remember here: The wheel has no parent so its position is in comp space. Each anchor point is in that layer's own layer space. Positions of children layers are in the layer space of the parent. So the key to making this work is that when you calculate a distance you just need to make sure that both points are in the same space. It turns out that After Effects provides a couple of useful functions to assist us. These are part of the "layer space transform methods". The two that could be useful to us are "to_comp" (which converts from layer space to comp space) and "from_comp" (which - you guessed it - converts from comp space to layer space).

O.K., on to the radius of the wheel. Since we're doing this calculation in the crank layer, we have available the crank's position (which is in the wheel's layer space), the crank's anchor point (which is in the crank's layer space), the wheel's position (which is in comp space) and the wheel's anchor point (which is in the wheel's layer space) to work with. So any of these formulas should work:

r=length(this_comp.layer("wheel").anchor_point,position);

r=length(from_comp(this_comp.layer("wheel").position),anchor_point);

r=length(this_comp.layer("wheel").position,to_comp(anchor_point));

We'll use the first one. This works because the crank's position and the wheel's anchor point are both already in the wheel's layer space. Get it? Alrighty then!

Now for the length of the crank. Since this formula will be used in the crank layer, we have the crank's position (which is in the wheel's layer space), the crank's anchor point (which is in the crank's layer space), the piston's position (which is in the crank's layer space) and the piston's anchor point (which is in the piston's layer space). Because both elements are already in the crank's layer space, the obvious choice in this case is:

l=length(anchor_point,this_comp.layer("piston").position);

So it turns out we didn't need the "layer space transform methods" after all, but it's nice to know about them. If, for example, we needed to create an expression in the piston layer that calculated the distance to the wheel we would need to use one of these transforms.

O.K. math-o-phobes I apologize, but it's going to get a little bumpy for a while. To make this all work, we need to use a little trigonometry (you all remember that from school, right?) For those of you still with me, we'll slog through this as quickly as possible. The following diagram shows the relationships we need to deal with:

where:

A = the angle of rotation of the wheel

B = adjustment angle for the crank

r = radius of the wheel

c = length of the crank


from our trig class we know:

r * sin(A) = d

and

c * sin(B) = d

so

sin(B) = r * sin(A) / c

or,

B = arc sin(r*sin(A) / c)

O.K. - just one more math thingy and we'll be done. For sin and arc sin we're going to be using the JavaScript functions "Math.sin" and "Math.asin". These functions want their angles in radians, not degrees. Since After Effects expresses rotation angles in degrees, not radians, we'll have to do a conversion. Fortunately, this isn't too tough. JavaScript kindly offers us the value of pi as "Math.PI". Since there are 180 degrees in pi radians we can make the following two conversion factors:

rad2deg = 180 / Math.PI; //radians to degrees

deg2rad = Math.PI / 180; //degrees to radians

That's it! Now we have all the pieces necessary to create the expressions for the rotation of the crank and the piston. For the crank we just add the negative of the wheel rotation to the adjustment angle.

rad2deg=180/Math.PI;

deg2rad=Math.PI/180;

r=length(this_comp.layer("wheel").anchor_point,position);

l=length(anchor_point,this_comp.layer("piston").position);

-this_comp.layer("wheel").rotation+
Math.asin(r*Math.sin(this_comp.layer("wheel").rotation*deg2rad)/l)*rad2deg

For the piston's rotation we just use the negative of the adjustment angle.

-1*(this_comp.layer("wheel").rotation+this_comp.layer("crank").rotation)

Here's a screen shot with the complete formulas in place:




Now just add some keyframes for the rotation of the wheel and you're done!

Admittedly, this is a lot of trouble to go through for such a simple example as this. You can, of course, use these techniques for much more complex animations where the payoff would be much greater.

###Dan Ebberts.
If you have any questions or comments, please visit the CreativeCOW.net After Effects forum.




Please visit our forums or view other tutorials at CreativeCOW.net if you found this page from a direct link.


Comments

Re: Parenting and Expressions
by Amy Young
Although this is old it's still useful. The downloadable .aep didn't work for my AE CC2015, but I was able to recreate it with the expressions included in this text and with the AI file (which still opened in AI CC2015 for me.) Thanks much for posting!
Re: Parenting and Expressions
by Eric Sanderson
Hey Dan, not sure if youre on these boards any more but im trying to reverse engineer this to accomplish the opposite, have the angles be driven (with correct relationship) by a moving object. Im on a short deadline to figure it out so thought in the meantime id see if you could explain it to me. Thanks.


Related Articles / Tutorials:
Adobe After Effects
Compositing Secrets Everyone Can Use: Pt. 3 - Alpha Channels

Compositing Secrets Everyone Can Use: Pt. 3 - Alpha Channels

When most people hear the words "alpha channels", they think "transparency", but that's not exactly accurate. The truth is more complex, and a quite bit more interesting! Join longtime VFX artist, editor, software developer, and business owner Simon Ubsdell for Part 3 of the best look behind the technology of compositing that you've ever seen, packed with practical advice for applying the secrets of alpha channels that's simply not possible before understanding these underlying principles. No matter which applications you're using for editing, compositing, or visual effects, this one is a must-see!

Tutorial
Simon Ubsdell
Adobe After Effects
Compositing Secrets Everyone Can Use 2: Advanced Blend Modes

Compositing Secrets Everyone Can Use 2: Advanced Blend Modes

Whether you're a full-time compositor and VFX artist, an editor working in one of the many NLEs that supports Blend Modes (including Adobe Premiere, Apple FCP and FCPX, Avid Media Composer, DaVinci Rsolve, and VEGAS Pro), or whether you just want the additional graphics power that comes with insight into the secret world of pixels, this is the tutorial for you! Join longtime VFX artist, editor, software developer, and business owner Simon Ubsdell for Part 2 of the best look behind the technology of blend modes that you've ever seen, with some practical steps you can start taking today to make your work look better than ever.

Tutorial
Simon Ubsdell
Adobe After Effects
Compositing Secrets Everyone Can Use: Pt. 1 - Blend Modes

Compositing Secrets Everyone Can Use: Pt. 1 - Blend Modes

Join longtime VFX artist, editor, software developer, and business owner Simon Ubsdell for the best look behind the technology of blend modes that you've ever seen. This isn't just for graphics and VFX, but for video editors too -- anyone who puts anything together, and wants to learn more about HOW images combine at the most basic level, in a way that applies to every application you might use, whether Adobe Premiere Pro, Apple FCPX, Avid Media Composer, DaVinci Resolve, VEGAS Pro, and yes, graphics and VFX programs like After Effects, Motion, Fusion, Nuke, Scratch, and many more.

Tutorial
Simon Ubsdell
Adobe After Effects
Disintegration VFX in Adobe After Effects

Disintegration VFX in Adobe After Effects

Join VFX guru Tobias Gleissenberger of Surfaced Studio for a classically energetic Adobe After Effects tutorial combining difference mattes, displacement maps, edge glows, and compositing tricks to dissolve (or explode!) one element in your frame while leaving the rest alone.

Tutorial
Tobias Gleissenberger
Adobe After Effects
Adobe After Effects: Common QT & Export Problems & Solutions

Adobe After Effects: Common QT & Export Problems & Solutions

Problems with Adobe After Effects? Can't import or export QuickTime videos? Exports too big? Missing codecs? AE guru and VFX whiz Tobias Gleissenberger of Surfaced Studio has the answers you're looking for to make every project come to completion more smoothly, including a comparison of exported file sizes for common codecs.

Tutorial
Tobias Gleissenberger
Adobe After Effects
Fancy Title Animations In Adobe After Effects -- FAST!

Fancy Title Animations In Adobe After Effects -- FAST!

Follow along as VFX guru Tobias Gleissenberger shows you how how to quickly create professional-looking animated text elements using the free presets that are included with Adobe After Effects, customizing them to suit your needs. Tobias will also show you how to apply masks to your text layers to have them appear from behind other elements.

Tutorial
Tobias Gleissenberger
Adobe After Effects
Remove People from Moving Video in After Effects with Mocha

Remove People from Moving Video in After Effects with Mocha

Learn how to remove a person from a moving video using the Remove module of the Mocha Pro plug-in for Adobe After Effects! In one of his most epic tutorial's yet, Surfaced Studio's Tobias Gleissenberger then adds a bonus Beam Up effect using some of the great filters from the Boris Continuum and Sapphire collections.

Tutorial
Tobias Gleissenberger
Adobe After Effects
Basic Clone Effect Interaction - After Effects Tutorial

Basic Clone Effect Interaction - After Effects Tutorial

Want to take your CLONE EFFECT to the next level? Want to know how to make your clones INTERACT? Tobias Gleissenberger shows you how.

Tutorial
Tobias Gleissenberger
Adobe After Effects
Adobe After Effects: Glitch Effects Without Using Plug-ins!

Adobe After Effects: Glitch Effects Without Using Plug-ins!

Join VFX guru Tobias Gleissenberger for a fun, high-energy look at how to create all sorts of digital noise, glitch, and other disturbing video effects with any version of Adobe After Effects, without using any plug-ins!

Tutorial
Tobias Gleissenberger
Adobe After Effects
Adobe After Effects Energy Ball

Adobe After Effects Energy Ball

In his latest high-energy Adobe After Effects tutorial, VFX guru Tobias Gleissenberger of Surfaced Studio combines a variety of effects to create the pulsating energy ball, composited with motion tracking, optical flares, and more.

Tutorial
Tobias Gleissenberger
MORE
© 2018 CreativeCOW.net All Rights Reserved
[TOP]