3D Swarm tutorial

Understand the code
Post Reply
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

This tutorial shows an expression that can be applied to multiple layers to create a 3D swarm effect.

Image

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];
As I'm planning on having lots of layers in my "swarm", I need some way to randomly place each layer in 3D space. In this expression I'm using random(min, max) to create three random numbers for the X, Y and Z Position values. Here's a more descriptive version of the expression;

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];
Select the layer and repeatedly press Cmd-D to duplicate it, until you end up with about 10 layers. You'll see that each layer has been positioned randomly in 3D space.


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

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];
As before, select the Drone layer and make quite a few duplicates. Now try scrubbing the Range effect's value and the layers will move closer or further apart.

Image

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];
The two new lines are:

Code: Select all

seed = thisComp.layer("Control Null").effect("Seed")("Slider");
seedRandom(seed, true);
I've added another Slider Control effect called "Seed" to the Control Null layer. In the same way as I did with "range", I've created a variable called "seed" and used the expression pickwhip to link it to the new Slider Control effect.

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;
wiggle(frequency, amount) is probably the most useful of all expressions, providing an easy way to make properties change over time. In this example "wiggle(1,100)" I'm varying the Drone's Position once every second by up to 100 pixels. I'm storing the result in a variable called "wiggleResult" and adding it to the result of the random methods at the end.

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;
As you can see, I've created two new Slider Effects (Wiggle Freq and Wiggle Amount) and have linked them to variables "freq" and "amount" which are then used in the wiggle().


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);
Create lots of duplicates, and the result should be that each Drone displays a random frame selected from 0-1 seconds in the precomp.
nab
Posts: 203
Joined: November 29th, 2005, 3:00 am
Location: Royan
Contact:

Nice tutorial Paul !

I would add as a remark (or in the Tips section), that the random() method may also take an array or a pair of arrays as argument; it's sometimes handy to write the random stuff in a single line.

for instance

Code: Select all

min = -250;
max = 250;
random([min,min,min],[max,max,max]);
dsp_418
Posts: 10
Joined: December 5th, 2005, 6:53 am

Nice!
I'm wondering if there's a way to adapt it to rig a camera to wiggle the x,y,z independently.
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

Looking back at this tutorial, I'd just like to point out that if anyone's looking to create 3D wiggling/swarming text, a much easier way is to just use a "Wiggly Selector" on a text layer with "Per-character 3D" enabled.

Anyway, if you want to wiggle the x,y,z values for a 3d property (such as camera position) independently, you could use an expression like:

Code: Select all

theX = wiggle(1, 20)[0];
theY = wiggle(1, 10)[1];
[theX,theY,value[2]];
(In that example I'm only actually wiggling the x and y values, while keeping the original z value.)

If you wanted to keyframe the amount of wiggle, you could create a Null, add three Slider Control effects and use an expression like this:

Code: Select all

theX = wiggle(1, thisComp.layer("Null 1").effect("Slider Control X")("Slider"))[0];
theY = wiggle(1, thisComp.layer("Null 1").effect("Slider Control Y")("Slider"))[1];
theZ = wiggle(1, thisComp.layer("Null 1").effect("Slider Control Z")("Slider"))[2];
[theX, theY, theZ];
Also, I've recently posted a script that helps you create your own custom wiggle expressions (and smooth and loop, but not random....for now). Here's the link: Auto Express - automatic expressions
dsp_418
Posts: 10
Joined: December 5th, 2005, 6:53 am

That's great!
thanks Paul
pecijackson
Posts: 8
Joined: January 31st, 2012, 12:23 am

Hi there, i'm currently work for a web design company, and i recently asked to make a promotional video with effect like this,i'm not influence with flash ,and i thank you very much for this tutorial. thanks sir :)
sshahriyari
Posts: 30
Joined: January 16th, 2017, 11:59 pm
Contact:

Paul Tuersley wrote: March 20th, 2007, 10:16 am

This tutorial shows an expression that can be applied to multiple layers to create a 3D swarm effect.

Image

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

download link is dead! :(

tiertiga
Posts: 1
Joined: July 11th, 2020, 9:30 am
Contact:

Yeah, the download link is dead

visualneocortex
Posts: 1
Joined: October 9th, 2021, 4:44 am

By any chance can anybody reload projects zip??

Post Reply