Aligning an object's rotation to that of a vector.

2,858 views
Skip to first unread message

Adam Miels

unread,
Aug 25, 2010, 2:44:50 AM8/25/10
to python_in...@googlegroups.com
Hi All.

Not really a question specific to Python in Maya, but I am trying to
write this in python, and know that there are quite a few experienced
maya people on the list.

I was wondering if there was possibly a better way to do this.

I am querying a point on a surface, to get a normal vector, then
trying to use this vector to orient an objects rotation matrix so that
it points in the direction of surface.
I have written my own method to apply the transform as .setTransform
in pymel doesn't seem to work:

def mySetMatrix(anObject, aMatrix):

   select(anObject)

   xform(m=(aMatrix.a00,aMatrix.a01,aMatrix.a02,aMatrix.a03, \

   aMatrix.a10,aMatrix.a11,aMatrix.a12,aMatrix.a13, \

   aMatrix.a20,aMatrix.a21,aMatrix.a22,aMatrix.a23, \

   aMatrix.a30,aMatrix.a31,aMatrix.a32,aMatrix.a33))

I query the point on the surface with:

normalVector = aMesh.getClosestNormal(aPoint, 'world')[0]

and then use the X,Y,Z values of this vector to calculate the
following matrices:

rotate on x axis:
where X = atan2(Y,Z)

              1        0        0        0

              0 cosX -sinX 0

0 sinX cosX 0

0 0 0 1


rotate on y axis:
where Y = atan2(X,Z)

            cosY     0      sinY     0

              0 1 0 0

-sinY 0 cosX 0

0 0 0 1


rotate on z axis:
where Z = atan2(Y,X)

            cosZ -sinZ      0        0

            sinZ cosZ 0 0

0 0 1 0

0 0 0 1

and then assigning the resulting matrix (original Matrx) X (X Matrix)
X (Y Matrix) X (Z Matrix) to the original object.

Although this approach seems to make sense to me on paper, when I
implement it, I get matrices that end up scaling the object instead of
only affecting its rotations, and they point in the wrong directions.

I was wondering if anyone could point me in the right direction with
this one (literally :P ) ?

Regards,

Adam Miels.

Chad Vernon

unread,
Aug 25, 2010, 2:54:20 AM8/25/10
to python_in...@googlegroups.com
You need more than one vector to set an orientation since there are infinite orientations that can spin around the normal.  You need at least 2 non-parallel vectors to create an orientation.

If you have a normal vector n and a tangent vector t, you can create an orthonormal basis by:

x = n ^ t
t = x ^ n

Then you can pick which axis is which for your matrix:
x.x  x.y  x.z  0
n.x  n.y  n.z  0
t.x   t.y   t.z  0
0      0     0   1

Chad


Adam Miels

unread,
Aug 25, 2010, 3:23:31 AM8/25/10
to python_in...@googlegroups.com
Hi Chad,

Thanks for your reply.

Is the ^ representing to the power of?

I am not really worried about the rotation around the normal at this
stage, so can I use the getFaceVertexBinormal to get the tangent?

I am not sure what the . in the matrix you drew represents, is this a
dot product?

Regards,

Adam.

> --
> http://groups.google.com/group/python_inside_maya

Chad Vernon

unread,
Aug 25, 2010, 3:34:39 AM8/25/10
to python_in...@googlegroups.com
The ^ is a cross product.  n, t, and x are MVectors which are part of the Maya API. So, n.x, n.y, n.z are just the 3 components of the vector n.  You should be able to use either the binormal or the tangent.  You could also even use an adjacent vertex.


Adam Mechtley

unread,
Aug 25, 2010, 10:09:49 AM8/25/10
to python_in...@googlegroups.com
Hi Adam,

This short video basically visualizes Chad's explanation, if it is helpful

http://adammechtley.com/tutorials/rigging/aim-constraints/

Adam Miels

unread,
Aug 25, 2010, 7:23:16 PM8/25/10
to python_in...@googlegroups.com
Hi Adam.

Thanks for the link to your video.

This did help explain the need to calculate the cross product twice.

Now to implement it :)

/adam.

> --
> http://groups.google.com/group/python_inside_maya
>

Reply all
Reply to author
Forward
0 new messages