- Home /
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:
Why is this not working?
Are there any AvatarBuilder.BuildHumanAvatar examples available?
Should it even be possible to use this code in runtime, or is it meant to be used in the editor?
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!
I'm working on this problem too.
2) No, I do not believe there are any examples.
3) I do believe it is okay for runtime since these class documentations are in the runtime section of the Unity documentation.
The other questions I don't have answers for but am working to figure this whole thing out.
A couple things I am curious about:
1) I can't find a function called CreateSkeletonBone() in the API. I get this compile error:
error CS0103: The name `CreateSkeletonBone' does not exist in the current context
2) Assu$$anonymous$$g such a function exists, what deter$$anonymous$$es what you're putting in there as a parameter?
Hi lcairco,
The functions CreateHumanBone and CreateSkeletonBone are my own. I should probably have posted them too, since they do construct an important part of the humanDescription. I start with an already existing GameObject hierarchy. For now I try to recreate an avatar that I already have. $$anonymous$$y final goal is to deconstruct the bone structures of existing characters en construct them again in different combinations. This allows me to use one retargeted animation for a combinations of characters with varying proportions (small arms of character 1, tall legs of character 2, head of character 3 etc).
I have this code at work, but I am at home today, so I can only give you a description of what they do:
CreateSkeletonBone(): I pass the function the name of the bone in the object hierarchy. In the function I set this name to the SkeletonBone.name. I use a recursive search to find the corresponding bone in my gameObject hierarchy, and use the local position, rotation and scale to set the transform.
CreateHumanBone(): I use the two names passed to set the boneName and humanName. The limits are left to default and I set the useDefaultValues to true
I hope you can work with this. Please let me know if you can get to work.
Regards, Ilja
Answer by lcairco · Jan 27, 2014 at 08:29 PM
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 :) Make the filenames camel-cased and change the extension to .cs for them to work.
[1]: /storage/temp/21236-buildavatar.txt
Thank you very much for sharing your experience and implementation! Due to time constraints I am using a different approach for now. One with multiple character animations merged in to one. This might result in performance problems on phone hardware though. If so, I will come back at looking into creating a merged avatar again.
Sorry for re-war$$anonymous$$g question after one year, but this is ONLY topic abut Building Avatar.
When I do everything follow lcairco instruction and press Play, I see: AvatarBuilder '$$anonymous$$ac_A_05(Clone)': Hips bone parent '$$anonymous$$ac_A_05(Clone)' must be included in the HumanDescription Skeleton $$anonymous$$y hierarchy is on image. Of course with "(clone)" in the name after PLAY.
I tried a few ways but no success. Have you also hade to fix that?!
Yes. You need to do exactly what it says--make sure that $$anonymous$$ac_A_05(Clone) is in the array you pass into HumanDescription.
Hi lcairco. Those are very nice script and I wish I could build that damn avatar. Unfortunately, I stumbled uppon the same problem as panbook, which I resolved (or so I thought) by adding the line "none,root" in the skeleton file and "root,0,0,0,0,0,0,1,1,1,1" in the tpose one. I also had to rename the freshly instantiated character to remove the "(Clone)", or the bone's name wouldn't match.
Problem is, it crashes the editor (unity 5). Here is a package if anyone wants to try : https://dl.dropboxusercontent.com/u/82985785/Unity/avatar.unitypackage
Hi , I have a doubt. Can we modify HumanDescription structure of imported Avatar from .fbx file?
Answer by RatherGood · Jul 20, 2015 at 08:56 AM
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!