Skip to content

Commit e06ad24

Browse files
authored
Merge pull request #406 from Unity-Technologies/UNI-36617-fix-skinned-mesh-update
UNI-36617 fix skinned mesh bone update
2 parents de6fad9 + c279832 commit e06ad24

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Packages/com.unity.formats.fbx/Editor/Scripts/FbxPrefabAutoUpdater.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,17 @@ public HashSet<String> GetNodesToRename()
11641164
return m_nodesToRename;
11651165
}
11661166

1167+
/// <summary>
1168+
/// Get the equivalent transform on the linked prefab for the given fbx transform.
1169+
/// e.g. if there is a transform called Bone1 in the fbx, find the transform called Bone1 in the prefab.
1170+
/// </summary>
1171+
/// <returns>The linked prefab transform that matches the given fbx transform or null if not found</returns>
1172+
private Transform GetMatchingPrefabTransform(Transform fbxTransform, Transform fbxAssetRoot, Transform prefabAssetRoot)
1173+
{
1174+
var transformPath = AnimationUtility.CalculateTransformPath(fbxTransform, fbxAssetRoot);
1175+
return prefabAssetRoot.Find(transformPath);
1176+
}
1177+
11671178
/// <summary>
11681179
/// Then we act -- in a slightly different order:
11691180
/// 1. Create all the new nodes we need to create.
@@ -1355,6 +1366,42 @@ public HashSet<GameObject> ImplementUpdates(FbxPrefab prefabInstance)
13551366
}
13561367
// Now set the values.
13571368
UnityEditor.EditorJsonUtility.FromJsonOverwrite(fbxComponent.jsonValue, prefabComponent);
1369+
1370+
// if the prefab component is a skinned mesh renderer, then make sure to update the bone list so that
1371+
// the bones are in the prefab, and not the fbx
1372+
if (prefabComponent is SkinnedMeshRenderer)
1373+
{
1374+
var skinnedMeshComponent = prefabComponent as SkinnedMeshRenderer;
1375+
var rootBone = skinnedMeshComponent.rootBone;
1376+
var bones = skinnedMeshComponent.bones;
1377+
1378+
var fbxAssetTransform = m_fbxPrefabUtility.GetFbxAsset().transform;
1379+
1380+
var rootPrefabTransform = GetMatchingPrefabTransform(rootBone, fbxAssetTransform, prefabRoot);
1381+
if (rootPrefabTransform == null)
1382+
{
1383+
Debug.LogWarningFormat("FbxPrefabAutoUpdater: Could not find root bone {0} for {1} skinned mesh in linked prefab", rootBone.name, skinnedMeshComponent.name);
1384+
}
1385+
skinnedMeshComponent.rootBone = rootPrefabTransform;
1386+
1387+
var prefabBones = new Transform[bones.Length];
1388+
for (int i = 0; i < bones.Length; i++)
1389+
{
1390+
var bone = bones[i];
1391+
1392+
var prefabTransform = GetMatchingPrefabTransform(bone, fbxAssetTransform, prefabRoot);
1393+
if (prefabTransform == null)
1394+
{
1395+
Debug.LogWarningFormat("FbxPrefabAutoUpdater: Could not find bone {0} for {1} skinned mesh", bone.name, skinnedMeshComponent.name);
1396+
continue;
1397+
}
1398+
1399+
prefabBones[i] = prefabTransform;
1400+
}
1401+
skinnedMeshComponent.bones = prefabBones;
1402+
1403+
continue;
1404+
}
13581405
}
13591406
}
13601407
return updatedNodes;

0 commit comments

Comments
 (0)