Creative COW SIGN IN :: SPONSORS :: ABOUT US :: CONTACT US
LIBRARY: TutorialsVideo TutorialsReviewsInterviewsEditorialsFeaturesBusinessAuthorsRSS FeedTraining DVDs

A better Grid using expressions

COW Library : Mylenium : A better Grid using expressions
A better Grid using expressions
A CreativeCOW.net Adobe After Effects Tutorial


A Better Grid Using Expressions

Mylenium
Mylenium
http://www.mylenium.de
Germany

©Creativecow.net. All rights are reserved.

Article Focus:
This tutorial from Mylenium will show you some basic principles when using expressions in After Effects. These include using the pick whip and some simple things you need to think of when referencing elements and converting numerical values.

Download your project files here. Both files for versions 6.5 and 5.5 are in the zipped folder.


Step 1: Setting up the project

This first step requires some forward thinking and planning but is needed if you do not want to run into trouble later. We are going to link multiple items from different compositions so we need those right before doing anything else. After Effects cannot detect if you move things from one composition to another at a later point. If you do so, you may end up with broken expressions and all their benefits would be lost.

Create a new composition at the desired size. For mine I used the standard PAL with square pixels. Since this is going to be our main composition name it Grid or something like that. Now create two other compositions with the exact same settings as the first one. Name them Lines vertical and Lines horizontal. Your timeline window should look like this:



I suggest you hide all unnecessary columns in the timeline. This will give you more screen space to work with. Do so by moving the mouse over the column title and right-clicking on it. Then simply choose Hide.



Step 2: Making a controller

In your Grid composition create a new layer and name this layer Controller. Its size and color are of no importance. We are going to hide it anyway. It is only meant to function as a layer on which we can add our expression controls.


If you like, you may also use a Null object instead of a color solid. This does not affect the functionality of this project. I do not use Null objects because their visual feedback is so poor.


Now add some expression controls to your Controller. They can be found under Effect -> Expression Controls. We need 5 sliders. Rename each slider (select its name in the effects palette and hit Return to type in a new name). Name them Line thickness, Spacing vertical, Spacing horizontal, Tilt vertical and Tilt horizontal. You should have something like this:




Note that all sliders already have assigned a value other than zero. This is important for multiple reasons. For one you would not be able to see something - a line thickness of 0 pixels would leave everything invisible as well as a spacing of 0 would put all the lines in the same place. Also this helps to avoid a mathematical problem: division by zero. Since division by zero is illegal (it is undefined), After Effects would disable your expressions if it detected an equation which produces such results. So save yourself some headache and set the sliders to something other than the default value. Later on we are going to add some simple code to make our setup even more fool-proof.


For the Tilt controls we could also have used the angle control. This does not have any advantage, it's just another way to represent data for angles. I do not use the angle control at all in my projects - I can get finer adjustments with sliders. Like the use of solids vs. Null layers this is more a personal preference rather than a serious reason.


Step 3: Preparing the line layer

This one is simple. In the Vertical lines composition create a new solid. Make its width 100 pixels and its height 1000 pixels and name it Line. The solid must be higher than the composition so it still extends over the edge of the comp when it is tilted. In the Horizontal lines composition create a solid of 100 pixels height and 1200 pixels width.

You should leave your layers white since you can always colorize your resulting grid with other means. At this point having everything in yet another sub-comp may seem unnecessary, but this will give you much greater flexibility later on - instead of modifying multiple solids all you need to do is tweak it once in your nested composition and the rest will update automatically.


Why 100 pixels height/ width? We are going to link this value to the line thickness using the scale parameter. To simplify calculations we will assume that 100 % scale is equal to 100 pixels. That's why we use 100 or multiples of hundred for those values.


Step 4: Linking rotation and scale

Make sure that the effects controls of your Controller layer in your Grid composition are visible. In order to assign an expression to a channel in After Effects you need to tell it where you want that expression. Reveal the rotation and scale properties of your Line layer in the Lines vertical composition. Alt + click on the stop watch symbol.

