LIBRARY: Tutorials Reviews Interviews Editorials Features Business Authors RSS Feed

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.

Comments

Re: A better Grid using expressions
by Jerry Witt
Yeah I gotta agree. This tutorial neglects to mention that you need to lock the effects panel from the Grid comp so you can pick whip to the controllers. Which got me past where David got stuck. But then the code doesn't work. THEN Mylenium tells one to "clone the layer"!!! What... It is possible to do this without cloning the layer I am certain.

Seriously, two thumbs way down for this tutorial.

Re: A better Grid using expressions
by David Scott
Hi Mylenium
Thanks for taking the time to post this tutorial.
I've start to work through it and got stuck...
You don't say how to be able to see the Controller layer effects in the Grid Comp while also revealing the scale and rotation of the Line Comp at the same time. Click on one and the other disappears... So I'm presuming you want me to add the 2 Line Comps to Grid Comp? (But you don't say to do that).
OK so now I've pickwhiped Rotation to the Tilt slider and it works fine, but which slider do I pickwhip the Scale to? I'm presuming it's Line Thickness, but it also adjusts the length as well as the width of the line...? So I'm guessing it's only the x axis which get's pickwhipped... not sure how to that...?!
Any additional help greatly received!
I like the idea of what you are trying to demonstrate and want to understand it, so I'd be grateful if you could please fill in the missing steps.
Many thanks

David Scott,
Senior Editor,
GOD TV (UK)
Re: A better Grid using expressions
by Matthew Galvin
This is one of the most incomplete tutorials that I have seen on Creative Cow. Did you actually try to follow your own instructions?


Related Articles / Tutorials:
Adobe After Effects Expressions
Adobe After Effects Expressions 101

Adobe After Effects Expressions 101

Expressions in Adobe After Effects open up a world of possibilities for your visual effects! Expressions can be daunting when you first get into them, though, as you have to essentially write 'code' - and code can be scary. Join After Effects guru Tobias Gleissenberger of Surfaced Studio for the first in a series covering expressions, from the very basics - all the way through to programming the Matrix!

Tutorial
Tobias Gleissenberger
Adobe After Effects Expressions
In The Loop - A Killer Expression

In The Loop - A Killer Expression
  Play Video
A detailed explanation and demonstration of the uses of the loop expression in After Effects. Examining the loopOut(), loopIn(), loopInDuration(), loopOutDuration() commands and their "ping pong", "cycle" and "continue" modes.

Tutorial, Video Tutorial
Matthew Tompkins
Adobe After Effects Expressions
EQ Animation using AE Expressions

EQ Animation using AE Expressions
  Play Video
In this video tutorial Matt Tompkins gives some great tips on how to create and animate a rack of Graphic Equalizer bars using solely expressions in Adobe After Effects.

Tutorial, Video Tutorial
Matthew Tompkins
Adobe After Effects Expressions
Jack of All Trades, Master of Expressions

Jack of All Trades, Master of Expressions
  Play Video
In this video tutorial, Creative Cow contributing editor Eran Stern demonstrates how to create a jumpy and squishy animation using the power of expressions.

Tutorial, Video Tutorial
Eran Stern
Adobe After Effects Expressions
AE Quick Tips #4: Copy Expression Only

AE Quick Tips #4: Copy Expression Only
  Play Video
In this video tutorial, CreativeCow leader Aharon Rabinowitz shows you a menu item for speeding up the process of copying expressions to other layers, without typing or altering the current values.

Tutorial, Video Tutorial
Aharon Rabinowitz
Adobe After Effects Expressions
Animating Text Reflections with ExpressionsAnimating Text Reflections with Expressions

In this tutorial from Creative COW Contributing Editor Trent Armstrong, you'll see how to use Expressions to animate reflected light across text. You can get great results using only ingredients included free with AE, including track mattes and the AE Camera.

Tutorial
Trent Armstrong
Adobe After Effects Expressions
Swinging A Realistic ChainSwinging A Realistic Chain

Creative COW member Trent Armstrong shows you expression features that apply all the way from After Effects 5 to today. He combines his experience as an average expressions user with tips and shortcuts for many core features that every AE user will be able to start using today.

Tutorial
Trent Armstrong
Adobe After Effects Expressions
Building and Animating a CG SnakeBuilding and Animating a CG Snake
  Play Video
In this video tutorial from CreativeCOW.net you will learn how to create a dynamic animation based on a chain rig. You will also learn how to animate one layer using keyframes, expressions, or both, and make other layers follow the leading layer. The rest of the tutorial is about texturing and making the final look.

Tutorial, Video Tutorial
Jerzy Drozda Jr
Adobe After Effects Expressions
Using Expressions to Create a Volume MeterUsing Expressions to Create a Volume Meter
  Play Video
In this video tutorial from CreativeCOW.net you will learn how a smart way of creating dynamic a sound reactive Volume Meter using Expressions in After Effects.

Tutorial, Video Tutorial
Jerzy Drozda Jr
Adobe After Effects Expressions
After Effects Expressions Simplified

After Effects Expressions Simplified

In this After Effects tutorial from The Creative COW Magazine, see through the confusion of After Effects Expressions with just a few simple steps.

Tutorial
Andrew Kramer
MORE
© 2016 CreativeCOW.net All Rights Reserved
[TOP]