There is a given three-dimensional vector.
I want to convert this vector to a rotation which could make one of local axis (like X axis) of that cube to match the vector's direction.
I prepared two pictures. But I can't paste them because of my reputation. I am a freshman.
I tried so many times but still can't get it to work. I think this may involve the rotation order of the cube when I rotate the cube.
解决方案
You can probably get what you want using an aim constraint. If you want to do it as a one-time operation (not having the constraint active in your scene, you can just do:
lc = cmds.aimConstraint( 'your_object_here', 'target_object_here')
cmds.delete(*lc)
However, if you want to this mathematically in code you can do it without euler angles. Eulers are hard to construct because there are many euler combinations that are equally valid for any given orientation. It's easier to do it by directly setting the matrix of the object so it's local axes point the way you want.
In maya, a matrix works likes this:
Xx Xy Xz 0
Yx Yy Yz 0
Zx Zy Zz 0
Tx Ty Tz 1
where
(Xx, Xy, Xz) is the local X vector of the matrix
(Yx, Yy, Yz) is the local Y vector of the matrix
(Zx, Zy, Zz) is the local Z vector of the matrix
(Tx, Ty, Tz) is the translation of the matrix
Scale, if any, is encoded in the size of the vectors; for a matrix at scale 1 and no rotation, the vectors are normalized so the matrix would be
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
You can make a matrix using Pymel like so, with 4 tuples representing the rows:
import pymel.core as pm
# in practice these vectors would reflect the orientation you want
new_mat = pm.core.matrix (
( 1, 0, 0, 0),
( 0, 1, 0, 0),
( 0, 0, 1, 0),
( 0, 0, 0, 1)
)
And apply it to an object using the xform command:
pm.xform(my_object, matrix = new_mat)