- Home /
How to re-apply a mesh to an armature?
Hi!
I created a rigged character in blender, imported it to unity and applied rigidbodies to the armature to create a ragdoll. So far so good.
In blender, I added another mesh (a clothing layer) attached to the same armature. Upon importing this into Unity, the character in the prefab I already had there is twisted and weird. However, if I simply drag the new model into the scene, it looks good. Because it took a lot of effort to set up the ragdoll, I do not want to apply all the rigidbodies and joints again.
I want to apply the new mesh to the old armature.
Re-coupling a mesh to another armature seems pretty much impossible - no matter what I do the mesh follows the original armature which spawned with it on dragging the asset to the scene. Even if that original armature is deleted!
From my understanding this is because the mesh's Bone Weights are coupled to that armature. How do I change which armature they are hooked up to?
Thanks!
To clarify, I want Unity to do exactly what it does when pulling a rigged model into the scene, just with an already existing armature.
Answer by JLJac · Dec 15, 2017 at 01:49 PM
Solved it! Add the below code to a monobehavior. Add the monobehavior to the (same transform as the) SkinnedMeshRenderer. Now drag the base transform of the new armature you want to assign to the Armature slot in the inspector, and click the checkbox to activate the script.
The script will go through the Bones of the mesh, and try to re-assign to bones of the same name in the new armature.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class ReassignBoneWeigthsToNewMesh : MonoBehaviour
{
public Transform newArmature;
public string rootBoneName = "Hips";
public bool PressToReassign;
void Update()
{
if (PressToReassign)
Reassign();
PressToReassign = false;
}
// [ContextMenu("Reassign Bones")]
public void Reassign()
{
if (newArmature == null) {
Debug.Log("No new armature assigned");
return;
}
if (newArmature.Find(rootBoneName) == null) {
Debug.Log("Root bone not found");
return;
}
Debug.Log("Reassingning bones");
SkinnedMeshRenderer rend = gameObject.GetComponent<SkinnedMeshRenderer>();
Transform[] bones = rend.bones;
rend.rootBone = newArmature.Find(rootBoneName);
Transform[] children = newArmature.GetComponentsInChildren<Transform>();
for (int i = 0; i < bones.Length; i++)
for (int a = 0; a < children.Length; a++)
if (bones[i].name == children[a].name) {
bones[i] = children[a];
break;
}
rend.bones = bones;
}
}
You rock dude, exactly what I needed. I create a new hair mesh to add to my existing prefabs but they weren't showing up when I dragged them from my base armature to my prefab. This did the trick!!!
Your answer
Follow this Question
Related Questions
How can I assemble a body from parts in game?? 0 Answers
Creating mesh runtime 1 Answer
Prioritizing Visibility of Meshes 1 Answer
Bunch of Cubes. How to reduce tris/verts? 0 Answers
Generate Polygons and Colliders Runtime in 2D Game 0 Answers