Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions get global rotation / orientation of a child object

  • get global rotation / orientation of a child object

    Posted by Ben Rollason on May 26, 2009 at 5:01 pm

    Hi there,

    Can anyone tell me how to find the global rotation of a child object…

    I’m thinking something along the lines of childObj.toWorld(childObj.transform.anchorPoint); but for rotation and orientation.

    Does that exist, or is there perhaps a more long winded workaround?

    Thanks if you can help.

    Ben.

    http://www.benrollason.com

    Dan Ebberts replied 1 year ago 4 Members · 9 Replies
  • 9 Replies
  • Dan Ebberts

    May 26, 2009 at 6:06 pm

    It’s more long-winded. Try this:

    C = thisComp.layer(“child”);
    u = C.toWorldVec([1,0,0]);
    v = C.toWorldVec([0,1,0]);
    w = C.toWorldVec([0,0,1]);

    sinb = clamp(w[0],-1,1);
    b = Math.asin(sinb);
    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)]

    Dan

  • Ben Rollason

    May 26, 2009 at 8:20 pm

    Dan, you really are the maestro! You have just saved me from something significantly longer winded.

    I can’t say I really understand the ins and outs of the code yet, but I’d be really interested to know roughly what principles it’s based on. I’m also keen to find out exactly what the difference is between toWorld and toWorldVec. The After Effects help isn’t especially enlightening on this subject.

    Thanks for your help.

    Ben.

    http://www.benrollason.com

  • Ben Rollason

    May 27, 2009 at 6:27 pm

    I’d just like to add something about the script for the record…

    It stops working if the scale of the layer in question is anything other than [100,100,100]. I’ve found that can be fixed by changing the unit vectors in the script. For instance, if the scale of the layer is [200,237,200], the script still works if the first lines read…

    C = thisComp.layer(“REFLECTED”);

    u = C.toWorldVec([2,0,0]);
    v = C.toWorldVec([0,2.37,0]);
    w = C.toWorldVec([0,0,2]);

    This is only for the child object so far. I haven’t checked it with various parents up the line that have various scales… I’ll report back!

    Ben.

    http://www.benrollason.com

  • Dan Ebberts

    May 27, 2009 at 6:47 pm

    The scale doesn’t have to be [100,100,100], it just needs to be uniform scale (same scale value in all dimensions). I have a version kicking around somewhere that works for non-uniform scale, but it sounds like you cracked it already.

    Dan

  • Ben Rollason

    May 28, 2009 at 11:05 am

    Hi Dan,

    No I haven’t cracked it… that little trick only works for the child object. I’m trying to get my head round your code here.

    Am I right in saying u,v,w is a rotation matrix and the trigonometry is a matrix to euler conversion?

    I you do find the code that also deals with scaling, that would be very useful – I think I’m reaching the limit of my abilities here!

    Best,

    Ben.

    http://www.benrollason.com

  • Dan Ebberts

    May 28, 2009 at 1:57 pm

    I think all you need to do for the scaling issue is normalize the unit vectors like this:

    C = thisComp.layer(“child”);
    u = normalize(C.toWorldVec([1,0,0]));
    v = normalize(C.toWorldVec([0,1,0]));
    w = normalize(C.toWorldVec([0,0,1]));

    sinb = clamp(w[0],-1,1);
    b = Math.asin(sinb);
    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)]

    It was so long ago that I don’t really remember how I came up with this. I may have just stumbled onto the math somewhere. If I generated it, it surely started with a rotation transform matrix. I really need to bone up on that stuff. 🙂

    Dan

  • Ferenc Van der velde

    August 7, 2020 at 8:45 pm

    Thanks Dan, this script is still going strong in 2020!

  • Xavier Bonet

    September 14, 2023 at 8:59 pm

    Hello, Dan,

    Although the OP doesn’t specify, I’m assuming your response is for 3D layers, correct? (Which is why I’m having an error come up using it on a 2D layer.) How could it be adapted to use on a 2D layer? I’m trying several edits here and there but none are working.

    EDIT: Sorry, a few minutes after asking I finally found another post (which I hadn’t seen before, promise, although I’ve been searching for a while) where, again, Dan, you offer this simple expression for 2D Layers:

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

    September 15, 2023 at 12:10 am

    Here’s another way to do it:

    L = thisComp.layer("child");
    v = L.toCompVec([1,0,0]);
    radiansToDegrees(Math.atan2(v[1],v[0]));

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy