I need to do the opposite of you – import rotations from Maya into Unity.
Update: SOLUTION FOUND … see below under Update.
(Note: This code here does not work properly. See below for working code.)
var qRot : Quaternion = Quaternion.Euler(mayaRotation); // Convert Vector3 output from Maya to Quaternion
var mirrorQuat : Quaternion = Quaternion(-qRot.x, qRot.y, qRot.z, -qRot.w); // Mirror the quaternion on X W
var reOrderedEulers : Vector3 = XYZtoZXY(mirrorQuat.eulerAngles); // Reorder XYZ (maya) to ZXY (unity)
theObject.transform.localEulerAngles = Vector3(reOrderedEulers.x, reOrderedEulers.y, reOrderedEulers.z);
theObject.transform.Rotate(new Vector3(0, 180, 0));
static function XYZtoZXY(v : Vector3) : Vector3 {
var qx : Quaternion = Quaternion.AngleAxis(v.x, Vector3.right);
var qy : Quaternion = Quaternion.AngleAxis(v.y, Vector3.up);
var qz : Quaternion = Quaternion.AngleAxis(v.z, Vector3.forward);
var qq : Quaternion = qz * qx * qy;
return qq.eulerAngles;
}
In some instances, it comes close, but not always. Obviously Unity must do this when importing maya files as animations come in fine.
Update: Okay after spending the better part of two days on this from converting eulers to rotation matrices, writing axisangle and matrix3 classes, scaling, writing a matrix3 to quaternion converter, poring over papers and a zillion different approaches, I threw it all out and went back to this system. The solution was simpler than I thought (isn’t it always?):
To convert Euler rotations from Maya (Right-handed XYZ to Unity left-handed ZXY):
- Invert rotation’s y and z signs: x, -y, -z
- Convert to ZYX
I’m actually terrible with matrix math so maybe I’m not understanding it properly, but I had to modify my code to qz * qy * qx. Once I did that it worked perfectly. To me this sounds like ZYX, not ZXY like all the docs say.
My working code:
static function MayaRotationToUnity(rotation : Vector3) : Quaternion {
var flippedRotation : Vector3 = Vector3(rotation.x, -rotation.y, -rotation.z); // flip Y and Z axis for right->left handed conversion
// convert XYZ to ZYX
var qx : Quaternion = Quaternion.AngleAxis(flippedRotation.x, Vector3.right);
var qy : Quaternion = Quaternion.AngleAxis(flippedRotation.y, Vector3.up);
var qz : Quaternion = Quaternion.AngleAxis(flippedRotation.z, Vector3.forward);
var qq : Quaternion = qz * qy * qx ; // this is the order
return qq;
}
Thanks to this forum post for the simple y, z flip left->right conversion.
I hope this helps someone.