Skip to content

Commit d2d1bf5

Browse files
authored
Merge pull request #22 from Unity-Technologies/UNI-20467-zero-out-root-transform
Uni 20467 zero out root transform
2 parents eb28aeb + 8166e34 commit d2d1bf5

File tree

3 files changed

+89
-41
lines changed

3 files changed

+89
-41
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,30 +99,12 @@ private static List<GameObject> OnConvertInPlace ()
9999

100100
if (unityMainAsset != null) {
101101
Object unityObj = PrefabUtility.InstantiateAttachedAsset (unityMainAsset);
102-
103-
if (unityObj != null)
102+
GameObject unityGO = unityObj as GameObject;
103+
if (unityGO != null)
104104
{
105-
GameObject unityGO = unityObj as GameObject;
106-
Transform unityGOTransform = unityGO.transform;
107-
Transform origGOTransform = gosToExport [i].transform;
108-
109-
// Set the name to be the name of the instantiated asset.
110-
// This will get rid of the "(Clone)" if it's added
111-
unityGO.name = unityMainAsset.name;
112-
113-
// configure transform and maintain local pose
114-
unityGOTransform.SetParent (origGOTransform.parent, false);
115-
116-
unityGOTransform.SetSiblingIndex (origGOTransform.GetSiblingIndex());
105+
SetupImportedGameObject (gosToExport [i], unityGO);
117106

118-
// copy the components over, assuming that the hierarchy order is unchanged
119-
if (origGOTransform.hierarchyCount != unityGOTransform.hierarchyCount) {
120-
Debug.LogWarning (string.Format ("Warning: Exported {0} objects, but only imported {1}",
121-
origGOTransform.hierarchyCount, unityGOTransform.hierarchyCount));
122-
}
123-
CopyComponentsRecursive (gosToExport[i], unityGO);
124-
125-
result.Add (unityObj as GameObject);
107+
result.Add (unityGO);
126108

127109
// remove (now redundant) gameobject
128110
#if UNI_19965
@@ -144,6 +126,37 @@ private static List<GameObject> OnConvertInPlace ()
144126
return result;
145127
}
146128

129+
private static void SetupImportedGameObject(GameObject orig, GameObject imported)
130+
{
131+
Transform importedTransform = imported.transform;
132+
Transform origTransform = orig.transform;
133+
134+
// Set the name to be the name of the instantiated asset.
135+
// This will get rid of the "(Clone)" if it's added
136+
imported.name = orig.name;
137+
138+
// configure transform and maintain local pose
139+
importedTransform.SetParent (origTransform.parent, false);
140+
importedTransform.SetSiblingIndex (origTransform.GetSiblingIndex());
141+
142+
// set the transform to be the same as before
143+
bool success = UnityEditorInternal.ComponentUtility.CopyComponent (origTransform);
144+
if (success) {
145+
success = UnityEditorInternal.ComponentUtility.PasteComponentValues(importedTransform);
146+
}
147+
if (!success) {
148+
Debug.LogWarning (string.Format ("Warning: Failed to copy component Transform from {0} to {1}",
149+
imported.name, origTransform.name));
150+
}
151+
152+
// copy the components over, assuming that the hierarchy order is unchanged
153+
if (origTransform.hierarchyCount != importedTransform.hierarchyCount) {
154+
Debug.LogWarning (string.Format ("Warning: Exported {0} objects, but only imported {1}",
155+
origTransform.hierarchyCount, importedTransform.hierarchyCount));
156+
}
157+
CopyComponentsRecursive (orig, imported);
158+
}
159+
147160
private static void CopyComponentsRecursive(GameObject from, GameObject to){
148161
if (!to.name.StartsWith(from.name) || from.transform.childCount != to.transform.childCount) {
149162
Debug.LogError (string.Format("Error: hierarchies don't match (From: {0}, To: {1})", from.name, to.name));

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 54 additions & 19 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,24 +463,26 @@ 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 exportProgress, int objectCount, TransformExportType exportType = TransformExportType.Local)
450469
{
451-
int i = currentIndex;
470+
int numObjectsExported = exportProgress;
452471

453472
// create an FbxNode and add it as a child of parent
454473
FbxNode fbxNode = FbxNode.Create (fbxScene, unityGo.name);
455474
NumNodes++;
456475

457-
i++;
476+
numObjectsExported++;
458477
if (EditorUtility.DisplayCancelableProgressBar (
459478
ProgressBarTitle,
460-
string.Format ("Creating FbxNode {0}/{1}", i, objectCount),
461-
(i / (float)objectCount) * 0.5f)) {
479+
string.Format ("Creating FbxNode {0}/{1}", numObjectsExported, objectCount),
480+
(numObjectsExported / (float)objectCount) * 0.5f)) {
462481
// cancel silently
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);
@@ -475,9 +494,9 @@ protected int ExportComponents (GameObject unityGo, FbxScene fbxScene, FbxNode
475494

476495
// now unityGo through our children and recurse
477496
foreach (Transform childT in unityGo.transform) {
478-
i = ExportComponents (childT.gameObject, fbxScene, fbxNode, i, objectCount);
497+
numObjectsExported = ExportComponents (childT.gameObject, fbxScene, fbxNode, numObjectsExported, objectCount);
479498
}
480-
return i;
499+
return numObjectsExported;
481500
}
482501

483502
/// <summary>
@@ -538,6 +557,8 @@ public static HashSet<GameObject> RemoveRedundantObjects(IEnumerable<UnityEngine
538557
return toExport;
539558
}
540559

560+
public enum TransformExportType { Local, Global, Zeroed };
561+
541562
/// <summary>
542563
/// Export all the objects in the set.
543564
/// Return the number of objects in the set that we exported.
@@ -600,14 +621,28 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
600621

601622
// export set of object
602623
FbxNode fbxRootNode = fbxScene.GetRootNode ();
603-
int i = 0;
624+
// stores how many objects we have exported, -1 if export was cancelled
625+
int exportProgress = 0;
604626
var revisedExportSet = RemoveRedundantObjects(unityExportSet);
605627
int count = GetHierarchyCount (revisedExportSet);
606-
foreach (var unityGo in revisedExportSet) {
607-
i = this.ExportComponents (unityGo, fbxScene, fbxRootNode, i, count);
608-
if (i < 0) {
609-
Debug.LogWarning ("Export Cancelled");
610-
return 0;
628+
629+
if(revisedExportSet.Count == 1){
630+
foreach(var unityGo in revisedExportSet){
631+
exportProgress = this.ExportComponents (unityGo, fbxScene, fbxRootNode, exportProgress, count, TransformExportType.Zeroed);
632+
if (exportProgress < 0) {
633+
Debug.LogWarning ("Export Cancelled");
634+
return 0;
635+
}
636+
}
637+
}
638+
else{
639+
foreach (var unityGo in revisedExportSet) {
640+
exportProgress = this.ExportComponents (unityGo, fbxScene, fbxRootNode, exportProgress, count,
641+
unityGo.transform.parent == null? TransformExportType.Local : TransformExportType.Global);
642+
if (exportProgress < 0) {
643+
Debug.LogWarning ("Export Cancelled");
644+
return 0;
645+
}
611646
}
612647
}
613648

Assets/FbxExporters/LICENSE.txt

100755100644
File mode changed.

0 commit comments

Comments
 (0)