Search Unity

Writing a Script to Minimize Retargeting Errors

Discussion in 'Animation' started by Forberg, Sep 18, 2022.

  1. Forberg

    Forberg

    Joined:
    Oct 27, 2018
    Posts:
    25
    Hi,

    As using 3rd party humanoid animations is such a common thing to do and foot sliding and imprecise animations is such a horrible thing to look at I would like to write a script that minimizes these errors.

    The workflow by hand:

    1. Edit the avatar that is being used to import the animations (e.g. convert them to humanoid mechanim animations)
    2. Save
    3. Test the animation on the mesh that you would use ingame
    4. Check the errors by looking at them from all directions
    5. Repeat until satisfied

    I was unable to find a way to edit the Avatar settings of an imported FBX (the prefab representation with the generated avatar attached)

    What I found so far:
    • You can build a modified Avatar from an existing one using AvatarBuilder.BuildHumanAvatar. You can then save this Avatar as an asset.
      HOWEVER: You cannot reference this avatar asset in "Copy From Other Avatar" in the Animation import window. This will result in an error trying to copy over the HumanDescription
    • I tried overwriting the exisiting auto-generated avatar by using EditorUtility.CopySerialized without success (the function goes through but does not do anything on the asset file)
      EDIT: The copying seems to do something. Loading the asset by script and checking the changed values confirm that they have been saved. It does not show in the editor though)
    • Editing the .meta file of the FBX, which contains the skeleton info is not recognized by the editor even after a restart - EDIT: I does seem to have some effect!
    • You can create human poses using the HumanPoseHandler - this seems like something for runtime use and unable to edit the avatar pose

    My Question:

    Is it possible in any shape or form to configure the generated Avatar of an imported FBX by script, i.e. the pose used for coversion to mechanim?
     
    Last edited: Sep 18, 2022
  2. Forberg

    Forberg

    Joined:
    Oct 27, 2018
    Posts:
    25
    In case anyone wonders:

    Code (CSharp):
    1.  HumanDescription sourceDescription = sourceAvatar.humanDescription;
    2.  
    3. // Edit description...
    4.  
    5. string sourcePath = AssetDatabase.GetAssetPath(sourceAvatar);
    6. ModelImporter modelImporter = (ModelImporter)AssetImporter.GetAtPath(sourcePath);
    7. modelImporter.humanDescription = ownDescription;
    8. modelImporter.SaveAndReimport();
    9.