- Home /
Is it possible to update a skinned mesh renderer's bones?
I have a model with nested game objects that have skinned mesh renderers that are linked to that model's bone hierarchy. This nested objects are its clothing, so when the model moves, its clothes deforms with it accordingly. I'm trying to add new clothes to it, but they don't deform at all.
I'm thinking that maybe I shouldn't link a picture of the entire model, as it is kind of NSFW, so here are some pictures of the clothing item in question.
To the left is the default boot which I imported together with the complete model. To the right is the new boot that I imported with the very same complete model, but at a later time.
Here is how this new boot deforms when its entire model is added to the scene.
This is what happens when I try to attach this game object to another model.
Even though I changed this new item's Root Bone property's, it's still not linking itself with the other model's bones.
The model which I'm trying to nest this new boot is my main prefab that has much work done to it (ragdoll, hitboxes, tags, custom scripts that use specific bones, etc). The only thing that seems I can do is to recreate this prefab again, with the new model this time. That is very inefficient for me, as I have a lot of prefabs that would need to be created all over again. Looking through the documentation is not yielding answers to this, neither googling for this. How do you nest new skinned mesh renderers to game object and link its bones to it?
Edit: Issue solved. As I'm not skilled enough as a programmer to make a script of my own without scratching my head for hours, I grabbed the one linked by Cherno. The formatting was a little messed in the post, so I took the liberty to fix it and put it on a pastebin: https://pastebin.com/RVY8ygur.
Thank you for providing this code snippet! It' s awesome <3
Answer by Bunny83 · Nov 26, 2017 at 05:57 PM
The rootbone has nothing to do with the actual skinning of the SkinnedMeshRenderer, It has been added for the root motion feature of the the mecanim system.
The actual link between the bones and the SkinnedMeshRenderer is made through the bones array. However this array is not visible in the inspector though it is serialized. That's why a SkinnedMeshRenderer does not animate when you delete it's own bone array.
One problem is that if you have two skinned meshes with identical bones, it's not necessarily given that the order is the same. If it is the same you could simply copy the bones array from your main model to your clothes model.
If the order is not the same and you want to use a single bone structure you would need to keep the both bone hierarchies and use a script to replace the bones one-by-one based on the bone name / hierarchical position.
Note that when you import a Skinned$$anonymous$$esh the mesh itself (not the skinnedmeshrenderer) has a bindposes array. Those define the actual relative bone positions in their bind pose / reference pose. Each element of the bones array corresponds to the same element in the bindposes array. Further more each vertex of the mesh has a BoneWeight. Each "boneIndex" refers to one element in the bones array. This is the whole magic behind the linking between the $$anonymous$$esh and it's bones.
I suppose this might help you: Shared skeleton and animation state
Thank you so much guys for the quality answers, you are life savers. I'll edit the original post with a clear note for the solution.
Your answer
Follow this Question
Related Questions
Combine Skinned meshes 2 Answers
Bones do not switch when called for (Find bones of player then replace clothes bones) 1 Answer
Does the transform.parent hierarchy have any relationship to the bone hierarchy? 1 Answer
New material only covering the same area as the previous one 1 Answer
Attach one animated mesh to another 0 Answers