Pymel has wrapper classes for quaterions and matrices and euler rotations
hence:
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]])
You can also get direct access to the underlying API classes without Pymel, though it's kind of annoying because you need the icky MScriptUtil to provide double values
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
Update
This is all much easier nowadays using the 2.0 version of the 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)