### Index > 3D Forums > Maya Boards > Python

May 15, 2012
Post id: 310060

Hello, I'm having problems converting a quaternion back to a proper Euler rotation and I'm at my wit's end here. So I direly need help!

I'm able to convert this Euler rotation:  19.1255, -67.8468, 55.4967 to this quaternion: <<0.68098956,0.37822461,-0.42286018,0.46302125>> without any problem using the following python code:

def quatRot(x=None, y=None, z=None):
quatRot = []

chr = math.cos(x/2) # Radians of course
shr = math.sin(x/2)
chp = math.cos(y/2)
shp = math.sin(y/2)
chd = math.cos(z/2)
shd = math.sin(z/2)

quatRot.append((chd*chp*chr+shd*shp*shr))
quatRot.append((chd*chp*shr-shd*shp*chr))
quatRot.append((chd*shp*chr+shd*chp*shr))
quatRot.append((shd*chp*chr-chd*shp*shr))

return(quatRot)

But I am incapable of getting back a valid Euler rotation from the very same quaternion. I'm using this code but obviously it's wrong, since it gives me 68.96, 7.097, 106.697. Which doesn't work at all, even when swapping the values around.

def eulerFromQuat(x, y, z, w):
roll  = math.atan2(2*y*w - 2*x*z, 1 - 2*y*y - 2*z*z)
pitch = math.atan2(2*x*w - 2*y*z, 1 - 2*x*x - 2*z*z)
yaw   = math.asin(2*x*y + 2*z*w)

return (math.degrees(roll), math.degrees(pitch), math.degrees(yaw))

Could anyone help me figuring out what's wrong with the code, please?

May 16, 2012
Post id: 310064

What is the rotation order of your euler rotation? (or rather the rotations in maya are Taint-Bryant rotations since if ones really accurate euler only described the rotations that repeat one of the axes such as ZXZ).

>> Which doesn't work at all, even when swapping the values around.

rotation order does not swap values areound it gives you a totally different set of values, as theres a number of different but equal solutions to reach the same position.

PS: why do this at all, i mean maya.OpenMaya has a class called MEulerRotation and MQuaternionion you should be able to just do:

euler = om.MEulerRotation( 19.1255, -67.8468, 55.4967 ) # this assumes xyz order where Z rotate first
quat = euler.asQuternionion()
euler = quat.asEulerRotation()

atleast you can thuis verify that your doing the right thing even if you are developping the tranformation to another app then maya. But then its not a proper question for a maya forum.