How to convert three dimensional vector to an Eule

2019-06-13 23:13发布

问题:

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.

回答1:

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)


回答2:

Using constraints as @theodox says is perfectly valid as is using xform. Just providing some alternate info (see postscript below for even more info).

However, Maya has a node called decomposeMatrix, that will do the conversion for you if that's the only thing your after. Maya API also has a function for the decomposition:

import maya.OpenMaya as om

util = om.MScriptUtil()

mat = om.MMatrix()
util.createMatrixFromList(
    [
    1., 0., 0., 0., 
    0., 1., 0., 0.,
    0., 0., 1., 0.,
    0., 0., 0., 1.
    ], mat)
rot = om.MEulerRotation.decompose(mat, om.MEulerRotation.kXYZ)

# result in radians which in this are is known but my example is just a placeholder
print rot.x, rot.y, rot.z

You should prefer nodes tough since they would update when your need updates unless your doing static things that never animate or your doing a exporter.

Anyway you might some day do this outside Maya, or with different composition orders for scale and shear example then i would point you to Christoph Gohlke's homepage, and see Transformations.py and transformations.c they provide a lot of information on needed matrix math and references to original sources, such as in this case Ken Shoemakes excellent analysis on rotations.

PS: This is a near duplicate, it contains also some info on the matrix math just like @theodox's answer.



回答3:

I'd suggest using NumPy or equivalent libraries for the math, as they are highly optimized and implemented various functions and operators.

To solve matrix and vector multiplication in python, lets assume:

  • Vectors are 1-D lists, and treated as column vectors
  • Matrices are 2-D lists, where m[R][C] indicates element on the Rth row and Cth column

Then the product of rotation_matrix * some_vector is

[
  sum([m * v for m, v in zip(m_column, some_vector)]) 
      for m_column in rotation_matrix
]

Some handy links: zip - manual, Python List Comprehension