Skip to main content
françois leroy
Inspiring
March 27, 2013
Answered

Full auto-orient via expression

  • March 27, 2013
  • 2 replies
  • 13804 views

Hi!

I'd like a 3D Null to get the orientation of 3D a layer. (without parenting, it would be too easy )

I've tried a lot of solutions, through orientation, rotations, both combined... some results are close, but never perfect.

If I'm right, orientation is computed before rotations...

I think the simplest way would be with auto-orientation, or the lookAt() expression on orientation, wich gives me correct X and Y orientations, and then get an expression on zRotation to get a "full" orientation.

But what would it be?

How can I get the Z orientation in layer space?

Any idea will be appreciated!

François

Correct answer Dan Ebberts

Try this oreientation expression:

L = thisComp.layer("other layer");

u = fromWorldVec(L.toWorldVec([1,0,0]));

v = fromWorldVec(L.toWorldVec([0,1,0]));

w = normalize(fromWorldVec(L.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 = (sinb < 0 ? -1 : 1)*Math.atan2(u[1],v[1]);

  c = 0;

}

[radiansToDegrees(a),radiansToDegrees(b),radiansToDegrees(c)]

Dan

2 replies

synthetick
Community Expert
Community Expert
January 19, 2025

This thread has helped me a lot but I'm stuck on something. I'm trying to set up expressions for an animation of an office chair. I have a 3D null called "null - direction" which controls the path of the chair, and it is set to auto-orient along the path. But I wanted the wheels to have a slight delay so that when the chair turns a corner the wheels take a little bit of extra time to rotate into that orientation.

In order to do this I made a null called "null - getOrientation" and I used the expressions in this thread to get the orientation of the chair, and then I used another null called "null - delay" to delay the orientation by half a second. The orientation of the wheels is pick-whipped to the orientation of "null - delay"

It all seems to work ok at the first corner but when we get to the second corner the chair is turning clockwise but "null - getOrientation" and "null - delay" start turning anticlockwise and we end up with wheels not oriented to the path. I'm attaching a video which I hope shows what I mean. On further investigation it seems to be occurring when the value of sinb reaches the top or bottom of the sine wave curve ie. at 1 or -1.

françois leroy
Inspiring
March 27, 2013

It seems I asked a bit quickly...

Here's the solution in case somebody needs it:

for orientation:

a = thisComp.layer("master").toWorld([0,0,0]);

b = thisComp.layer("master").toWorld([0,0,100]);

lookAt(a,b)

then for zRotation:

a = fromWorld(thisComp.layer("master").toWorld([0,0,0]));

b = fromWorld(thisComp.layer("master").toWorld([100,0,0]));

c = a-b;

radiansToDegrees(Math.atan2(c[1],c[0]))+180;

Hope it can help!

François

françois leroy
Inspiring
March 28, 2013

Aaaaargh !

Nope, it's not perfect, even if it's close to the result...

Does anyone have a solution?

Dan?

Thanx.

Dan Ebberts
Community Expert
Community Expert
April 2, 2013

Sorry, here it is:

https://www.dropbox.com/s/o61chbg7x5eg4bp/for_dan.aep

François


So are you talking about when you turn the Master's Scale expression on? As I recall, the orientation expression is derived from a simplified 3D rotation transform the assumes uniform scale. I think the correction I put in for non-uniform scaling is probably not correct/adequate and the original derivation of the expression needs to be revisited with non-uniform scaling in mind. I've got my original notes somewhere, but it could be a chunk of work and I'm not sure I have the time to dive into it.

Dan