Expression Shorts Auto Center Layers

March 19th 2012 08:00:18am

NEW UPDATED CODE (Works for 2D and 3D):

point1 = thisComp.layer("Point 1").transform.position;
point2 = thisComp.layer("Point 2").transform.position;
div(add(point1, point2), 2);

Ever have the need to have a layer stay exactly at the midpoint between two other moving layers? Sometimes your layers have to move erratically, or even linear, but you just want your midpoint layer to just stay in the middle without doing math and hand keyframing it the whole way. Well this expression should help you out. Includes a 2D and 3D layer explanation.
The need for something like this may not always be apparent or even used very often, but when it does come up, you definitely wish you had an automated way to do it.

2D SOURCE CODE:

x1 = thisComp.layer("Point 1").transform.position[0];
y1 = thisComp.layer("Point 1").transform.position[1];
x2 = thisComp.layer("Point 2").transform.position[0];
y2 = thisComp.layer("Point 2").transform.position[1];
xResults = (x1 + x2) / 2;
yResults = (y1 + y2) / 2;
[xResults, yResults];

NOTE: The above expression is meant to be placed on the Position property of your 2D layer.

3D SOURCE CODE:

x1 = thisComp.layer("3D Point 1").transform.position[0];
y1 = thisComp.layer("3D Point 1").transform.position[1];
x2 = thisComp.layer("3D Point 2").transform.position[0];
y2 = thisComp.layer("3D Point 2").transform.position[1];
z1 = thisComp.layer("3D Point 1").transform.position[2];
z2 = thisComp.layer("3D Point 2").transform.position[2];
xR = (x1 + x2) / 2;
yR = (y1 + y2) / 2;
zR = (z1 + z2) / 2;
[xR, yR, zR];

NOTE: The above expression is meant to be placed on the Position property of your 3D layer.

This weeks expression doesn’t involve any new special code to learn, just good old fashion simple math and some variables to organize it all. Emphasis on simple, so continue on down to the breakdowns to better understand what each line of code is doing.

2D CODE BREAKDOWN:

x1 = thisComp.layer("Point 1").transform.position[0];
y1 = thisComp.layer("Point 1").transform.position[1];
x2 = thisComp.layer("Point 2").transform.position[0];
y2 = thisComp.layer("Point 2").transform.position[1];
xResults = (x1 + x2) / 2;
yResults = (y1 + y2) / 2;
[xResults, yResults];

Line 1: We need to simplify all of the values we are using in this expression, so this first line starts it off by taking the X Position value of our first 2D point. This layer is called “Point 1”, and we create a variable called x1 and assign it the X Position value of “Point 1”.

x1 = thisComp.layer("Point 1").transform.position[0];

Line 2: We then create a variable called y1 and assign it the Y Position value of the “Point 1” layer.

y1 = thisComp.layer("Point 1").transform.position[1];

Line 3: Continuing on, we now have to do the same for our second point, a layer called “Point 2”. We create a variable called x2 and assign it the X Position value of “Point 2”.

x2 = thisComp.layer("Point 2").transform.position[0];

Line 4: Yet again we create a new variable called y2 and assign it the Y Position value from “Point 2”. Seeing a pattern.

y2 = thisComp.layer("Point 2").transform.position[1];

Line 5: So for this line we get to start doing the math part. First we create a variable called xResults, we then assign it the final X mid point value. To do this we need to take x1 and add it to x2, our X values from “Point 1” and “Point 2”. We then encase that equation in parenthesis like so, (x1 + x2). Next we divide that total value by 2, (x1 + x2) / 2. It’s that simple.

xResults = (x1 + x2) / 2;

Line 6: We literally duplicate what we did in line 5, only we make it for the Y values. So the variable is called yResults and we use y1 and y2 for our equation.

yResults = (y1 + y2) / 2;

Line 7: Finally we return our resulting array and encase it in brackets, [ ]. We use the xResults variable as our first array item, representing the X value, then we add the yResults variable for our second item, which is our Y value. You should now be good to go.

[xResults, yResults];

3D CODE BREAKDOWN:

x1 = thisComp.layer("3D Point 1").transform.position[0];
y1 = thisComp.layer("3D Point 1").transform.position[1];
x2 = thisComp.layer("3D Point 2").transform.position[0];
y2 = thisComp.layer("3D Point 2").transform.position[1];
z1 = thisComp.layer("3D Point 1").transform.position[2];
z2 = thisComp.layer("3D Point 2").transform.position[2];
xR = (x1 + x2) / 2;
yR = (y1 + y2) / 2;
zR = (z1 + z2) / 2;
[xR, yR, zR];

Note: The 3D breakdown is nearly identical to the 2D breakdown, so I will cover the major changes only below. The biggest being that our source layers are called “3D Point 1” and “3D Point 2” instead of “Point 1” and “Point 2”.

Line 5: This line is where are adding our Z position information. We continue the naming convention by using z1 as this variable name and assign it the Z Position value of our “3D Point 1” layer.

z1 = thisComp.layer("3D Point 1").transform.position[2];

Line 6: We now add another variable and call it z2. This variable gets assigned the Z position value of our “3D Point 2” layer.

z2 = thisComp.layer("3D Point 2").transform.position[2];

Line 7: We do the same math equation for our X value. The only difference here is I name the variable xR instead of xResults. It was just easier to type a shorter name.

xR = (x1 + x2) / 2;

Line 8: Same goes for the Y value, I used yR for the variable name.

yR = (y1 + y2) / 2;

Line 9: We then create a zR variable to hold the midpoint Z Position value. The same math equation applies here as well. Adding z1 to z2 then dividing the total by 2.

zR = (z1 + z2) / 2;

Line 10: Finally our resulting array, that now adds the additional Z value. We place the zR variable in the third item slot as this is our Z value.

[xR, yR, zR];

Hope this all made sense. Having this kind of code can be useful and save you a bunch of headaches trying to manually place a layer at the midpoint of two other layers. Making After Effects do the math for you can allow you to focus on other more pressing issues within a project. See you next week.

Checkout more:
After Effects ExtendScript Training: Ep. 17 Part 3
After Effects ExtendScript Training: Ep. 5
Random Number Grid After Effects Template
Houdini | FFX Geometry Explainer
Expression Shorts Swinging Motion Part 2