Skip to content

Commit 51ce74e

Browse files
authored
Merge pull request #53 from Unity-Technologies/UNI-21594-incorrect-rotation-in-maya
UNI-21594 incorrect rotation in Maya
2 parents 4d9b4c4 + f9ad815 commit 51ce74e

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -502,47 +502,70 @@ meshInfo.Vertices [v].z*UnitScaleFactor
502502
return fbxMesh;
503503
}
504504

505+
/// <summary>
506+
/// Takes a Quaternion and returns a Euler with XYZ rotation order.
507+
/// Also converts from left (Unity) to righthanded (Maya) coordinates.
508+
///
509+
/// Note: Cannot simply use the FbxQuaternion.DecomposeSphericalXYZ()
510+
/// function as this returns the angle in spherical coordinates
511+
/// instead of Euler angles, which Maya does not import properly.
512+
/// </summary>
513+
/// <returns>Euler with XYZ rotation order.</returns>
514+
public static FbxDouble3 QuaternionToXYZEuler(Quaternion q)
515+
{
516+
FbxQuaternion quat = new FbxQuaternion (q.x, q.y, q.z, q.w);
517+
FbxAMatrix m = new FbxAMatrix ();
518+
m.SetQ (quat);
519+
var vector4 = m.GetR ();
520+
521+
// Negate the y and z values of the rotation to convert
522+
// from Unity to Maya coordinates (left to righthanded).
523+
var result = new FbxDouble3 (vector4.X, -vector4.Y, -vector4.Z);
524+
525+
return result;
526+
}
527+
505528
// get a fbxNode's global default position.
506529
protected void ExportTransform (UnityEngine.Transform unityTransform, FbxNode fbxNode, Vector3 newCenter, TransformExportType exportType)
507530
{
508531
// Fbx rotation order is XYZ, but Unity rotation order is ZXY.
509532
// This causes issues when converting euler to quaternion, causing the final
510533
// rotation to be slighlty off.
511-
// Fixed if we set the rotation order to the Unity rotation order in the FBX.
512-
fbxNode.SetRotationOrder (FbxNode.EPivotSet.eSourcePivot, FbxEuler.EOrder.eOrderZXY);
534+
// Fixed by exporting the rotations as eulers with XYZ rotation order.
535+
fbxNode.SetRotationOrder (FbxNode.EPivotSet.eSourcePivot, FbxEuler.EOrder.eOrderXYZ);
513536

514537
UnityEngine.Vector3 unityTranslate;
515-
UnityEngine.Vector3 unityRotate;
538+
FbxDouble3 unityRotate;
516539
UnityEngine.Vector3 unityScale;
517540

518541
switch (exportType) {
519542
case TransformExportType.Reset:
520543
unityTranslate = Vector3.zero;
521-
unityRotate = Vector3.zero;
544+
unityRotate = new FbxDouble3 (0);
522545
unityScale = Vector3.one;
523546
break;
524547
case TransformExportType.Global:
525548
unityTranslate = GetRecenteredTranslation(unityTransform, newCenter);
526-
unityRotate = unityTransform.rotation.eulerAngles;
549+
unityRotate = QuaternionToXYZEuler(unityTransform.rotation);
527550
unityScale = unityTransform.lossyScale;
528551
break;
529552
default: /*case TransformExportType.Local*/
530553
unityTranslate = unityTransform.localPosition;
531-
unityRotate = unityTransform.localRotation.eulerAngles;
554+
unityRotate = QuaternionToXYZEuler(unityTransform.localRotation);
532555
unityScale = unityTransform.localScale;
533556
break;
534557
}
535558

536559
// transfer transform data from Unity to Fbx
537-
// Negating the x value of the translation, and the y and z values of the rotation
538-
// to convert from Unity to Maya coordinates (left to righthanded).
560+
// Negating the x value of the translation to convert from Unity
561+
// to Maya coordinates (left to righthanded).
539562
// Scaling the translation by 100 to convert from m to cm.
540563
var fbxTranslate = new FbxDouble3 (
541564
-unityTranslate.x*UnitScaleFactor,
542565
unityTranslate.y*UnitScaleFactor,
543566
unityTranslate.z*UnitScaleFactor
544567
);
545-
var fbxRotate = new FbxDouble3 (unityRotate.x, -unityRotate.y, -unityRotate.z);
568+
var fbxRotate = unityRotate;
546569
var fbxScale = new FbxDouble3 (unityScale.x, unityScale.y, unityScale.z);
547570

548571
// set the local position of fbxNode
@@ -772,6 +795,7 @@ public enum TransformExportType { Local, Global, Reset };
772795
/// </summary>
773796
public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
774797
{
798+
exportCancelled = false;
775799
Verbose = true;
776800
try {
777801
// Create the FBX manager

0 commit comments

Comments
 (0)