Search Unity

Permanently modifying Avatar properties via script

Discussion in 'Animation' started by Lord_Eniac, Jul 4, 2021.

  1. Lord_Eniac

    Lord_Eniac

    Joined:
    Jan 28, 2020
    Posts:
    50
    I've been testing some animations retrieved from Mixamo on some Synty models. Synty has a wonderful tutorial on doing this, and it works nicely. That being said, it would be nice to automate the part of that process that involves deleting the Jaw and moving the transforms associated with the little finger to the middle finger for each hand.

    Gaining access to the Avatar is simple enough in an editor script, however I believe it is merely a copy of the actual Avatar, and any changes made to it in the script are not saved. Is there a way to alter an Avatar's properties in script? The Avatar is one of several children to an FBX asset (downloaded from Mixamo).

    Test code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEngine;
    5.  
    6. public class AdjustAvatar : EditorWindow
    7. {
    8.     public Avatar avatar = null;
    9.  
    10.     [MenuItem("Window/Modify Avatar")]
    11.     // Start is called before the first frame update
    12.     public static void ShowWindow()
    13.     {
    14.         EditorWindow.GetWindow(typeof(AdjustAvatar));
    15.     }
    16.  
    17.     public void OnGUI()
    18.     {
    19.         avatar  = EditorGUILayout.ObjectField(avatar, typeof(Avatar), true) as Avatar;
    20.  
    21.         if (GUILayout.Button((avatar != null && avatar.isHuman) ? "Do it!" :
    22.             ((avatar != null) ? "Avatar must be humanoid" : "Select an avatar")))
    23.         {
    24.             if (null == avatar)
    25.             {
    26.                 return;
    27.             }
    28.  
    29.             HumanDescription hd = avatar.humanDescription;
    30.          
    31.             // for my test case, I know that hd.human[29].humanName is
    32.             // "Left Little Proximal"
    33.  
    34.             Debug.Log($"humanName is {hd.human[29].humanName}");
    35.             // Always outputs "humanName is Left Little Proximal"
    36.  
    37.             hd.human[29].humanName = "Left Middle Proximal";
    38.  
    39.             Debug.Log($"humanName is {hd.human[29].humanName}");
    40.             // Always outputs "humanName is Left Middle Proximal"
    41.  
    42.             // The text does change, but subsequent presses of the button do
    43.             // not reflect the change.  I just don't know how to save it.
    44.         }
    45.     }
    46. }
    47.