This will activate expressions for that property and you should see an = symbol as well as 3 other symbols appear. The stopwatch itself will remain disabled since we do not need normal keyframes (some expressions can use this feature but we don't need it). Now choose the pickwhip symbol and click-drag towards one of the sliders in the effects palette.



After Effects will automatically insert a line of code into the text field of the timeline. It should read something like this for the rotation property of a vertical line.

comp("Grid").layer("Controller").effect("Tilt vertical")("Slider")
Use a similar approach for the scale property. Do the same for the horizontal Line in the other composition.


Step 5: Calculating the positions of the lines

Since a grid consists of multiple lines, we have to find a way of calculating the position of each line without further manual interference. In the beginning of this tutorial we created sliders for the spacing of the lines which we are now going to use. This is where the real power of expressions comes into play. First of all activate the expressions for the position property.

Since we want to do some more complex math, we cannot solely rely on linking parameters. To save us some typing effort later on we first create 2 custom variables. Type in Spacing= in the text field and then use the pick whip to select the Spacing horizontal slider of your Controller layer. Create another variable named Start. Complete the line until your code reads as follows

Spacing=comp("Grid").layer("Controller").effect("Spacing horizontal")("Slider");
Start=thisComp.width/2;
As hinted in step 2, we are going to use some other basic expression code to make sure we don't get error messages related to invalid mathematical calculations. The basic usage is as follows:
clamp(minimum value, maximum value, user value)
This simple construct will cut off any values that are not within the range specified so they will be ignored by the expression code. This can be put to good use to limit your values so your lines stay inside the comp window, avoid flipovers with rotations or avoid funny results when incidentally scaling layers in the negative range. After modification, your lines should look like this:
Spacing=clamp(0.001,10000,comp("Grid").layer("Controller").effect("Spacing horizontal")("Slider"));
Start=thisComp.width/2;
As well as defining the spacing we have already calculated the center of our composition. We need that, since we are going to build our grid from the middle outward. Why? This way we can easily create the illusion of zooming into our grid in a consistent way without being afraid of our lines drifting out of sight or loosing quality due to scaling things up too much. Now type in the rest of our expression:
if (index%2 == 0)
{myPosition=Start-index*Spacing}
else
{myPosition=Start+index*Spacing-Spacing};
[myPosition,thisComp.height/2]
So what do we have here? This is a conditional statement that will allow us to automatically distribute the lines simply by cloning our layer. In the first line we are taking a look at the layers number in the timeline called index. We then use the modulus operator % to decide whether it is an odd or even number.

The rule is: If we divide our index by 2 and the remainder of this division is zero, then it is an even number. For that case we tell After Effects to take this layer and calculate its position in the negative direction (towards the left edge) from the center of the composition. For everything else we want After Effects to calculate the position in the positive direction (towards the right edge) of the comp. The myPosition is simply another variable we declared on the fly.

The last line is the correct way to tell After Effects which values to use and that the expression is finished at this point. We use myPosition and half the height of the composition. Now go ahead and duplicate your layer several times. You will see your cloned layers snap into position automatically. Use the same expression on the horizontal Line but don't for get to swap height and width.


Step 6: Expanding the technique

So far we have only re-created a part of the functionality of the standard Grid effect and added the ability to create parallelogram shapes instead of just rectangles. That's not bad, but still not exciting, either. However, since we have real layers that are independent from each other, we can apply a plethora of effects. I for instance used a Basic Text effect to create a dotted line.

Another variation is using the Advanced Lightning effect to create some kind of moving weave pattern. In both cases the Scale parameter can be omitted since the look of the lines should be controlled by the effects settings themselves. in After Effects 6 and up the new text layer functions give you even more to play with. In this case, a simple text layer with some basic brackets, a few animators and another simple expression was used to create something that looks like a tapestry or quilting pattern. If you added animators to the scale property, you could easily create a grid of pulsing energy bursts.

Just let your imagination run wild.

Feel free to ask questions regarding this tutorial in the After Effects forum at Creativecow.net


Please visit our forums at Creativecow.net if you found this page from a direct link.

  Digg it Digg itAdd Comment


Related Articles / Tutorials:
New AE DVDs for the Cow Master Series. Save Now!Motion StabilizingA Walk in the Park: Tracking & ExpressionsAE Quick Tips #4: Copy Expression OnlyCreating Advanced Glitter using Expressions with Trapcode Particular


Recent Articles / Tutorials:
Native P2 Editing in Final Cut Pro with MXF4MacBleach Bypass EffectLighting with ShadowsSimple MorphBuilding a Cube World: Part 4
MORE


FORUMSTUTORIALSMAGAZINEDVDsBOOKSPODCASTSEVENTSSERVICESNEWSLETTERNEWSBLOGS

© CreativeCOW.net All rights are reserved.

[Top]