Skip to content

Commit e398ce2

Browse files
committed
add function to validate bones
a transform is a bone if it is in the skinned meshes's bone list or has both a parent and child that is in the skinned meshes' list of bones. Go through all bones and their hierarchies, creating fbx node's for them as we go and storing the valid bones.
1 parent a721762 commit e398ce2

File tree

1 file changed

+28
-42
lines changed

1 file changed

+28
-42
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,20 @@ SkinnedMeshRenderer unitySkin
741741
return true;
742742
}
743743

744+
private bool IsBone(Transform t, Dictionary<Transform, int> bones){
745+
if (bones.ContainsKey (t)) {
746+
return true;
747+
}
748+
749+
foreach (Transform child in t) {
750+
if (IsBone (child, bones)) {
751+
//Debug.LogError ("our child is a bone: " + t.name);
752+
return true;
753+
}
754+
}
755+
return false;
756+
}
757+
744758
/// <summary>
745759
/// Export bones of skinned mesh, if this is a skinned mesh with
746760
/// bones and bind poses.
@@ -772,39 +786,14 @@ private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene)
772786
}
773787

774788
// Step 1: create the bones.
775-
for (int boneIndex = 0, n = bones.Length; boneIndex < n; boneIndex++) {
776-
Transform unityBoneTransform = bones [boneIndex];
777-
778-
// Create the bone node if we haven't already. Parent it to
779-
// its corresponding parent, or to the scene if there is none.
780-
FbxNode fbxBoneNode;
781-
if (!MapUnityObjectToFbxNode.TryGetValue(unityBoneTransform.gameObject, out fbxBoneNode)) {
782-
if (ExportSettings.mayaCompatibleNames) {
783-
unityBoneTransform.name = ConvertToMayaCompatibleName (unityBoneTransform.name);
784-
}
785-
fbxBoneNode = FbxNode.Create (fbxScene, unityBoneTransform.name);
786-
MapUnityObjectToFbxNode.Add(unityBoneTransform.gameObject, fbxBoneNode);
787-
}
788-
789-
// Set it up as a skeleton node if we haven't already.
790-
if (fbxBoneNode.GetSkeleton() == null) {
791-
FbxSkeleton fbxSkeleton = FbxSkeleton.Create (fbxScene, unityBoneTransform.name + "_Skel");
792-
793-
var fbxSkeletonType = skinnedMesh.rootBone != unityBoneTransform//index.ContainsKey(unityBoneTransform.parent)
794-
? FbxSkeleton.EType.eLimbNode : FbxSkeleton.EType.eRoot;
795-
fbxSkeleton.SetSkeletonType (fbxSkeletonType);
796-
fbxSkeleton.Size.Set (1.0f*UnitScaleFactor);
797-
fbxBoneNode.SetNodeAttribute (fbxSkeleton);
798-
}
799-
}
800-
801-
List<Transform> boneList = new List<Transform> (bones);
802-
Queue<Transform> q = new Queue<Transform> ();
803-
q.Enqueue (skinnedMesh.rootBone);
789+
List<Transform> boneList = new List<Transform> ();
790+
Queue<Transform> q = new Queue<Transform> (bones);
804791
while (q.Count > 0) {
805792
var v = q.Dequeue ();
806793

807-
if (!index.ContainsKey (v) && v.childCount > 0) {
794+
if (IsBone(v, index)) {
795+
// Create the bone node if we haven't already. Parent it to
796+
// its corresponding parent, or to the scene if there is none.
808797
FbxNode fbxBoneNode;
809798
if (!MapUnityObjectToFbxNode.TryGetValue (v.gameObject, out fbxBoneNode)) {
810799
if (ExportSettings.mayaCompatibleNames) {
@@ -826,16 +815,11 @@ private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene)
826815
fbxBoneNode.SetNodeAttribute (fbxSkeleton);
827816
}
828817

829-
if (v.parent != null && MapUnityObjectToFbxNode.ContainsKey(v.parent.gameObject)) {
830-
var fbxParent = MapUnityObjectToFbxNode [v.parent.gameObject];
831-
fbxParent.AddChild (fbxBoneNode);
832-
}
833-
834818
boneList.Add (v);
835-
}
836819

837-
foreach (Transform child in v) {
838-
q.Enqueue (child);
820+
foreach (Transform child in v) {
821+
q.Enqueue (child);
822+
}
839823
}
840824
}
841825

@@ -854,24 +838,26 @@ private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene)
854838
var fbxBone = MapUnityObjectToFbxNode [unityBone.gameObject];
855839

856840
Matrix4x4 pose;
857-
if (boneIndex < bones.Length) {
841+
if (index.ContainsKey(unityBone)) {
842+
int i = index[unityBone];
843+
858844
if (fbxBone.GetSkeleton().GetSkeletonType() == FbxSkeleton.EType.eRoot) {
859845
// bind pose is local -> root. We want root -> local, so invert.
860846
var parentTransform = skinnedMesh.transform;
861847
var parentMatrix = Matrix4x4.TRS (parentTransform.localPosition, parentTransform.localRotation, parentTransform.localScale);
862848

863-
pose = parentMatrix * bindPoses[boneIndex].inverse;
849+
pose = parentMatrix * bindPoses[i].inverse;
864850
} else {
865851
if (index.ContainsKey (unityBone.parent)) {
866852
// Bind pose is local -> parent -> ... -> root.
867853
// We want parent -> local.
868854
// Invert our bind pose to get root -> local.
869855
// The apply parent -> root to leave just parent -> local.
870-
pose = bindPoses [index [unityBone.parent]] * bindPoses [boneIndex].inverse;
856+
pose = bindPoses [index [unityBone.parent]] * bindPoses [i].inverse;
871857
} else if (unityBone.parent != null) {
872858
//var parentPose = Matrix4x4.TRS (unityBone.parent.localPosition, unityBone.parent.localRotation, unityBone.parent.localScale);
873859
var parentPose = unityBone.parent.worldToLocalMatrix * skinnedMesh.transform.localToWorldMatrix;
874-
pose = parentPose * bindPoses [boneIndex].inverse;
860+
pose = parentPose * bindPoses [i].inverse;
875861
} else {
876862
pose = Matrix4x4.identity;
877863
}

0 commit comments

Comments
 (0)