ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

3D Camera facing expressions

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
high1memo3D Camera facing expressions
by on Sep 4, 2007 at 6:35:42 pm

Hi All, I initially posted this in the standard AE forums without realizing there was a seperate container for expressions, so I'm closing that and carrying on here.
The original thread is at

To summarize I would like my 3D layers to
1. Always remain fully parallel to my camera
2. OR Always remain parallel to my camera while staying perpendicular to the floor (only rotate around Y axis).

The Auto-orient mode/expressions don't work because they POINT the layer to the camera, so if the camera is close to the layer the layer rotates a lot, and thus distorts (more details in the original thread) - I need the layer normal to be parallel to the camera normal, NOT pointing to the camera.

Searching these forums and the web, I've seen lots of people ask the same question but with no answer, so I'm posting my results so far which works good enough for what i need, but not fool proof so if anyone wants to improve they are more than welcome.

I'll start with Q2 first (the easier one, only rotate around Y).

paste this to the ORIENTATION:
camZ = normalize( thisComp.activeCamera.transform.position - thisComp.activeCamera.transform.pointOfInterest );
camX = normalize( cross(camZ, [0,1,0]) );
[0, radiansToDegrees(Math.atan2(-camX[2],camX[0])), 0];

In the original thread I mentioned putting this on the Y-Rotation which also works, but then doesnt work with the full camera facing mode (must be a rotation order thing).

Q1. (full camera facing) paste this to X-Rotation (in addition to above)
camZ = normalize( thisComp.activeCamera.transform.position - thisComp.activeCamera.transform.pointOfInterest );
-radiansToDegrees(Math.atan2(camZ[1], camZ[2]));

Like I said this isnt fool proof. If something has parents it may not work I dunno, this is my first AFX expression, so any experts who see potential pitfalls feel free to fix it. Also probably wont work if the camera is looking completely straight down. And finally, this doesnt take into consideration any camera orientation. I usually never use camera rotations but only use camera position and anchor-point. Moreover I usually import camera animation from 3dsmax or cinema4d which also just imports keyframes for position and anchor. So if you are using nulls to control camera or anchor, or animating actual camera rotation then the expressions would need to be modified for that.

This is such a fundamental thing for anyone doing any form of navigating 3D sets that I can't believe its not built into AE like the auto-orient thing is!

Return to posts index

pxlguyRe: 3D Camera facing expressions
by on Sep 11, 2007 at 2:28:28 pm

Sorry you haven't gotten your answer yet! Here is a great link for a great page on a great site with even more information than you were asking for.

Good Luck!

Return to posts index

high1memoRe: 3D Camera facing expressions
by on Sep 11, 2007 at 3:18:21 pm

Hi Trent, that is indeed a great site and I have learnt a lot from it. In this case however, I could not find an answer to my question there. The page you've linked to seems very similar to what I'm asking, but actually it is exactly what I am trying to avoid. The expression on that page is for orienting the layer towards the camera - i.e. points the layer to the camera. So when the camera is close to the layer there is a lot of perspective deformation. What I am looking for is to keep the layer parallel to the screen (either on all 3 axis, or staying perpendicular to the floor - i.e. only rotating around Y).

I've seen a lot of people ask about this (and I even found an old post of mine from years ago when I'm asking how to do the same thing in combustion!).

To show the problem I've taken a capture from aftereffects of my project (if it doenst come up for some reason, its at .

The expression I've come up with for rotating around Y only (shown in my post above) works perfectly for me now (thats the blue layer). Like I say, it probably wont work if linked to other layers and stuff, but I dont have time to figure that out right now.

The expression I came up with for full camera facing works not perfectly (with some camera angles it goes bonkers) - but for my current project its fine so I"m not working on it anymore. Probably in the future I will need a foolproof version as well. I'm just working with two axis rotation angles. the correct solution I guess would involve setting up a rotation matrix from the camera x,y,z axes; and then extracting the 3 orientation values. Like I said, I've seen a lot of posts with people asking how to do that, so if anyone would like to tackle it, it would be great!!

Return to posts index

Dan EbbertsRe: 3D Camera facing expressions
by on Sep 12, 2007 at 3:49:36 pm

I'm not sure if this is what you're after, but it should give a layer the same orientation as the camera:

C = thisComp.activeCamera;

V = C.toWorldVec([0,0,1]);
P = toWorld(anchorPoint);
lookAt(P, P + V);


Return to posts index

high1memoRe: 3D Camera facing expressions
by on Sep 12, 2007 at 5:44:54 pm

Yup thats perfect thanks! I cant believe how simple it is! I guess I need to look more into built-in functions like toWorldVec and lookAt to simplify things!

For the camera-facing around Y, would you advise that I keep my solution above, or use something like:
YRotation =
C = thisComp.activeCamera;
V = C.toWorldVec([0,0,1]);
P = toWorld(anchorPoint);
O = lookAt(P, P + V);
i.e. It probably is more foolproof?

P.S. slightly off-topic, how can I save these expressions to my presets?

*EDIT* Ignore the last part about saving presets. found that, its dead easy...

Return to posts index

Dan EbbertsRe: 3D Camera facing expressions
by on Sep 12, 2007 at 7:02:01 pm

I'd give this a try:

C = thisComp.activeCamera;

V = C.toWorldVec([1,0,0]);
-radiansToDegrees(Math.atan2(V[2], V[0]))


Return to posts index

high1memoRe: 3D Camera facing expressions
by on Sep 14, 2007 at 10:49:05 am

yup works perfectly thanks!

Return to posts index

Dave LarsonRe: 3D Camera facing expressions
by on Aug 12, 2016 at 10:11:50 pm

Hey guys, I'm having trouble getting this script to work. I have a lazy susan set-up with products around the outside.
A central null creates the rotation, and the product layers are parented to the null. I've pasted both sets of code (1 at a time) into my orientation field of the products and it doesn't work. Is this because I'm using a null with parented layers in this scenario?
Here's a still:

-Dave Larson
3D and 2D Motion Graphics Design & Visual Effects

Return to posts index

Dan EbbertsRe: 3D Camera facing expressions
by on Aug 13, 2016 at 1:53:21 am

Try this in the y rotation property:

delta = anchorPoint - fromWorld(thisComp.activeCamera.toWorld([0,0,0]));
value +radiansToDegrees(Math.atan2(delta[0],delta[2]))


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 All Rights Reserved