How to use AvatarBuilder.BuildHumanAvatar?

I want to use the AvatarBuilder.BuildHumanAvatar to create custom avatar compositions in runtime, but am not successful in making it work. I can’t find any documentation or examples about the AvatarBuilder, so I hope to find more answers here.

This is the code I use to build the HumanDescription and create the avatar, with two helper functions to create the HumanBone and the Skeleton Bone:

        HumanDescription humanDescription = new HumanDescription();
        humanDescription.human = new HumanBone[18];
        humanDescription.human[0] = CreateHumanBone("Hips", "Base HumanPelvis001");
        //...
        humanDescription.human[17] = CreateHumanBone("Head", "Base HumanPelvis002Bone001");

        humanDescription.skeleton = new SkeletonBone[26];
        humanDescription.skeleton[0] = CreateSkeletonBone("Composite");
        //...
        humanDescription.skeleton[25] = CreateSkeletonBone("Torso");

        humanDescription.upperArmTwist = 0.5f;
        humanDescription.lowerArmTwist = 0.5f;
        humanDescription.upperLegTwist = 0.5f;
        humanDescription.lowerLegTwist = 0.5f;
        humanDescription.armStretch = 0.05f;
        humanDescription.legStretch = 0.05f;
        humanDescription.feetSpacing = 0.0f;

        Animator animatorComponent = compositeCharacter.AddComponent<Animator>();
        animatorComponent.runtimeAnimatorController = controller;
	    animatorComponent.avatar = AvatarBuilder.BuildHumanAvatar(compositeCharacter, humanDescription);

The code is correctly executed, no warnings or errors, but the avatar doesn’t seem to be correct since the character doesn’t start moving. The limits on the HumanBone are all kept at zero, with useDefaultValues set to true, the SkeletonBones use the local pos/rot/scale of the similar named gameobject in the hierarchy. When I use an avatar from my resources the character moves correctly.

My questions are:

  1. Why is this not working?
  2. Are there any AvatarBuilder.BuildHumanAvatar examples available?
  3. Should it even be possible to use this code in runtime, or is it meant to be used in the editor?
  4. What object in the hierarchy do I pass as the first parameter in the AvatarBuilder.BuildHumanAvatar, the one with the Animator object on it, or the sub object with the root of the bone structure.

Thanks in advance!

All right, I have an example working on my end!

I basically took the same approach you did, so I’m not entirely sure what the differences are. I think the trickiest part for me to figure out was the t-pose mapping transform held in the SkeletonBones, since my characters weren’t in t-pose to begin with. Before I got that right I had all kinds of problems.

What I ended up doing was automapping one character with the same skeletal structure as my set of characters, then enforcing a tpose there. Then, I exported that data into a text file that I could read in later to get the character into a t-pose. After that, everything worked fine.

I wrote two scripts to do this. The AvatarUtils script has instructions for getting the t-pose information, while the BuildAvatar script does all the character creation work. Right now everything’s loaded in from the Resources folder but of course you could change that. The only fiddly thing you might have to change is that in my skeleton mapping I have this “root” bone that is parent to the hips bone that changes names for each model, so I had to do some stuff with my text parsing to fix that. But otherwise everything’s fairly generic.

To do the composite thing you’re trying to accomplish, I’m guessing that you’d probably have to fiddle with those t-pose translations to make them all fit together nicely.

I don’t really know a better way to attempt to help besides providing my scripts. So here goes. If you happen to use this for academic work, I’d appreciate a citation or acknowledgement. I don’t know how my university feels about intellectual property, but it says I retain the right to distribute for educational or research purposes so we’ll go with that :slight_smile: Make the filenames camel-cased and change the extension to .cs for them to work.

Hello, I have been struggling with this myself. I found one big hint to get animations to work. Have been messing with this way too long. Has to be a bug.

 Transform parent = transform.parent;
 transform.parent = null;

...build the Avatar as your code above

transform.parent = parent;

This makes animations work.

However, My editor is crashing quite often during the build

“avatar = AvatarBuilder.BuildHumanAvatar(gameObject, desc);”

Freezes right there. Still not sure why. Any thoughts?

Edit: Just want to add the reason this works is setting the parent to null puts the game object at the base level of the scene. Probably has to do with the animation looking for a specific transform hierarchy. So if you build your avatar at the base level everything works fine also. As soon as you parent it no animation plays. Took quite a bit of head scratching for me to figure that out!

Edit2: The crash seems to be due to my poor coding I haven’t tracked down (or bug?) who knows :(. Somewhere I’m losing the reference to the hips transform in my custom editor. reference = missing!, so missing is not null and causes the editor crash! How to check for missing??? Too much code to post here and if it’s off topic I apologize for hijacking your thread.

Thank you for sharing your code. I am using it to build skinned meshes and bones right in the editor and creating an avatar that immediately allows me and animate it. Fun stuff!

hi~could you please tell me how to exported that data into a text file ?I want to explose the data to text ,but the file is messy code. the unity explosed the file is .ht file.I can not open the the file.Is there any way to explosed the data to text?