There have recently been several tutorials listed here on creativecow.net that have started teaching the basics of expressions. I've also seen some really advanced tutorials like the manual lens flare with some really advanced expressions. We'll I'm an average expressions user that is trying to figure out how to build expressions to work like plugins with easy controls that do powerful things.
In this example I'm going to set up a chain that starts swinging and then appears to be effected by gravity until it comes to a stop. At the end of the tute we'll be able to control the amplitude of the swing, the speed of the swing, and the gravity or how quickly the chain stops swinging. And with many new tutorials coming out that are made specifically for AE CS3, I thought it might be fun to do something that can work in versions of AE at least back to version 5.0.
Step 1 - Start by creating a new project and saving it. I'm calling mine "Swinging Chain."
Step 2 - Create two folders named " FINAL" and "PreComps". There is a space in front of " FINAL" so it rises to the top of the list.
NOTE: We're not creating a lot of comps in this project, as a good organizational practice, I always create the same few folders. I would normally add an "Assets" folder or "Movies" and "Stills" depending on the needs of the project. This way when I open it back up or give it to someone else, it is easy to decipher.
CREATING THE LINK
Step1 - (Actual Stuff) Select the PreComps folder and create a new Comp (HOTKEY = cmd+n on Mac, ctl+n on Windows).
Name the comp "Chain Link" and make it 100px wide and 100px tall.
Step 2 - Create a new solid (HOTKEY = cmd+y on Mac, ctl+y on Windows). Name the solid "Bead", click the button for make "Make Comp Size", change the color to a solid Black, and click OK.
Step 3 - Duplicate the Bead solid, select the top layer, and select the circular mask tool from the tool palette.
Now, double-click on the circular mask tool to apply a circular mask to the full limit of the solid on Layer 1.
Step 4 - Open the Position and Anchor Point layer transform properties for the "Bead" layer. (HOTKEYS = P and A) Click the second number, the Y value, for Position and set it to 100. Then Click the second number for the AnchorPoint (Y value) and set it to 100. This keps the layer in place and puts the Anchor Point at the edge of the layer and the comp. It should look like this:
Step 5 - Select Layer 2, name it "Chain", and unlink the scale properties. Set X to 70 and set Y to 90. Then reveal the Anchor Point and Position properties.
Set them to the following values:
Anchor Point - 50, 0
Position - 50, 0
Your comp window should look like this:
SETTING UP THE CHAIN BASE
Now that the Link is built, we need to set up the chain. The next few steps will involove expressions, but we should be able to keep it pretty painless.
Step 1 - Select the " FINAL" folder in the project window and create a new composition named "Chain Swing". It should be at least 640x480, square pixels, but I'm creating a 720p comp. 1280x720, square pixels.
Step 2 - Create a Null Object named "Effect Controls." You can create a Null with Layer> New> Null Object. A NULL is a blank layer that can be used to aid in controlling a number of different aspects of a composition. In this example I'm only using it as a place to put my effect slider controls.
Step 3 - Select the "Effect Controls" NULL and Select Effect> Expressions Controls> Slider Control from the Effect menu. Select the newly applied slider and hit RETURN to change the name of the Slider Control to "Swing Amplitude."
Step 4 - Duplicate the Slider Control enough times to have the following individual controls and set each to the corresponding value:
• Swing Amplitude (already created) - 13
• Swing Speed - 15
• Time Decay - 1.5
• Link Rotation - 1.01
• Link Size - 75
Step 4 - Drag the "Chain Link" comp into the "Chain Swing" comp. Select the "Chain Link" layer, hit RETURN and name it "Static Link" then set the following values:
Anchor Point X=50, Y=0
Position X=640, Y=0
Scale X=20, Y=20
It's not very exciting yet, but you should see something that resembles this:
EXTENDING THE CHAIN
This composition called "Chain Swing" is meant to be dropped into another composition as a complete object so the "Static Link" will always stay where it is and the subsequent links will be positioned relative to that starting point.
Step 1 - Duplicate the "Static Link" layer, move to the bottom, and rename it to "Link".
Step 2 - Parent the "Link" layer to the "Static Link" with the Pick Whip.
Step 3 - Twirl down the Position property and option+click (alt+click on Windows) the Stop Watch next to Position.
Doing this adds an expression to the Position property. With the Expression Field still selected enter the following text:
x = 50;
y = 97;
Hitting the ENTER key will apply the expression.
This gives the "Link" layer a constant relative position to the "Static Link". Even if the Position property is changed accidentally, this will keep the "Link" locked to the "Static Link".
Step 4 - Create the next link by duplicating the "Link" layer. After Effects will auto name the new layer "Link 2."
Step 5 - Repeat Step 4 for as many layers as you need until your chain is long enough. I created 16 Links to have a good length on the chain and every layer is parented to the layer immediately above it.
As you can see, all the links have spaced themselves correctly using the expression for Position.
i = index - 1;
linkRotation = thisComp.layer("Effect Controls").effect("Link Rotation")("Slider");
thisComp.layer(i).rotation * linkRotation
This expression takes the Rotation of the previous layer and multiplies it times the value of the Link Rotation slider control. The result is that there is a slight increase in rotation per link as the math moves down the chain.
Step 3 - Select the Rotation property of the "Link" layer and select Edit> Copy Expression Only from the menu bar. Then SHIFT select all of the links from "Link 2" to your last link and Paste the expression by hitting cmd+v on the Mac or ctl+v on Windows. This will apply the rotation expression to each of the selected layers.
Finally, it's time for a RAM Preview. Hit the 0 (zero) key on the number pad and watch the chain swing away. Turning on Motion Blur will add to the realism.
For further control we have the ability to change the size of the Links. Part of the process is already in place. We just need to hook up the expressions.
Step 1 - Open the "Chain Link" composition and pull the timeline tab off so you can see the timelines for both "Chain Link" and "Chain Swing."
Step 2 - Twirl down the scale properties for each of the layers in the "Chain Link" comp and reveal the expression field for each.
Step 3 - For the "Bead" layer copy and paste the following expression.
x = comp("Chain Swing").layer("Effect Controls").effect("Link Size")("Slider");
This applies the same value from the "Link Size" slider control to both the X and Y Scale of the "Bead" layer.
Step 4 - Copy and Paste the following expression to the Scale property of "Chain" in the "Chain Link" comp.
x = comp("Chain Swing").layer("Effect Controls").effect("Link Size")("Slider");
y = 90;
This applies the value from the "Link Size" slider control to the X Scale and keeps the Y Scale at 90% for "Chain".
When you adjust the "Link Size" slider control, the links stay together, but the beads get smaller.
Step 5 (Previously Missing) - It's time to make the chain swing! Twirl down the rotation property and opt (or alt) click the stopwatch to reveal the expression entry window. Then copy and paste in the following expression:
amplitude = thisComp.layer("Effect Controls").effect("Swing Amplitude")("Slider");
decay = thisComp.layer("Effect Controls").effect("Time Decay")("Slider");
swingSpeed = thisComp.layer("Effect Controls").effect("Swing Speed")("Slider");
amplitude * Math.sin(swingSpeed * time)/Math.exp(time * decay)
In this expression, "amplitude" references the "Amplitude Slider" to give you a range to start with. I like to go big but not too big on this one. "13" is a good number to start with.
"Decay" is how quickly the chain gives in to our artificial gravity. You can decrease the swing time by increasing the "Time Decay" slider.
"Swing Speed" can be thought to refer to the mass of the individual links. In the real world a large string of links will have a nice full swing but swing slower than a string of smaller links. To get that kind of result, set "Swing Speed" to 8 and "Time Decay" to .5.
The math at the end of the expression takes all those variables and applies them with an exponential decay to have the chain appear to be reacting to actual gravity and wind resistance.
I hope this can be a springboard to launch you into trying out more expressions on your own.
Feel free to discuss this tutorial in the Adobe After Effects Expressions Forum at CreativeCOW.net.
Trent Armstrong is a motion graphic designer and video producer for Igniter Media Group in Dallas, TX. He also is the producer and host of The MannersCast, a podcast about everyday manners for everyday folks. More of Trent's work can be seen at www.ignitermedia.com, and The MannersCast is at http://www.mannerscast.com.