wrote a Unity AssetPostprocessor to auto-apply FBX import settings by folder — tired of fixing the same checkboxes every import

368 views 0 replies

Working on a project with three animators dropping FBX files into Unity at various times. Every single import requires the same manual checkbox routine. Characters need humanoid rig and no animation, animation clips need avatar set to copy-from-other with materials disabled, props need secondary UV generation. You know the drill.

Wrote an AssetPostprocessor that reads the folder path and applies the right settings automatically on first import. The logic is dead simple. The frustrating part was figuring out which ModelImporter properties actually map to which inspector checkboxes. Docs are thin exactly where you need them.

using UnityEditor;

public class FBXImportConfigurator : AssetPostprocessor
{
    void OnPreprocessModel()
    {
        var importer = assetImporter as ModelImporter;
        if (importer == null) return;

        // Skip if already configured
        if (!string.IsNullOrEmpty(importer.userData)) return;

        string path = assetPath.ToLower();

        if (path.Contains("/characters/"))
        {
            importer.animationType = ModelImporterAnimationType.Human;
            importer.avatarSetup = ModelImporterAvatarSetup.CreateFromThisModel;
            importer.importAnimation = false;
            importer.importBlendShapes = true;
            importer.optimizeMeshPolygons = true;
        }
        else if (path.Contains("/animations/"))
        {
            importer.animationType = ModelImporterAnimationType.Human;
            importer.avatarSetup = ModelImporterAvatarSetup.CopyFromOther;
            importer.importAnimation = true;
            importer.importBlendShapes = false;
            importer.importVisibility = false;
            importer.importCameras = false;
            importer.importLights = false;
            importer.importMaterials = false;
        }
        else if (path.Contains("/props/") || path.Contains("/environment/"))
        {
            importer.animationType = ModelImporterAnimationType.None;
            importer.importAnimation = false;
            importer.generateSecondaryUV = true;
            importer.importBlendShapes = false;
            importer.importMaterials = false;
        }
        else
        {
            return; // Unknown folder — don't touch
        }

        importer.userData = "auto-configured";
    }
}

The userData field persists in the .meta file, so the early-out survives reimports cleanly. Want to re-run auto-config on a specific asset? Clear that field in the .meta and reimport.

The one unsolved piece: CopyFromOther avatar assignment. Setting the avatar setup type works fine programmatically, but actually pointing the clip importer at the right source avatar still requires a manual drag. The sourceAvatar property exists on ModelImporter, but getting a reliable reference to the correct avatar at import time is messy. If you're batch-importing a character and its animations together, the avatar asset might not exist yet when the clips get processed.

Anyone solved that cross-reference problem cleanly, or is it always going to be a manual step?

Moonjump
Forum Search Shader Sandbox
Sign In Register