[Closed] Rotation Rotation Rotation
Hi all,
I have around 10,000 tree mapped planes which are to be aligned to camera, (they are already haphazardly distributed). I know I can use look at constraint but wish to do it by scripting.
They are to be rearranged to look towards camera.
Tried script
rot_face = eulerangles 0 0 77.5
for o in selection do rotate o rot_face
entering the angle to at axes (z)
But this script only works on few selected planes,
I want my script to work on any selected object (whatever angle it may be previously at)
what’s wrong with this script. Please guide me
thanks in advance
I didn’t understand why not to use lookAt for this, I think its perfect for the job.
How ever, this is how I would do it:
(
local tar = $Camera01 -- lookat target
-- loop through selected objects and build up a lookat transformation matrix
for obj in selection where obj != tar do (
-- Z axis will be used to point at the target
local theZ = normalize (obj.pos - tar.pos)
-- Y axis will be used as an up axis
local theY = [0,0,1]
-- finding the X axis
local theX = normalize (cross theZ theY)
-- ortogonizing the Z axis
local theZ = normalize (cross theX theY)
-- asigning the new transformation matrix
obj.transform = matrix3 theX theY theZ obj.pos
)
)
Thank you for TzMtN for the script. it is absolutely working.
Let me explain why I didn’t want to apply lookat constraint, the camera keeps changing according to clients wish. some times the client wants totally new camera then reassigning to the new camera is not an easy job (my feeling).
once again Thanks for the script.
If u have time can u please explain the script in depth (the matrix part)
About using LookAt constraint, you can pick a dummy as your lookAt target
and than simply align and link it to whatever camera you want.
About the Matrix thingy, the basic idea is that every object in a 3d scene has a transformation matrix which holds it's position rotation and scale in a matrix 3 format.
the matrix looks like this:
matrix3 row1 row2 row3 row4
every row holds a point3 value.
row1 represents the x vector of the object
row2 represents the y vector of the object
row3 represents the z vector of the object
row4 represents the position of the object in space.
If we look at an axis vector, the length of the vector will be the scale of the object on that axis, while the direction will be it’s rotation on this axis.
If we want the object to look normal and not skewed or stretched, we have to make sure that every axis is perpendicular to the other two and that every vector is normalized, so that it’s length will equal to 1.
So what I basically did is to calculate a vector that points at the camera, normalize it to make sure its length is 1, added another vector that points up so that the tree or whatever will always stand up, and than calculate the third axis by using the cross product of the first two and than recalculate the first one to make sure everything is ortogonized.
To better understand this you can try positioning the camera right above the object and see what happens. The problem with this is that the first two vectors we calculate will both point up (they will both look like that [0,0,1])
if you’ll try this
cross [0,0,1] [0,0,1]
the result will be [0,0,0] which is bad for us because it doesn’t say where to point the third axis. in other words we get a gimbal lock. The same thing happens with lookAt by the way.