Skip to content

Commit 8c4283e

Browse files
committed
export nodes with transforms then components
in a separate pass. Ensures that all bones are exported before skinned mesh is exported.
1 parent 8022566 commit 8c4283e

File tree

1 file changed

+60
-42
lines changed

1 file changed

+60
-42
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -800,12 +800,7 @@ private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene)
800800
// its corresponding parent, or to the scene if there is none.
801801
FbxNode fbxBoneNode;
802802
if (!MapUnityObjectToFbxNode.TryGetValue (t.gameObject, out fbxBoneNode)) {
803-
if (ExportSettings.mayaCompatibleNames) {
804-
t.name = ConvertToMayaCompatibleName (t.name);
805-
}
806-
fbxBoneNode = FbxNode.Create (fbxScene, t.name);
807-
fbxBoneNode.SetTransformationInheritType (FbxTransform.EInheritType.eInheritRSrs);
808-
MapUnityObjectToFbxNode.Add (t.gameObject, fbxBoneNode);
803+
Debug.LogError ("node should already be created");
809804
}
810805

811806
// Set it up as a skeleton node if we haven't already.
@@ -828,18 +823,11 @@ private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene)
828823
}
829824
}
830825

831-
// Step 2: connect up the hierarchy.
832826
var boneList = boneSet.ToArray();
833-
foreach (var unityBone in boneList) {
834-
if (unityBone.parent != null && MapUnityObjectToFbxNode.ContainsKey(unityBone.parent.gameObject)) {
835-
var fbxBone = MapUnityObjectToFbxNode [unityBone.gameObject];
836-
var fbxParent = MapUnityObjectToFbxNode [unityBone.parent.gameObject];
837-
fbxParent.AddChild (fbxBone);
838-
}
839-
}
840827

841-
// Get bindposes
842828
SkinnedMeshToBonesMap.Add (skinnedMesh, boneList);
829+
830+
// Step 2: Get bindposes
843831
var boneToBindPose = new Dictionary<Transform, Matrix4x4>();
844832
for (int boneIndex = 0, n = boneList.Length; boneIndex < n; boneIndex++) {
845833
var unityBone = boneList [boneIndex];
@@ -1206,9 +1194,17 @@ private string GetUniqueName(string name)
12061194
}
12071195

