Batch setting FBX animation import settings

Is there any way to batch set clip settings for animation imports, ie the settings shown below in an FBX animation from Mixamo?

I understand that errors can arise due to setting them wrong, however, there are more changes required than resetting the ones that don’t need setting.

Hi @craigjwhitmore ,

You can use AssetPreprocessor, but it seems that many of the examples out there from a few years back in time don’t work properly. Afaik, if you try to use SaveAndReimport to apply settings, import will get stuck in a loop, so better not use it.

Put the script in an Editor folder, otherwise it won’t react to the callbacks.

I think it’s also better and safer to configure your script to only process animations in certain folder (imho) so that you can limit the custom importer’s effect just those folders…

This script gets the defaultAnimationClips, modifies them and then feeds them back after modifications. If you don’t set that modelImporter.clipAnimations like I do in my example, the values don’t get applied.

using UnityEngine;
using UnityEditor;

public class CustomImportAnimation : AssetPostprocessor
{
    void OnPreprocessAnimation()
    {
        if (assetPath.Contains("Animations"))
        {
            ModelImporter modelImporter = assetImporter as ModelImporter;
            var animations = modelImporter.defaultClipAnimations;

            for (int i=0; i <animations.Length; i++)
            {
                animations[i].loopTime = true;
                animations[i].loopPose = true;
            }

            modelImporter.clipAnimations = animations;

            Debug.Log("Imported animation with custom settings.");
        }
    }
}

Those other import settings are easy to modify, most of them can be changed by just setting a value.

5 Likes

Thanks ever so much, that is really helpful. I’ll be sure to be careful using this.

Glad I could help you. Why not smash that like button then :slight_smile:

One more thing abou using those custom importers; think about situation where you (for some reason) apply Reimport All and you’ve forgotten that you had that kind of script (and haven’t built any checks/limits into it.). It might potentially mess up a big amount of your content in your project… Backups or not, it will take time to restore things as they were.

That’s good advice. I think i’ll use a staging project for importing the animations for this script, then copy them over to my working project. Less chance of mass error.

This is my code, however I get an error. I have very little experience with the editor, but it still works when I do a ReImport.

ModelImporterEditor.OnInspectorGUI must call ApplyRevertGUI to avoid unexpected behaviour.
UnityEditor.ModelImporterEditor:OnDisable()

Odd, the error seems to have gone away and I haven’t done anything.

public class CustomAnimationImport : AssetPostprocessor
{
    private string SourceModelPath = "Assets/Character.fbx";
    void OnPreprocessAnimation()
    {
        if (assetPath.Contains("Animations"))
        {
            ModelImporter modelImporter = assetImporter as ModelImporter;
            var animations = modelImporter.defaultClipAnimations;

            ConfigureRig(modelImporter);
            ProcessAllAnimations(animations);
            ProcessLoopingAnimations(animations);
            ProcessOneTimeAnimations(animations);
            modelImporter.clipAnimations = animations;
            Debug.Log("Imported animation with custom settings.");
        }
    }
    private void ConfigureRig(ModelImporter model)
    {
        var fbxAsset = AssetDatabase.LoadAssetAtPath<GameObject>(SourceModelPath);
        model.animationType = ModelImporterAnimationType.Human;      
        var avatar = fbxAsset.GetComponent<Animator>().avatar;
        model.sourceAvatar = avatar;
    }
    private void ProcessAllAnimations(ModelImporterClipAnimation[] animations)
    {
        for (int i = 0; i < animations.Length; i++)
        {
            animations[i].lockRootRotation = true;
            animations[i].lockRootHeightY = true;
            animations[i].keepOriginalOrientation = true;
            animations[i].keepOriginalPositionXZ = true;
            animations[i].keepOriginalPositionY = true;
        }
    }
    private void ProcessOneTimeAnimations(ModelImporterClipAnimation[] animations)
    {
        if (assetPath.Contains("OneTime"))
        {
            for (int i = 0; i < animations.Length; i++)
            {
                animations[i].loopTime = false;
                animations[i].loopPose = false;
            }
        }
    }
    private void ProcessLoopingAnimations(ModelImporterClipAnimation[] animations)
    {
        if (assetPath.Contains("Looping"))
        {
            for (int i = 0; i < animations.Length; i++)
            {
                animations[i].loopTime = true;
                animations[i].loopPose = true;
            }
        }
    }
}

I really want to know if you have ever met this problem now. I wrote an Editor Script before and now every time I select multiple animation clips it shows the error message
"
ModelImporterEditor.OnInspectorGUI must call ApplyRevertGUI to avoid unexpected behavior.
UnityEditor.ModelImporterEditor:OnDisable()
"

EVEN WHEN I CREATE A NEW PROJECT!!!

I can’t answer your question. since writing this editor, I haven’t had the error again and have installed several new versions of Unity.

I ultimately stopped using this script, as I was getting some other issues with the animations and was not sure if it was this script or my editing of the anims causing the issue. I’ve also installed a dozen different versions of Unity since I stopped using this script, so I can’t say that I’ve ever had the error appear again.

You could try reinstalling Unity perhaps. Sorry I can’t be of more help.