Adding animationcurves to imported model from script (AssetPostProcessor)

Hello all.

I’m trying to reduce the manual labor involved in our art pipeline from Maya → Unity. Specifically, by using custom attributes to transfer and import alpha animation keys (these are not supported by default).

I’m stuck on generating curves for Mecanim in an AssetPostProcessor. You can see my script below, and you can see in the comments what the different debug logs are giving me. No animationcurves are being saved. The reason I’m doing this, is because we have a system in place to use these curves in Mecanim to drive all of our alpha and UV-scroll animation.

Just to be clear, this image shows which curves I’m trying to modify from script:

Any help here?

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

public class EffectsImporter : AssetPostprocessor {


    void OnPostprocessGameObjectWithUserProperties(GameObject go, string[] propNames, System.Object[] values)
    {
        Debug.Log("OnPostprocessGameObjectWithUserProperties");
        ModelImporter mi = assetImporter as ModelImporter;

        for (int i = 0; i < propNames.Length; i++)
        {

           
            if (propNames[i].ToLower().Contains("alpha"))
            {
                ClipAnimationInfoCurve clipAnimInfoCurve = new ClipAnimationInfoCurve();
                AnimationCurve alphaCurve = new AnimationCurve();

                if (mi.clipAnimations[0].curves.Length < 1)
                {
                    Debug.Log("0 existing curves");
                }

                List<ClipAnimationInfoCurve> allcurves = new List<ClipAnimationInfoCurve>(mi.clipAnimations[0].curves);



                int keyFrame = alphaCurve.AddKey(0.2f, (float)values[i]);
                int endFrame = alphaCurve.AddKey(1f, 1f);

                clipAnimInfoCurve.curve = alphaCurve;

                Debug.Log("keyframe result: "+clipAnimInfoCurve.curve.keys.Length);
               
                allcurves.Add(clipAnimInfoCurve);

                Debug.Log("trying to add "+allcurves.Count+ " curves"); // this prints 1 curve.
              
                mi.clipAnimations[0].curves = allcurves.ToArray();
                Debug.Log("mi clip length: "+mi.clipAnimations[0].curves.Length); // this prints 0
                AssetDatabase.WriteImportSettingsIfDirty(mi.assetPath);
                AssetDatabase.SaveAssets();
                //This results in 0 curves saved

            }
        }

      
    }
}

I am not entirely sure if the animation values can be changed in OnPostprocessGameObjectWithUserProperties however you may be able to store the new values and apply the change in OnPreprocessAnimation or alternatively save the animation clips as a new asset using CreateAsset.

I found out my issue. Moved it to OnPreprocessAnimation, and also I was trying to write to modelimporter.clipAnimations[0]. Thats not how it works, thats a copy of the array. Construct a new array, do my alpha modifications there and write it back to the clipAnimiations array of the modelImporter. Thanks for your help, and thanks for letting me pester you on Twitter. Hope you still got some work done today :slight_smile:

1 Like

No problem, glad it works. I also learnt something new :slight_smile: