Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Modifying an Avatar in editor script

Discussion in 'Animation' started by dgoyette, Oct 3, 2023.

  1. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,192
    I import several characters as FBX files, which use a Humanoid rig. In order to get my lipsync tool to behave properly, I need to make sure that the "Jaw" bone is unset in the Avatar configuration. Unfortunately, any time I import an updated version of the FBX, the Jaw bone gets set again, and I have to keep going in to the Avatar config to set it to None:

    upload_2023-10-3_11-53-53.png

    I'd like to do this in a script. It's simple enough to get the avatar in code, and to modify its Human Description. But at that point, I don't know how to reassign the new avatar to the FBX. Here's what I'm doing now. The final bit with
    anim.avatar = newAvatar;
    doesn't do anything to the FBX file itself, so this code effectively doesn't work.

    Does anyone know of a way to permanently edit the humanoid mapping via an editor script? (This is code I'd expect to call just in the Unity Editor, not at runtime.)

    Code (CSharp):
    1.     public static class FBXUtil
    2.     {
    3.         [MenuItem("Gravia/FixFBX")]
    4.         public static void ProcessFBX()
    5.         {
    6.             foreach (var o in Selection.objects)
    7.             {
    8.                 var go = o as GameObject;
    9.                 if (go != null)
    10.                 {
    11.                     var anim = go.GetComponent<Animator>();
    12.                     if (anim != null)
    13.                     {
    14.                         var avatar = anim.avatar;
    15.                         var humanDescription = avatar.humanDescription;
    16.                         humanDescription.human = humanDescription.human.Where(b => b.boneName != "CC_Base_JawRoot").ToArray();
    17.                         var newAvatar = AvatarBuilder.BuildHumanAvatar(go, humanDescription);
    18.                         if (newAvatar.isValid)
    19.                         {
    20.                             Debug.Log("New avatar is valid.");
    21.                             anim.avatar = newAvatar;
    22.                         }
    23.                     }
    24.                 }
    25.             }
    26.         }
    27.     }