- Home /
Changing default animation type to Humanoid via script
Hello! I'm trying to change default animation type during .fbx import to Humanoid via Editor script using this little piece of code:
public class ChangeModelToHumanoid : AssetPostprocessor
{
void OnPreprocessModel()
{
ModelImporter modelImporter = assetImporter as ModelImporter;
modelImporter.animationType = ModelImporterAnimationType.Human;
EditorUtility.SetDirty(modelImporter);
EditorApplication.SaveAssets();
}
}
While it seems to change the setting properly, Unity is unable to automatically generate avatar for imported animations, displaying following warning:
Required human Bone Hips not found
Is there any proper way to change the dafault import option and still get a working avatar?
There seems to be unexposed operations under the hood.
I was going to try and manually convert a avatar using avatarhumanbuilder, however that requires data from a valid humandescription. That still doesn't cover changes to animations clips. Curve properties, etc. There is some hint of the data in the meta after a conversion. I was unable to find anything that works.
I was able to get bone path matching using a ht(humantemlate) file, but the steps to convert property names to humanoid are unknown to me. I don't understand the difference in animation file format before and after the conversion as well. (path -> properyname.x) vs Path.rotate in humanoid animation clip properties.
If the process is not exposed, or needs to be kept private, can we at least get a function to replicate the conversion process. This will make the automation of processing assets for unity into mechanim compatible much easier.
This would also allow for users to custom the automation process on models that don't 100% work.
For example I Converted a $$anonymous$$odel that was missing a foot bone, failed to convert. I would love to be able to add code during the import process to add missing bones.
Answer by Devilwhale · May 23, 2017 at 02:51 PM
This is what I ended up using. Using existing avatar as a template.
-Resources have a pre-converted model with the avatar (named as template "Max", "MakeHuman", etc)
-Input folder is where models will be auto imported and converted to humanoid.
private string[] templates = new string[] { "Max_", "MakeHuman_"};
//based on name prefix or if in a folder (Input/Max_/anymodel.fbx) will also work.
//matches up it's avatar from one in the resources folder
void OnPreprocessModel()
{
if (assetPath.Contains("Input"))
{
ModelImporter importer = (ModelImporter)assetImporter;
//Load in the preconfigured avatar from the resources folder
foreach (string template in templates)
{
if (assetPath.Contains(template))
{
//Switch to humanoid
importer.animationType = ModelImporterAnimationType.Human;
//Trim off the underscore
string trim = template.Substring(0, template.Length - 1);
//Apply the avatar from the resources folder
importer.sourceAvatar = Resources.Load<Animator>(trim).avatar;
break;
}
}
//Log a warning if we didn't find a match
Debug.LogWarning("Model prefix does not correspond to any pre-configured humanoid avatar.");
}
}
void OnPostprocessModel(GameObject model)
{
if (assetPath.Contains("Input"))
{
ModelImporter importer = (ModelImporter)assetImporter;
//Add the animator and preconfigure it.
if (!model.GetComponent<Animator>())
{
model.AddComponent<Animator>();
model.GetComponent<Animator>().avatar = importer.sourceAvatar;
model.GetComponent<Animator>().applyRootMotion = true;
}
}
}
Hi @Devilwhale ! Thanks for posting Your solution. I ended up doing almost exactly the same thing as You did. It seems that using a source avatar is the best way to tackle this problem right now (in my case, I also had to use a pre-generated human template file to create human and skeleton descriptions).
@maciekblady do you $$anonymous$$d posting what you did? Im stuck at the same problem right now and cant seem to fix it, even though i copied the Code from @Devilwhale
Why do you set the avatar in the postProcessmodel to sourceAvatar again? That doesnt change anything does it? Dont you want to set the Avatar back to the original Avatar which fbx File got imported?
Your answer
Follow this Question
Related Questions
Evaluating animation curves in the Timeline 1 Answer
Is there way to check "Add loop Frame" automatically when FBX importing? 2 Answers
Get current Animation Controller being edited by Animator (In Editor) 0 Answers
Animation is not running, but value is stuck, if Animatori s enabled! 0 Answers
Can I know why I can't control volume of audiomixergroup without exposing parameters? 0 Answers