12081196
/// <summary>
1209-
/// Unconditionally export components on this game object
1197+
/// Creates an FbxNode for each GameObject.
12101198
/// </summary>
1211-
protected int ExportComponents (
1199+
/// <returns>The nodes.</returns>
1200+
/// <param name="unityGo">Unity go.</param>
1201+
/// <param name="fbxScene">Fbx scene.</param>
1202+
/// <param name="fbxNodeParent">Fbx node parent.</param>
1203+
/// <param name="exportProgress">Export progress.</param>
1204+
/// <param name="objectCount">Object count.</param>
1205+
/// <param name="newCenter">New center.</param>
1206+
/// <param name="exportType">Export type.</param>
1207+
protected int ExportNodes(
12121208
GameObject unityGo, FbxScene fbxScene, FbxNode fbxNodeParent,
12131209
int exportProgress, int objectCount, Vector3 newCenter,
12141210
TransformExportType exportType = TransformExportType.Local)
@@ -1220,18 +1216,14 @@ protected int ExportComponents (
12201216
}
12211217

12221218
// create an FbxNode and add it as a child of parent
1223-
FbxNode fbxNode;
1224-
bool alreadyExported = MapUnityObjectToFbxNode.TryGetValue (unityGo, out fbxNode);
1225-
if (!alreadyExported) {
1226-
fbxNode = FbxNode.Create (fbxScene, GetUniqueName (unityGo.name));
1227-
}
1219+
FbxNode fbxNode = FbxNode.Create (fbxScene, GetUniqueName (unityGo.name));
12281220
NumNodes++;
12291221

12301222
numObjectsExported++;
12311223
if (EditorUtility.DisplayCancelableProgressBar (
1232-
ProgressBarTitle,
1233-
string.Format ("Creating FbxNode {0}/{1}", numObjectsExported, objectCount),
1234-
(numObjectsExported / (float)objectCount) * 0.5f)) {
1224+
ProgressBarTitle,
1225+
string.Format ("Creating FbxNode {0}/{1}", numObjectsExported, objectCount),
1226+
(numObjectsExported / (float)objectCount) * 0.25f)) {
12351227
// cancel silently
12361228
return -1;
12371229
}
@@ -1243,8 +1235,41 @@ protected int ExportComponents (
12431235
// Use RSrs as the scaling inhertiance instead.
12441236
fbxNode.SetTransformationInheritType (FbxTransform.EInheritType.eInheritRSrs);
12451237

1246-
if (!alreadyExported) {
1247-
ExportTransform (unityGo.transform, fbxNode, newCenter, exportType);
1238+
ExportTransform (unityGo.transform, fbxNode, newCenter, exportType);
1239+
1240+
MapUnityObjectToFbxNode.Add (unityGo, fbxNode);
1241+
1242+
if (Verbose)
1243+
Debug.Log (string.Format ("exporting {0}", fbxNode.GetName ()));
1244+
1245+
fbxNodeParent.AddChild (fbxNode);
1246+
1247+
// now unityGo through our children and recurse
1248+
foreach (Transform childT in unityGo.transform) {
1249+
numObjectsExported = ExportNodes (childT.gameObject, fbxScene, fbxNode, numObjectsExported, objectCount, newCenter);
1250+
}
1251+
return numObjectsExported;
1252+
}
1253+
1254+
/// <summary>
1255+
/// Unconditionally export components on this game object
1256+
/// </summary>
1257+
protected bool ExportComponents(FbxScene fbxScene)
1258+
{
1259+
int numObjectsExported = 0;
1260+
int objectCount = MapUnityObjectToFbxNode.Count;
1261+
foreach (KeyValuePair<GameObject, FbxNode> entry in MapUnityObjectToFbxNode) {
1262+
numObjectsExported++;
1263+
if (EditorUtility.DisplayCancelableProgressBar (
1264+
ProgressBarTitle,
1265+
string.Format ("Exporting Components for GameObject {0}/{1}", numObjectsExported, objectCount),
1266+
((numObjectsExported / (float)objectCount) * 0.25f) + 0.25f)) {
1267+
// cancel silently
1268+
return false;
1269+
}
1270+
1271+
var unityGo = entry.Key;
1272+
var fbxNode = entry.Value;
12481273

12491274
// try export mesh
12501275
bool exportedMesh = ExportInstance (unityGo, fbxNode, fbxScene);
@@ -1257,20 +1282,8 @@ protected int ExportComponents (
12571282
if (!exportedMesh) {
12581283
ExportCamera (unityGo, fbxScene, fbxNode);
12591284
}
1260-
1261-
MapUnityObjectToFbxNode.Add (unityGo, fbxNode);
12621285
}
1263-
1264-
if (Verbose)
1265-
Debug.Log (string.Format ("exporting {0}", fbxNode.GetName ()));
1266-
1267-
fbxNodeParent.AddChild (fbxNode);
1268-
1269-
// now unityGo through our children and recurse
1270-
foreach (Transform childT in unityGo.transform) {
1271-
numObjectsExported = ExportComponents (childT.gameObject, fbxScene, fbxNode, numObjectsExported, objectCount, newCenter);
1272-
}
1273-
return numObjectsExported;
1286+
return true;
12741287
}
12751288

12761289
/// <summary>
@@ -1502,7 +1515,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
15021515

15031516
if(revisedExportSet.Count == 1){
15041517
foreach(var unityGo in revisedExportSet){
1505-
exportProgress = this.ExportComponents (
1518+
exportProgress = this.ExportNodes (
15061519
unityGo, fbxScene, fbxRootNode, exportProgress,
15071520
count, Vector3.zero, TransformExportType.Reset);
15081521
if (exportCancelled || exportProgress < 0) {
@@ -1516,7 +1529,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
15161529
Vector3 center = ExportSettings.centerObjects? FindCenter(revisedExportSet) : Vector3.zero;
15171530

15181531
foreach (var unityGo in revisedExportSet) {
1519-
exportProgress = this.ExportComponents (unityGo, fbxScene, fbxRootNode,
1532+
exportProgress = this.ExportNodes (unityGo, fbxScene, fbxRootNode,
15201533
exportProgress, count, center, TransformExportType.Global);
15211534
if (exportCancelled || exportProgress < 0) {
15221535
Debug.LogWarning ("Export Cancelled");
@@ -1525,6 +1538,11 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
15251538
}
15261539
}
15271540

1541+
if(!ExportComponents(fbxScene)){
1542+
Debug.LogWarning ("Export Cancelled");
1543+
return 0;
1544+
}
1545+
15281546
// Set the scene's default camera.
15291547
SetDefaultCamera (fbxScene);
15301548

0 commit comments

Comments
 (0)