Skip to content

Commit dd9dd8d

Browse files
committed
zero out root if only one object exported
if multiple objects exported, use global position for objects whose parents are not being exported
1 parent 380febb commit dd9dd8d

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -415,18 +415,35 @@ meshInfo.Vertices [v].z
415415
}
416416

417417
// get a fbxNode's global default position.
418-
protected void ExportTransform (UnityEngine.Transform unityTransform, FbxNode fbxNode)
418+
protected void ExportTransform (UnityEngine.Transform unityTransform, FbxNode fbxNode, TransformExportType exportType)
419419
{
420420
// Fbx rotation order is XYZ, but Unity rotation order is ZXY.
421421
// This causes issues when converting euler to quaternion, causing the final
422422
// rotation to be slighlty off.
423423
// Fixed if we set the rotation order to the Unity rotation order in the FBX.
424424
fbxNode.SetRotationOrder (FbxNode.EPivotSet.eSourcePivot, FbxEuler.EOrder.eOrderZXY);
425425

426-
// get local position of fbxNode (from Unity)
427-
UnityEngine.Vector3 unityTranslate = unityTransform.localPosition;
428-
UnityEngine.Vector3 unityRotate = unityTransform.localRotation.eulerAngles;
429-
UnityEngine.Vector3 unityScale = unityTransform.localScale;
426+
UnityEngine.Vector3 unityTranslate;
427+
UnityEngine.Vector3 unityRotate;
428+
UnityEngine.Vector3 unityScale;
429+
430+
switch (exportType) {
431+
case TransformExportType.Zeroed:
432+
unityTranslate = Vector3.zero;
433+
unityRotate = Vector3.zero;
434+
unityScale = Vector3.one;
435+
break;
436+
case TransformExportType.Global:
437+
unityTranslate = unityTransform.position;
438+
unityRotate = unityTransform.rotation.eulerAngles;
439+
unityScale = unityTransform.lossyScale;
440+
break;
441+
default: /*case TransformExportType.Local*/
442+
unityTranslate = unityTransform.localPosition;
443+
unityRotate = unityTransform.localRotation.eulerAngles;
444+
unityScale = unityTransform.localScale;
445+
break;
446+
}
430447

431448
// transfer transform data from Unity to Fbx
432449
// Negating the x value of the translation, and the y and z values of the rotation
@@ -446,7 +463,9 @@ protected void ExportTransform (UnityEngine.Transform unityTransform, FbxNode fb
446463
/// <summary>
447464
/// Unconditionally export components on this game object
448465
/// </summary>
449-
protected int ExportComponents (GameObject unityGo, FbxScene fbxScene, FbxNode fbxNodeParent, int currentIndex, int objectCount)
466+
protected int ExportComponents (
467+
GameObject unityGo, FbxScene fbxScene, FbxNode fbxNodeParent,
468+
int currentIndex, int objectCount, TransformExportType exportType = TransformExportType.Local)
450469
{
451470
int i = currentIndex;
452471

@@ -463,7 +482,7 @@ protected int ExportComponents (GameObject unityGo, FbxScene fbxScene, FbxNode
463482
return -1;
464483
}
465484

466-
ExportTransform ( unityGo.transform, fbxNode);
485+
ExportTransform ( unityGo.transform, fbxNode, exportType);
467486

468487
bool weldVertices = FbxExporters.EditorTools.ExportSettings.instance.weldVertices;
469488
ExportMesh (GetMeshInfo( unityGo ), fbxNode, fbxScene, weldVertices);
@@ -532,6 +551,8 @@ public static HashSet<GameObject> RemoveDuplicateObjects(IEnumerable<UnityEngine
532551
return toExport;
533552
}
534553

554+
public enum TransformExportType { Local, Global, Zeroed };
555+
535556
/// <summary>
536557
/// Export all the objects in the set.
537558
/// Return the number of objects in the set that we exported.
@@ -597,11 +618,24 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
597618
int i = 0;
598619
var revisedExportSet = RemoveDuplicateObjects(unityExportSet);
599620
int count = GetGameObjectCount (revisedExportSet);
600-
foreach (var unityGo in revisedExportSet) {
601-
i = this.ExportComponents (unityGo, fbxScene, fbxRootNode, i, count);
602-
if (i < 0) {
603-
Debug.LogWarning ("Export Cancelled");
604-
return 0;
621+
622+
if(revisedExportSet.Count == 1){
623+
foreach(var unityGo in revisedExportSet){
624+
i = this.ExportComponents (unityGo, fbxScene, fbxRootNode, i, count, TransformExportType.Zeroed);
625+
if (i < 0) {
626+
Debug.LogWarning ("Export Cancelled");
627+
return 0;
628+
}
629+
}
630+
}
631+
else{
632+
foreach (var unityGo in revisedExportSet) {
633+
i = this.ExportComponents (unityGo, fbxScene, fbxRootNode, i, count,
634+
unityGo.transform.parent == null? TransformExportType.Local : TransformExportType.Global);
635+
if (i < 0) {
636+
Debug.LogWarning ("Export Cancelled");
637+
return 0;
638+
}
605639
}
606640
}
607641

0 commit comments

Comments
 (0)