Search Unity

Is there a way to remove RootT.(x,y,z) curves/keys from an animation via code?

Discussion in 'Animation' started by MostHated, Nov 19, 2019.

  1. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    Hey all,
    I have been searching far and wide for a way to convert root motion to in-place motion on animation via code but have been unable to find one. I keep thinking I might be close only to find out I mixed and matched code that works with humanoid and legacy, ended up being really old, etc.

    It looks like using something like this *might work* but it only gets to "Num of clips" and reports back 0 when passing in an fbx file via an editorwindow I have been creating.

    Code (CSharp):
    1.  
    2. public readonly EditorCurveBinding[] curveBindings =
    3. {
    4.   EditorCurveBinding.FloatCurve("", typeof(Animator), "RootT.x"),
    5.   EditorCurveBinding.FloatCurve("", typeof(Animator), "RootT.y"),
    6.   EditorCurveBinding.FloatCurve("", typeof(Animator), "RootT.z"),
    7. };
    8.  
    9. private void ConvertToInPlace(GameObject root)
    10. {
    11.   Debug.LogFormat("Root Object: {0}", root.name);
    12.  
    13.   var clips = AnimationUtility.GetAnimationClips(root);
    14.   Debug.LogFormat("Num of clips: {0}", clips.Length);
    15.  
    16.   foreach (var clip in clips)
    17.   {
    18.     Debug.LogFormat("Name of clip: {0}", clip.name);
    19.     for (int i = 0; i < AnimationCurveBindingAnimatorRootT.Length; i++)
    20.     {
    21.       Debug.LogFormat("Name of property: {0}", AnimationCurveBindingAnimatorRootT[i].propertyName);
    22.       AnimationUtility.SetEditorCurve(clip, AnimationCurveBindingAnimatorRootT[i], null);
    23.     }
    24.   }
    25. }
    26.  

    In another separate method I have, this code below works just fine and processes all of the animation clips with no issue, but the problem with that is, I can't find any way to take any of the resulting ClipAnimations or Objects and apply a change to the curves/keyframes. This is the one in which I kept coming across outdated and mixed legacy, generic, and humanoid code and could not get any of it working.

    Code (CSharp):
    1.  
    2.  
    3. public void ProcessAnimations(GameObject root)
    4. {
    5.   string path = AssetDatabase.GetAssetPath(root);
    6.   ModelImporter modelImporter = AssetImporter.GetAtPath(path) as ModelImporter;
    7.   ModelImporterClipAnimation[] clipAnimations = modelImporter.defaultClipAnimations;
    8.  
    9.   foreach (var clip in clipAnimations)
    10.   {
    11.     clip.loop = loop;
    12.     clip.loopTime = loopTime;
    13.     // etc, etc,
    14.   }
    15.   modelImporter.clipAnimations = clipAnimations;
    16.   modelImporter.SaveAndReimport();
    17. }
    18.  
    19.  

    The whole purpose of this is because I am making a small tool to make things easier when bringing in animations, so they all come in as FBX and ideally would be able to stay that way instead of ending up with a bunch of loose *.anim files, but I am honestly not sure if that is possible. Might anyone be able to fill me in on what I might be missing?

    I am restricted my tool /codebase to Unity 2017.1.5 for compatibility purposes, but after spending the day going through Github, I came across a bunch of repos that *seem* like the code might be able to do it, but there was never any confirmation.

    If anyone has any suggestions or ideas to throw my way, I would appreciate it.
    Thanks,
    -MH
     
  2. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    I was able to get the projects updated to 2017.4 LTS, so I am able to use the FBX exporter, but so far I have been unable to get it to work properly. The models all goof up and end up all over the place, but the animation seemingly comes out ok. I suppose issues with the FBX exporter belong elsewhere, though.