Rename a clip using AssetPostProcessor

When an animation is imported from an FBX, it creates a clip named “take 001.” I would like to rename “take 001” to the name of the FBX automatically on import using AssetPostProcessor. We’ve successfully renamed clips by doing this:

ModelImporter importer = assetImporter as ModelImporter;
importer.clipAnimations = ClipsWithNewName();

but that overrides all the clip settings, including time range. All my fbx’s have the exact time range I need and unity detects that time range automatically. So if I could access the time range that unity sets automatically from the FBX’s time range, I could successfully recreate the clip.

Ideally, I would like to rename “take 001” in ‘OnPostprocessModel’ without affecting the other clip settings or having to recreate the clip settings in the ModelImporter.

I am using Unity 4.6, so it appears I dont have access to ‘defaultClipAnimations’, which possibly could help me, though Im not sure exactly how that works.

Any help would be much appreciated, thanks!

I had a similar problem (importing and renaming Cinema 4D clips) and stumbled upon this post. In case someone else wants to do this as well, here is my solution:

using UnityEngine;
using UnityEditor;

public class C4DModelClipImporter : AssetPostprocessor {
    
    void OnPreprocessAnimation() {
        ModelImporter modelImporter = assetImporter as ModelImporter;
        if(modelImporter.clipAnimations.Length > 0) {
            // we already have non-default clip animations set, so we don't change anything
            return;
        }
        ModelImporterClipAnimation[] clipAnimations = modelImporter.defaultClipAnimations;
        if(clipAnimations.Length == 1 && clipAnimations[0] != null && clipAnimations[0].name.Contains("CINEMA")) {
            // only rename if we have exactly one clip that contains "CINEMA"
            clipAnimations[0].name = GetAssetName();
            modelImporter.clipAnimations = clipAnimations;
        }
    }

    private string GetAssetName() {
        int begin = assetPath.LastIndexOf('/');
        if(begin < 0) {
            begin = 0;
        }
        int end = assetPath.LastIndexOf('.');
        if(end < 0) {
            end = assetPath.Length - 1;
        }
        return assetPath.Substring(begin + 1, end - begin - 1);
    }
}

Tested only with 2018.1

Cheers,

Chris