Skip to content

Instantly share code, notes, and snippets.

@emilianavt
Last active October 20, 2023 19:57
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save emilianavt/721cd4dd2d4a62ba54b002b63f894dbf to your computer and use it in GitHub Desktop.
Save emilianavt/721cd4dd2d4a62ba54b002b63f894dbf to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class ReassignBoneWeigthsToNewMesh : MonoBehaviour {
public Transform newArmature;
public string rootBoneName = "Hips";
public bool resetPose = false;
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] != null && children[a] != null && bones[i].name == children[a].name) {
if (resetPose) {
children[a].transform.localPosition = bones[i].transform.localPosition;
children[a].transform.localRotation = bones[i].transform.localRotation;
children[a].transform.localScale = bones[i].transform.localScale;
}
bones[i] = children[a];
break;
}
}
}
rend.bones = bones;
}
}
@emilianavt
Copy link
Author

Updated from here.

@emilianavt
Copy link
Author

You can find a short demonstration video here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment