![Image](http://www.paul.tuersley.btinternet.co.uk/images/expSwarm01.jpg)
You'll see examples of how to:
use one set of Expression Control effects to control multiple layers.
use the wiggle(), random() and seedRandom() methods.
Rather than getting you to carry out all the steps, I've created an After Effects 7 project that you can use to follow along.
Download ExpressionSwarm.zip
Step 1 - Using the random() expression.
Open the project and the "Step 1" composition. There's a single 10x10 pixel white solid 3D layer called "Drone" with the following expression on Position:
Code: Select all
range = 250;
x = random(-range,range);
y = random(-range,range);
z = random(-range,range);
[x,y,z];
Code: Select all
// create a variable called "range" and assign it the value 250
range = 250;
// create variable "x" and use random(min, max) to assign it a random value between +/-250
x = random(-range,range);
// do the same for variables which I'll call "y" and "z"
y = random(-range,range);
z = random(-range,range);
// create the array of 3 values which is required for a 3D property such as Position
[x,y,z];
Step 2 - Adding a null to control the swarm.
Open the "Step 2" composition. Not that much has changed. I've added a null called Control Null and made it the Parent of the Drone layer.
Select the Drone layer and use Cmd-D to make some duplicates, then drag the Control Null around in the comp window to move the entire swarm.
Step 3 - Using the Slider Control effect.
Open the "Step 3" composition. I want a bit more control over the random placement of my Drone layers, I want to be able to animate the "range" that I'm feeding to the random() method, so that I can make my swarm fly closer together or further apart.
![Image](http://www.paul.tuersley.btinternet.co.uk/images/expSwarm02.jpg)
To do this I applied a Slider Control effect to the Control Null Layer and renamed it "Range". Then I highlighted the value of "250" in the expression and dragged the expression pickwhip over to the "Range" effect's value, which changes the expression to:
Code: Select all
range = thisComp.layer("Control Null").effect("Range")("Slider");
x = random(-range,range);
y = random(-range,range);
z = random(-range,range);
[x,y,z];
![Image](http://www.paul.tuersley.btinternet.co.uk/images/expSwarm03.jpg)
The Slider Control effect is one of a number of Expression Control effects that ship with AE. The effects themselves do nothing, but they can be used to hold animated values which can be accessed by expressions. So now we have a keyframeable value that can be used to control how close together the swarm is.
If you haven't already, now would be a good time to try doing a RAM Preview. You may be wondering what's going on, the layer's are jumping around all over the place. This is because, by default, random() uses time as one of the factors in creating a random value. To solve this we need to use seedRandom().
Step 4 - Using seedRandom() to stop time.
The seedRandom() method does nothing on it's own, but it will affect the result of any random() method applied after it. You have to specify a "seed" (it can be any number, and will be used as a factor in the random calculation) and a boolean value (true or false) to control whether the result is "timeless". i.e. not affected by time. It's this second part that we're most interested in.
Open the "Step 4" composition and you'll see I've changed the expression to:
Code: Select all
range = thisComp.layer("Control Null").effect("Range")("Slider");
seed = thisComp.layer("Control Null").effect("Seed")("Slider");
seedRandom(seed, true);
x = random(-range,range);
y = random(-range,range);
z = random(-range,range);
[x,y,z];
Code: Select all
seed = thisComp.layer("Control Null").effect("Seed")("Slider");
seedRandom(seed, true);
Next, I'm using the seedRandom() method, feeding in the "seed" value from the slider control and telling it that timeless = true, in other words, that the result of random() should not be affected by time.
Again, try creating lots of duplicates of the Drone layer. If you move along the timeline or do a RAM Preview you'll see that the layers no longer jump around. Also, if you alter the value of the Seed effect on the Control Null layer, you can choose a completely different random layout for the swarm. You could even try animating the seed.
Step 5 - Using the wiggle() expression.
Open the "Step 5" composition. The expression has grown a bit bigger with the addition of a wiggle().
Code: Select all
wiggleResult = wiggle(1,100);
range = thisComp.layer("Control Null").effect("Range")("Slider");
seed = thisComp.layer("Control Null").effect("Seed")("Slider");
seedRandom(seed, true);
x = random(-range,range);
y = random(-range,range);
z = random(-range,range);
[x,y,z] + wiggleResult;
Now when you duplicate the Drone layer to create the swarm, you should find that they're actually starting to behave like one.
Step 6 - Slider Controls for wiggle().
In the same way that I'm already using Slider Control effects to feed values for "range" and "seed" into the expression, I though I might as well do the same for the wiggle frequency and amount. Open the "Step 6" comp and you'll find this expression:
Code: Select all
freq = thisComp.layer("Control Null").effect("Wiggle Freq")("Slider");
amount = thisComp.layer("Control Null").effect("Wiggle Amount")("Slider");
wiggleResult = wiggle(freq, amount);
range = thisComp.layer("Control Null").effect("Range")("Slider");
seed = thisComp.layer("Control Null").effect("Seed")("Slider");
seedRandom(seed, true);
x = random(-range,range);
y = random(-range,range);
z = random(-range,range);
[x,y,z] + wiggleResult;
Step 7 - The finished expression
In composition "Step 7" I've just added a few final touches. I've animated the Range effect control to make the swarm expand and contract, and I've added a simple wiggle(0.5, 300) expression to the Control Null's Position to make the whole swarm move around as a group.
Step 8 - Using the swarm expression on existing layers.
I wanted to use this expression to make the individual letters of a piece of text animate in a swarm before returning to their original positions. In the "Step 8" composition I've already created a set of 3D layers, one for each character, and I've pasted in a copy of the Control Null layer. Just a few more things to do:
1. Select all the text character layers and use one of their Parent pickwhips to link them all to the Control Null.
2. Return to the previous "Step 7" composition so that we can copy the expression.
3. Select the Position property on one of the Drone layers that contains the expression and choose Edit > Copy Expression Only from the pulldown menu.
When you use "Copy Expression Only" you can copy/paste an expression without also copy/pasting the original values from the source property. This means the text layers are more likely to stay in roughly their original positions.
4. Return to the "Step 8" comp, select all of the text layers and paste (Cmd-V)
And that's it, do a RAM Preview to check out the result. It should look about the same as in the "Step 9" composition.
Some Tips:
1. As shown in that last example, you need to animate both the Range and Wiggle Amount values down to zero in order for the layers to regain their original formation.
2. Instead of applying the expression to existing layers, you could replace the Drone layer with a precomp made up of 1 frame duration still images, arranged as a sequence. Then, enable Time Remapping and add the following expression:
Code: Select all
seedRandom(1, true);
random(0,1);