Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

AnimatorController creation in AssetPostprocessor - issue with AnimatorState.motion

Discussion in 'Animation' started by FabulaLeonis, Feb 18, 2017.

  1. FabulaLeonis

    FabulaLeonis

    Joined:
    Apr 11, 2015
    Posts:
    11
    Hello,

    I'm trying to create a post processor that would generate automatically a dedicated animator controller for each character FBX.

    I managed to get a few things working (custom object instantiation based on bones custom properties, automatic animator controller creation with 1 state per animation..), but I can't seem to assign my animation clips to the motion of each animator state.

    I have the following code:

    Code (CSharp):
    1.         // Creates the controller
    2.         AnimatorController controller = AnimatorController.CreateAnimatorControllerAtPath(assetPath.Substring(0, assetPath.LastIndexOf(".")) + "_anim.controller");
    3.         Animator anim = g.GetComponent<Animator>();
    4.         anim.runtimeAnimatorController = controller;
    5.         AnimatorStateMachine rootStateMachine = controller.layers[0].stateMachine;
    6.  
    7.         // add all anim states automatically
    8.         AnimationClip[] clips = AnimationUtility.GetAnimationClips(g);
    9.         if (clips.Length == 0)
    10.         {
    11.             clips = UnityEngine.Object.FindObjectsOfType<AnimationClip>();
    12.         }
    13.  
    14.         foreach (AnimationClip ac in clips)
    15.         {
    16.             // add to controller
    17.             AnimatorState state = rootStateMachine.AddState(ac.name);
    18.             state.motion = ac;      // this here ! doesn't seem to work
    19.         }
    20.  
    When debugging the code, the motion seems to be properly applied, but once this whole routine is over the editor shows me "none" in each generated state's motion.

    Note that when debugging AnimationUtility.GetAnimationClips never finds anything therefore going into UnityEngine.Object.FindObjectsOfType<AnimationClip> every time.

    What do you think ?

    Thanks!

    Note: on a side note, I also have an issue where all animation clips from the FBX file receive a prefix to their name (shows up like this "armature_name|clip_name" instead of just "clip_name"). Probably more related to my poor understanding of Blender though.
     
  2. FabulaLeonis

    FabulaLeonis

    Joined:
    Apr 11, 2015
    Posts:
    11
    Ok so, after a lot of try and error I was actually pretty close to the solution when I posted this.

    I checked the following post: https://forum.unity3d.com/threads/getting-animationclip-from-fbx-in-the-editor-mecanim.295229/

    ... and modified the method to fetch all animation clips from the imported asset to the following:

    Code (CSharp):
    1. UnityEngine.Object[] objects = AssetDatabase.LoadAllAssetsAtPath(assetPath);
    2.         foreach (UnityEngine.Object obj in objects)
    3.         {
    4.             AnimationClip clip = obj as AnimationClip;
    5.             if (clip != null)
    6.             {
    7.                 AnimatorState state = rootStateMachine.AddState(clip.name);
    8.                 state.motion = clip;
    9.             }
    10.         }
    And that seems to work :)