Pymel 有 quaterions 的包装类和 matrices和 euler rotations
因此:
import pymel.core.datatypes as dt
quat = dt.Quaternion(.707, 0, 0, .707)
print quat.asEulerRotation()
# dt.EulerRotation([1.57079632679, -0.0, 0.0], unit='radians')
print quat.asMatrix()
# dt.Matrix([[1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, -1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0]])
您也可以在没有 Pymel 的情况下直接访问底层 API 类,尽管这有点烦人,因为您需要讨厌的 MScriptUtil 来提供 double 值
def APIQuat(*iterable):
'''
return an iterable as an OpenMaya MQuaternion
'''
opt = None
if isinstance(iterable, OpenMaya.MQuaternion):
opt = iterable
else:
assert len(iterable) == 4, "argument to APIQuat must have 3 or 4 entries"
it = list(copy(iterable))
v_util = OpenMaya.MScriptUtil()
v_util.createFromDouble(it[0], it[1], it[2], it[3])
opt = OpenMaya.MQuaternion(v_util.asDoublePtr())
opt.normalizeIt()
return opt
更新
现在使用 2.0 版本的 api 就容易多了:
from maya.api.OpenMaya import MQuaternion, MEulerRotation
import math
q = MQuaternion (.707, 0, .707, 0)
q.normalizeIt() # to normalize
print q
print q.asEulerRotation()
# (0.707107, 0, 0.707107, 0)
# (-3.14159, -1.5708, 0, kXYZ)
# note that EulerAngles are in radians!
e = MEulerRotation (math.radians(45) ,math.radians(60), math.radians(90), MEulerRotation.kXYZ)
print e
print e.asQuaternion()
# (0.785398, 1.0472, 1.5708, kXYZ)
# (-0.092296, 0.560986, 0.430459, 0.701057)
我是一名优秀的程序员,十分优秀!