[solved] How can i collect bone names and transforms?

I want to wear the armor to the character.

The character model has some bones, and has animation.

The armor model has some the bones of the same name as the character. However, it doesn’t have animation.

[Character Model’s bone]
pelvis
spine1
spine2
neck
head
left arm
right arm
left leg
right leg
…etc.

[Armor Model’s bone]
spine1
spine2
left arm
right arm.

I want to look for the bone of the same name from the character model and the armor model, and copy from Character bone transforms to Armor bone transforms.
As a result, I want to wear the armor to the character.

How can I collect the list(or array) of bone names and transforms from the GameObect?

Please show the sample code of C# or javascript.

I tested the code below.
However, the model of the armor stopped completely. Where of the code is wrong?

public class RefferAnimator : MonoBehaviour {

	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () 
	{
	}
	
	void LateUpdate()
	{
		GameObject characterObj = GameObject.Find("testBody");//this is character model object.
		Transform[] characterBones = characterObj.GetComponentInChildren<SkinnedMeshRenderer>().bones;
	
		SkinnedMeshRenderer smr = this.GetComponentInChildren<SkinnedMeshRenderer>();
		Transform[] thisBones = smr.bones;
	
		for(int i=0; i<thisBones.Length; i++)
		{
			for(int j=0; j<characterBones.Length; j++)
			{
				if(thisBones[i].name == characterBones[j].name)
				{
					thisBones[i] = characterBones[j];
				}
			}
		}
		
	}
}

I would approach it one of two ways–using recursion to match up the hierarchies or doing a full name lookup.

Recursion might look like:

	/**
	* Have one rig match another
	*/
	static function CopyTransformsRecurse (src:Transform, dst:Transform)
	{
		dst.position = src.position;
		dst.rotation = src.rotation;

		for (var child:Transform in dst)
		{
			// match the transform with the same name
			var curSrc = src.Find(child.name);
			
			if (curSrc)
				CopyTransformsRecurse(curSrc, child);
		}
	}

In your case, though, I think a name lookup will be more debug-able and more flexible (considering your armor/etc rigs are a subset of the main rig).

	/**
	* Get the full name of an object, including its parents to a point
	*/
	static function GetFullName(component:Component, relativeTo:Transform)
	{
		var name:String = component.name;
		var test:Transform = component.transform;
		
		while(test.parent != relativeTo)
		{
			name = test.parent.name + "/" + name;
			test = test.parent;
		}
		
		return name;
	}

Let’s say you have two rigs, your characterRoot and your armorRoot. You would do something like:

for(var t:Transform in armorRoot.GetComponentsInChildren(Transform))
{
	// full name of armor piece, relative to root
	var path:String = GetFullName(t, armorRoot);
	
	// find this piece in our character rig
	var match:Transform = characterRoot.Find(path);
	
	// parent it, store it for later movement matching, or whatever
	if(match)
	{
		// do something
	}
}

Thank you very much.
It works correctly.

I am most grateful!

The below should help. The character in position is assumed to be a shadowcharacter which you set and replace with another character the customer wants instead

using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;

public class BonesSetter : MonoBehaviour
{

    Transform ShadowCharacter;

    // three element list
    List< KeyValuePair<Transform,  KeyValuePair<Vector3, Quaternion>>> BoneList;

    void Start()
    {
        ReplaceShadowCharacter();
    }

    public void ReplaceShadowCharacter()
    {
        ShadowCharacter = gameObject.transform.Find("Character");

        BoneList = new List<KeyValuePair<Transform, KeyValuePair<Vector3, Quaternion>>>();

        FindBonesRecursive(ShadowCharacter, ShadowCharacter.childCount);
        Debug.Log("Recursive total Run: " + recursiveRun);

        Debug.Log("Total size list created: " + BoneList.Count);

        // read bones
        Debug.Log("Reading");
        foreach (KeyValuePair<Transform, KeyValuePair<Vector3, Quaternion>> KVP in BoneList)
        {
            Debug.Log("b: t=" + KVP.Key.name + " p=" + KVP.Value.Key + " r=" + KVP.Value.Value);
        }
        // character position for this vehicle
        Vector3 characterPosition = ShadowCharacter.position;
        Quaternion characterRotation = ShadowCharacter.rotation;

        // replace character with the one choosen spawn depending on network connected or not
        // resource load used for standard non photon instantiation
        GameObject newCharacter = Resources.Load("CharactersPrefabs/Richard") as GameObject;

        GameObject newCharacterInstance;

        if (PhotonNetwork.IsConnected)
        {
            // instantiate checking maszter client for the spot
            newCharacterInstance = PhotonNetwork.Instantiate(newCharacter.name,
                                                        characterPosition,
                                                        characterRotation);
        }
        else
        {
            // instantiation offline
            newCharacterInstance = Instantiate(newCharacter, characterPosition, characterRotation);
        }

        // make the new character child of the vehicle
        Transform vehicle = ShadowCharacter.parent;
        // set the new character child of this transform
        newCharacterInstance.transform.SetParent(vehicle);

        Debug.Log("REPLACE STARTING ");
        // create the bone structure of the
        recursiveRun = 0;
        ReplaceBonesRecursive(BoneList, newCharacterInstance.transform);

        Debug.Log("Recursive total REplacing Run: " + recursiveRun);

        // hide character used as model position
        ShadowCharacter.gameObject.SetActive(false);
    }


    int recursiveRun=0;
    void FindBonesRecursive(Transform t, int childCount)
    {
        //Debug.Log("root object count " + childCount);
        // t
        //Debug.Log("t " + t.name + " count "+childCount + " t position "+ t.position + " t rotation "+t.rotation);
        for (int i = 0; i < childCount; i++)
        {
            // t
            BoneList.Add(new KeyValuePair<Transform, KeyValuePair<Vector3, Quaternion>>(t.GetChild(i),
                         new KeyValuePair<Vector3, Quaternion>(t.GetChild(i).position, t.GetChild(i).rotation)));
            Debug.Log("b " + t.GetChild(i).name + " t position " + t.position + " t rotation " + t.rotation);
            // for each child find out if there are bones
            if (t.GetChild(i).childCount > 0)
            {
                // t recursive
                FindBonesRecursive(t.GetChild(i), t.GetChild(i).childCount);
            }
            recursiveRun++;
        }
       
    }

    void ReplaceBonesRecursive(List<KeyValuePair<Transform, KeyValuePair<Vector3, Quaternion>>> boneStructure, Transform newCharacterT)
    {
        // go through the bones of the new character. If the bone matches with the list, add position and rotation
        for (int i = 0; i < newCharacterT.childCount; i++)
        {
            // t
            // search the list to find same name bones
            foreach (KeyValuePair<Transform, KeyValuePair<Vector3, Quaternion>> KVP in boneStructure)
            {
                if (KVP.Key.name == newCharacterT.name)
                {
                    Debug.Log("b replace: t=" + KVP.Key.name + " p=" + KVP.Value.Key + " r=" + KVP.Value.Value);

                    newCharacterT.position = KVP.Value.Key;
                    newCharacterT.rotation = KVP.Value.Value;
                    recursiveRun++;

                }
            }

            Debug.Log("b new character " + newCharacterT.GetChild(i).name + " t position " + newCharacterT.position + " t rotation " + newCharacterT.rotation);
            // for each child find out if there are bones
            if (newCharacterT.GetChild(i).childCount > 0)
            {
                // t recursive
                ReplaceBonesRecursive(boneStructure, newCharacterT.GetChild(i));
            }
        }

    }





}
2 Likes