The Expressions Cheat Sheet
Loading...

Create a new empty comp and add a new layer. Then apply this expression to its Position property:

var x = thisLayer.index / (thisComp.numLayers+1) * thisComp.width;
[x, value[1]];

Now as you duplicate the layer multiple times you will see how all layers automatically adjust their x position to create an evenly spaced row.

Congratulations, you're a wizard Harry. Now let's break it down:

thisLayer.index / (thisComp.numLayers+1); // gives us a number between 0 and 1

We divide the current index of the layer with the number of layers of the comp. Notice though that we bumped the number of layers up by one.
That's because we want to keep things centered. The minimum index of a layer is 1. When we divide 1 by 2, we get 0.5, which is exactly half.
This scales up as we add more layers.

You can try omitting the one and see what happens.
Anyway, after having a number between 0 and 1, we multiply it by the comp width:

thisLayer.index / (thisComp.numLayers+1) * thisComp.width; // gives us a number between 0 and the width of the comp

And finally, we assign it to a variable named x, and use the original y value from the property.

//var x = thisLayer.index / (thisComp.numLayers+1) * thisComp.width;
[x, value[1]];

Let's add some colors

I have cleared the comp and copied the Position expression we wrote above, but this time to a shape layer with a fill.
Now let's apply the following expression to the fill:

var blue = [0,0,1, 1];
var red = [1,0,0, 1];
var progress = thisLayer.index / (thisComp.numLayers + 1);
linear(progress, blue,red);

Now duplicate the layer again and look at that:

We are using the same technique of dividing the current index by the number of layers in this comp to get a value between 0 and 1.
Once we have that value, we can use the linear() function to blend between our two arrays of colors progressively. How cool is that?

Caveats

These kinds of rigging examples using the numLayers property work best in isolated comps. If you were to add any extra layer to the comp we just built in the example above, you might screw up your entire logic.
However, used correctly numLayers can be pretty powerful and allow you to create some magical procedural systems that are easy to change and modify later on.