ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Track the rotation of a child layer.

FAQ   •   VIEW ALL
 Track the rotation of a child layer. on Jan 31, 2011 at 1:35:12 am

Hello all.

I know how to track the position of a child layer, using this expression:
```c=thisComp.layer("Child"); c.toWorld(c.transform.anchorPoint)```
And that works great. But how to track the rotation too? The rotation of child layer is unchanged, but it rotates following the Parent layer, so I need the result of that rotation in relationship to the 3D space.

Any help? Thanks!

 Re: Track the rotation of a child layer.on Feb 1, 2011 at 1:08:00 pm

Well, I found the solution in another post. This Dan's expression can track the orientation of a child layer, just paste it in the Orientation attribute:

```L = thisComp.layer("Child Layer"); s = L.scale/100; u = L.toWorldVec([s[0],0,0]); v = L.toWorldVec([0,s[1],0]); w = L.toWorldVec([0,0,s[2]]); sinb = clamp(w[0],-1,1); b = Math.asin(sinb/thisComp.pixelAspect); cosb = Math.cos(b); if (Math.abs(cosb) > .0005){ c = -Math.atan2(v[0],u[0]); a = -Math.atan2(w[1],w[2]); }else{ a = Math.atan2(u[1],v[1]); c = 0; } [radiansToDegrees(a),radiansToDegrees(b),radiansToDegrees(c)]```

In fact, I wanted to track a parented camera from a comp to another, creating a clone camera from the main comp camera, so I can animate just the main camera. But the expression "c=thisComp.layer("Child"); c.toWorld(c.anchorPoint)" needs the Anchor Point attribute, so it doesn't work with cameras. I used a Null layer and parented that to the original camera, setting all the Null's transformations to 0. So I used this two expressions in the clone cameras to track the Null's Position and Orientation in 3D space, now all the cameras in pre comps is following the original camera in original comp. We need to set the auto-orientation of the clone cameras to off, so it will be oriented just by the expression.

It's a kind of parent constraint that we have in Maya, so we can use many pre comps using the same camera.

Maybe there's an easy way, but at least it worked for me.

Hope it helps someone else. Cya.

 Re: Track the rotation of a child layer.on Feb 1, 2011 at 3:27:15 pm

With cameras, lights, or nulls, you can just use

L.toWorld([0,0,0]);

Dan

 Re: Track the rotation of a child layer.on Feb 1, 2011 at 5:50:07 pm

Lol, at least I tried my best... =P
Thanks for that Dan!

It is just for the Position right? Is there an easiest way for the Orientation too? Maybe Rotation? Cya!

 Re: Track the rotation of a child layer.on Feb 1, 2011 at 8:27:28 pm

Unfortunately, the layer space transforms (toWorld, etc.) only give you positions. You have to do some math to get orientation/rotation. You could split the orientation expression that you used into 3 separate expressions for rotation.

Dan

 Re: Track the rotation of a child layer.on Feb 12, 2015 at 12:44:48 am

I was about to make a post about how this wasn't working for me, but I found out that if you mess with the scale values, it'll throw off the calculations.
Not a big deal in my case, but any way we can create an expression that is scale-independent?

Now I just need to figure out how to convert the generated orientation values back into rotation values....

 Re: Track the rotation of a child layer.on Jun 1, 2012 at 9:43:36 pm

if there is just one level of parenting, like A parented to B, you could just take A's coordinates by toWorld() or toComp() and B's rotation.

but the real problem starts when there are more levels, like A parented to B, parented to C... parented to N. Cause then we would have to get every layer rotation.

I'm looking instead of doing this massive process, just get the A's rotation by doing something like toWorld() that returns the relative value of that property.

Thanks

 Re: Track the rotation of a child layer.on Jun 4, 2012 at 1:15:18 pm

Well, actually i found a solution walking to work(and thinking) this morning, and it was quite simple.

I've added an Angle Control Effect to the target which i want to get the real rotation, and added the code below.

If you want to put it on a 3D space, just make 3 copies of the Angle Control Effect and change ".rotation" to ".orientation[0]", ".orientation[1]", ".orientation[2]".

Basically it checks for every parent and sums up its rotations.

```result = effect("Angle Control")("Angle") + transform.rotation; parentStack = []; inheritance = true; if(thisLayer.hasParent) { parentStack.push(thisLayer.parent); while(inheritance) { if(parentStack[parentStack.length-1].hasParent) { parentStack.push(parentStack[parentStack.length-1].parent); }else{ inheritance = false; } } for(var i = 0, l = parentStack.length ; i<l;i++) { result += parentStack[i].rotation; } } result```

 Re: Track the rotation of a child layer.on Jun 4, 2012 at 6:19:03 pm

To get the world rotation of a parented 2D layer is pretty straight-forward (I think this is essentially what you came up with):

``` L = thisComp.layer("child"); r = L.rotation; while(L.hasParent){ r += L.parent.rotation; L = L.parent; } r ```

I don't think it's going to work correctly in 3D though, because the angles interact with each other. I think you'd have to end up using a 3D rotation transform.

Dan

 Re: Track the rotation of a child layer.on Mar 7, 2018 at 5:17:14 pm

Hi!

Just wanted to quickly update this subject. I have a problem w Dan's expression for getting the 3D orientation of a child null. At first it worked great. But now I try to execute the same thing with different layers in the same composition and the Y orientation behaves crazy.

Let's say the child null I follow is called NULLCHILD, his parent is called NULLPARENT, and the layer that follows the child null is called FOLLOWER.

When I change the X or Z orientation of my NULLCHILD, the FOLLOWER follows the null's orientation normally. But when I change the Y orientation value, it follows in a very strange way, also messing up the X and Z values. So I change the Y orientation in a NULLCHILD like this:

From 0 to 8.7 degrees, the FOLLOWER's Y goes from 0 to 90. Then when I change further, from 8.7 to 171 - nothing changes, it stays on 90. After that, from 172 to 188, the Y changes to 270, and the X and Z changes to 180, and the Y goes from 77 to 306 and at the end changes to 270, and X and Z change to 0. Now, from 189 to 352, everything stays the same. And from 352 to 360, the Y also goes from 270 to 360. I have absolutely no idea where those values come from...

As I wrote, it is the same composition, the same camera, only in a different position. When investigating it looks like the script is working ok untill I parent the NULLCHILD to the NULLPARENT. After that it goes crazy. Any clues?