Skip to content

Commit 6d66c08

Browse files
committed
code review fixes
- added function headers + additional comments to unclear code - log warning is bone parent is null - move "GetObjectToRootCount" into a function - remove unnecessary warning
1 parent 4adb207 commit 6d66c08

File tree

1 file changed

+87
-18
lines changed

1 file changed

+87
-18
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -958,9 +958,27 @@ private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene,
958958
while (s.Count > 0) {
959959
var t = s.Pop ();
960960

961-
boneSet.Add (t);
961+
if (!boneSet.Add (t)) {
962+
continue;
963+
}
964+
965+
if (t.parent == null) {
966+
Debug.LogWarningFormat (
967+
"FbxExporter: {0} is a bone but not a descendant of {1}'s mesh's root bone.",
968+
t.name, skinnedMesh.name
969+
);
970+
continue;
971+
}
962972

963-
if (t != root && t.parent != null && !boneSet.Contains(t.parent)) {
973+
// Each skinned mesh in Unity has one root bone, but may have objects
974+
// between the root bone and leaf bones that are not in the bone list.
975+
// However all objects between two bones in a hierarchy should be bones
976+
// as well.
977+
// e.g. in rootBone -> bone1 -> obj1 -> bone2, obj1 should be a bone
978+
//
979+
// Traverse from all leaf bones to the root bone adding everything in between
980+
// to the boneSet regardless of whether it is in the skinned mesh's bone list.
981+
if (t != root && !boneSet.Contains(t.parent)) {
964982
s.Push (t.parent);
965983
}
966984
}
@@ -2083,7 +2101,7 @@ protected int ExportTransformHierarchy(
20832101
// both Maya and Unity use RSrs inheritance by default.
20842102
// Note: MotionBuilder uses RrSs inheritance by default as well, though it is possible
20852103
// to select a different inheritance type in the UI.
2086-
// Use RSrs as the scaling inhertiance instead.
2104+
// Use RSrs as the scaling inheritance instead.
20872105
fbxNode.SetTransformationInheritType (FbxTransform.EInheritType.eInheritRSrs);
20882106

20892107
ExportTransform (unityGo.transform, fbxNode, newCenter, exportType);
@@ -2098,11 +2116,23 @@ protected int ExportTransformHierarchy(
20982116
return numObjectsExported;
20992117
}
21002118

2119+
/// <summary>
2120+
/// Export data containing what to export when
2121+
/// exporting animation only.
2122+
/// </summary>
21012123
public struct AnimationOnlyExportData {
2124+
// map from animation clip to GameObject that has Animation/Animator
2125+
// component containing clip
21022126
public Dictionary<AnimationClip, GameObject> animationClips;
2127+
2128+
// set of all GameObjects to export
21032129
public HashSet<GameObject> goExportSet;
2130+
2131+
// map from GameObject to component type to export
21042132
public Dictionary<GameObject, System.Type> exportComponent;
2105-
public AnimationClip defaultClip; // first clip to export
2133+
2134+
// first clip to export
2135+
public AnimationClip defaultClip;
21062136

21072137
public AnimationOnlyExportData(
21082138
Dictionary<AnimationClip, GameObject> animClips,
@@ -2116,6 +2146,13 @@ public AnimationOnlyExportData(
21162146
}
21172147
}
21182148

2149+
/// <summary>
2150+
/// Exports all animation clips in the hierarchy along with
2151+
/// the minimum required GameObject information.
2152+
/// i.e. Animated GameObjects, their ancestors, and their transforms are exported,
2153+
/// but components are only exported if explicitly animated. Meshes are not exported.
2154+
/// </summary>
2155+
/// <returns>The number of nodes exported.</returns>
21192156
protected int ExportAnimationOnly(
21202157
GameObject unityGO,
21212158
FbxScene fbxScene,
@@ -2180,7 +2217,6 @@ protected int ExportAnimationOnly(
21802217
}
21812218

21822219
// export animation
2183-
21842220
// export default clip first
21852221
if (exportData.defaultClip != null) {
21862222
var defaultClip = exportData.defaultClip;
@@ -2207,6 +2243,11 @@ public SkinnedMeshBoneInfo(SkinnedMeshRenderer skinnedMesh, Dictionary<Transform
22072243
}
22082244
}
22092245

2246+
/// <summary>
2247+
/// Exports the Gameobject and it's ancestors.
2248+
/// </summary>
2249+
/// <returns><c>true</c>, if game object and parents were exported,
2250+
/// <c>false</c> if export cancelled.</returns>
22102251
private bool ExportGameObjectAndParents(
22112252
GameObject unityGo,
22122253
GameObject rootObject,
@@ -2286,6 +2327,14 @@ private bool ExportGameObjectAndParents(
22862327
return true;
22872328
}
22882329

2330+
/// <summary>
2331+
/// Exports the bone transform.
2332+
/// </summary>
2333+
/// <returns><c>true</c>, if bone transform was exported, <c>false</c> otherwise.</returns>
2334+
/// <param name="fbxNode">Fbx node.</param>
2335+
/// <param name="fbxScene">Fbx scene.</param>
2336+
/// <param name="unityBone">Unity bone.</param>
2337+
/// <param name="boneInfo">Bone info.</param>
22892338
private bool ExportBoneTransform(
22902339
FbxNode fbxNode, FbxScene fbxScene, Transform unityBone, SkinnedMeshBoneInfo boneInfo
22912340
){
@@ -2358,6 +2407,37 @@ private bool ExportBoneTransform(
23582407
return true;
23592408
}
23602409

2410+
/// <summary>
2411+
/// Counts how many objects are between this object and the root (exclusive).
2412+
/// </summary>
2413+
/// <returns>The object to root count.</returns>
2414+
/// <param name="startObject">Start object.</param>
2415+
/// <param name="root">Root object.</param>
2416+
private int GetObjectToRootCount(Transform startObject, Transform root){
2417+
if (startObject == null) {
2418+
return 0;
2419+
}
2420+
2421+
int count = 0;
2422+
var parent = startObject.parent;
2423+
while (parent != null && parent != root) {
2424+
count++;
2425+
parent = parent.parent;
2426+
}
2427+
return count;
2428+
}
2429+
2430+
2431+
/// <summary>
2432+
/// Gets the count of animated objects to be exported.
2433+
///
2434+
/// In addition, collects the minimum set of what needs to be exported for each GameObject hierarchy.
2435+
/// This contains all the animated GameObjects, their ancestors, their transforms, as well as any animated
2436+
/// components and the animation clips. Also, the first animation to export, if any.
2437+
/// </summary>
2438+
/// <returns>The animation only hierarchy count.</returns>
2439+
/// <param name="exportSet">GameObject hierarchies selected for export.</param>
2440+
/// <param name="hierarchyToExportData">Map from GameObject hierarchy to animation export data.</param>
23612441
protected int GetAnimOnlyHierarchyCount(
23622442
HashSet<GameObject> exportSet,
23632443
out Dictionary<GameObject, AnimationOnlyExportData> hierarchyToExportData
@@ -2380,12 +2460,7 @@ out Dictionary<GameObject, AnimationOnlyExportData> hierarchyToExportData
23802460
int fromRoot = int.MaxValue;
23812461
Animation rootAnimation = null;
23822462
foreach (var anim in legacyAnim) {
2383-
int count = 0;
2384-
var parent = anim.transform.parent;
2385-
while (parent != null && parent != go.transform) {
2386-
count++;
2387-
parent = parent.parent;
2388-
}
2463+
int count = GetObjectToRootCount(anim.transform, go.transform);
23892464

23902465
if (count < fromRoot) {
23912466
fromRoot = count;
@@ -2399,12 +2474,7 @@ out Dictionary<GameObject, AnimationOnlyExportData> hierarchyToExportData
23992474
int aFromRoot = int.MaxValue;
24002475
Animator rootAnimator = null;
24012476
foreach (var anim in genericAnim) {
2402-
int count = 0;
2403-
var parent = anim.transform.parent;
2404-
while (parent != null && parent != go.transform) {
2405-
count++;
2406-
parent = parent.parent;
2407-
}
2477+
int count = GetObjectToRootCount(anim.transform, go.transform);
24082478

24092479
if (count < aFromRoot) {
24102480
aFromRoot = count;
@@ -2431,7 +2501,6 @@ out Dictionary<GameObject, AnimationOnlyExportData> hierarchyToExportData
24312501
var motion = dController.layers [0].stateMachine.defaultState.motion;
24322502
var defaultClip = motion as AnimationClip;
24332503
if (defaultClip) {
2434-
Debug.LogWarning ("default clip: " + defaultClip.name);
24352504
exportData.defaultClip = defaultClip;
24362505
} else {
24372506
Debug.LogWarningFormat ("Couldn't export motion {0}", motion.name);

0 commit comments

Comments
 (0)