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. ðŸ™‚ Continue on to the video to find out how.

**DISCLAIMER:** For readers that copy and paste the following Expression code below, be aware that double quotes are currently showing as smart quotes. This is a PVC article default that I cannot override at the moment. I am looking for a workaround, but in the meantime you will need to make sure you paste the Expression code into a blank simple text document, NOT a rich text one, first to convert all code to simple text, then copy that and paste that into your Expression field in After Effects. Use TextEdit on Mac or NotePad on PC for this. If you paste directly into the Expression field without this step, you will end up keeping the smart quote formatting and it will cause a syntax error when you try to run the Expression. Just a heads up.

**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:

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

2) y1 = thisComp.layer(“Point 1”).transform.position[1];

3) x2 = thisComp.layer(“Point 2”).transform.position[0];

4) y2 = thisComp.layer(“Point 2”).transform.position[1];

5) xResults = (x1 + x2) / 2;

6) yResults = (y1 + y2) / 2;

7) [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:

1) x1 = thisComp.layer(“3D Point 1”).transform.position[0];

2) y1 = thisComp.layer(“3D Point 1”).transform.position[1];

3) x2 = thisComp.layer(“3D Point 2”).transform.position[0];

4) y2 = thisComp.layer(“3D Point 2”).transform.position[1];

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

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

7) xR = (x1 + x2) / 2;

8) yR = (y1 + y2) / 2;

9) zR = (z1 + z2) / 2;

10) [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.

### Filmtools

Filmmakers go-to destination for pre-production, production & post production equipment!

Shop Now