Sometimes it would be very handy if the Corner Pin effect worked in three dimensions. It turns out that we can fake it in some situations by applying Corner Pin to a 2D layer and controlling the position of the "pins" by using layer space transforms to project the location of the objects we want to track onto the 2D layer.

The key to the success of this operation will be the application of layer space transforms to convert everything into the correct coordinate system. One key point to keep in mind is that the positions of the "corners" of Corner Pin effect are in the layer's coordinate system. For example, if you apply Corner Pin to a 100 by 100 solid, the coordinates of the Upper Left pin will be [0,0] and the coordinates of the Lower Right pin will be [100, 100]. So our solution needs to result in converting the positions of the target layers from their locations in world space to the 2D coordinate system of the Corner Pin layer. We also want this to work if the target layers are children of other layers.

It turns out we'll need to do this in two steps. The first step is to convert the locations of our target layers from world space to comp space. The best way to do this (a way that works for child layers as well) is to convert the layer's anchor point from layer space to comp space. We do it this way because the anchor point is always represented in the layer's space (relative to the layer's upper left corner), where the layer's position may either be represented in world space (if not a child layer) or the parent's layer space (if the layer is a child of another layer).

The second step is to then convert the target layers' positions from comp space into the layer space of the layer with Corner Pin applied. That's pretty much all there is to it. So the code is only two lines, but there's a lot of math going on under the hood.

target = thisComp.layer("UL");

Using the Corner Pin effect to attach a layer's corners to objects moving in three dimensions.

As a side benefit, it turns out that this expression will maintain the illusion, even if there is camera movement.