Expression Shorts Attach 2D Layer To 3D Layer

March 12th 2012 08:00:45am

This a fun useful expression that allows you to take any 2D layer in your timeline and have it attach to a moving 3D layer. Ideal for something like 2D flat text that follows a 3D tracked point in a scene.
Not much to hype up here before the video, so onto the video you go and the code breakdown as always is below.

SOURCE CODE:

thisComp.layer("3D Point").toComp([0, 0, 0]);

SOURCE CODE VARIANT:

thisComp.layer("3D Point").toComp([thisComp.layer("3D Point").width/2, thisComp.layer("3D Point").height/2, 0]);

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

So this week we get to learn yet another new snippet of expression code and this one is SUPER simple and very useful, yet a lot of people are unaware that it even exists.

toComp() – The toComp() expression allows you to do some complicated math translations from a three dimensional point to a two dimensional point. It even has a built-in way to offset from that point too. it only requires one argument, which is a three item vector array like so, [0, 0, 0]. What this array is doing is telling After Effects where the X, Y, and Z coordinates are located in relationship to the boundary area of the 3D layer you reference.

The easiest way to understand this is by using a square or rectangular 3D layer like I did in the video tutorial. Doing so will give you a 1 to 1 visual of where the boundary of the layer is and allow you to better see how the vector array affects your 2D layer location. If you were to take a look at your comp viewer and show the comp Rulers, you will see a pixel count starting at 0 in the top left corner that goes to the width of your comp in the top right corner.

You will also see the same pixel count running along the left edge also starting at 0 in the top left and stoping at your comp height in the bottom left corner. You can think of these as a coordinate system. So if you were to connect two points, one for your width at say 100 and one for your height at say, 300, you would have a single point on your screen located 100 pixels from the left side of your comp viewer and 300 pixels down from the top of your comp viewer.

The same concept applies to the vector array that you give to the toComp() expression. So saying toComp([100, 300, 0]), will place the Position point of your 2D layer 100 pixels in from the left edge of your 3D layer and 300 pixels down from the top edge. The third value is for the Z position and will offset it as well. By simply using the vector array, [0, 0, 0], your 2D layer will attach to the top left corner of the 3D layer, but what if you wanted it to be centered on the 3D layer? Well, we would simply grab the width of the 3D layer and divide by 2. Same for the height. The variant source code above does just that.

CODE BREAKDOWN:

thisComp.layer("3D Point").toComp([0, 0, 0]);

Line 1: This simple one line of code is placed on the Position property of your 2D layer. It references the 3D layer that you want to attach to, thisComp.layer(“3D Point”). In this example, my 3D layer is called “3D Point”. We then add the toComp() expression and give it the coordinate vector array, toComp([0, 0, 0]). This then will subsequently attach our 2D layer to the upper left corner of the 3D layer’s boundary.

thisComp.layer("3D Point").toComp([0, 0, 0]);

VARIANT CODE BREAKDOWN:

thisComp.layer("3D Point").toComp([thisComp.layer("3D Point").width/2, thisComp.layer("3D Point").height/2, 0]);

Line 1: For this variant of the expression, we use all the same code as above in the original breakdown, but we change the first two items in our vector array, [thisComp.layer(“3D Point”).width/2, thisComp.layer(“3D Point”).height/2, 0]. The first item is our X position and we make it the center or midpoint of our 3D layer by getting the 3D layer’s width and dividing by 2, thisComp.layer(“3D Point”).width / 2. We then do the same for the second item, our Y position, thisComp.layer(“3D Point”).height / 2 and we can leave our Z position at 0. By doing this we can be sure that our 2D layer will always stay in the center of the 3D layer no matter what sizes it appears to be in the frame.

thisComp.layer("3D Point").toComp([thisComp.layer("3D Point").width/2, thisComp.layer("3D Point").height/2, 0]);

As always, thanks so much for all the great feedback on this series and the requests for more episodes. I’m glad to hear that they are useful to you. See you next week.

Checkout more:
Expression Shorts Sample Images Part 1
Cinema4D Attach Splines To Animated Objects
Houdini | FFX Geometry Explainer
After Effects ExtendScript Training: Ep. 10
Houdini Mardini Beach