Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Adding animationcurves to imported model from script (AssetPostProcessor)

Discussion in 'Scripting' started by Theformand, Oct 6, 2015.

  1. Theformand

    Theformand

    Joined:
    Jan 2, 2010
    Posts:
    271
    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?

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. public class EffectsImporter : AssetPostprocessor {
    8.  
    9.  
    10.     void OnPostprocessGameObjectWithUserProperties(GameObject go, string[] propNames, System.Object[] values)
    11.     {
    12.         Debug.Log("OnPostprocessGameObjectWithUserProperties");
    13.         ModelImporter mi = assetImporter as ModelImporter;
    14.  
    15.         for (int i = 0; i < propNames.Length; i++)
    16.         {
    17.  
    18.            
    19.             if (propNames[i].ToLower().Contains("alpha"))
    20.             {
    21.                 ClipAnimationInfoCurve clipAnimInfoCurve = new ClipAnimationInfoCurve();
    22.                 AnimationCurve alphaCurve = new AnimationCurve();
    23.  
    24.                 if (mi.clipAnimations[0].curves.Length < 1)
    25.                 {
    26.                     Debug.Log("0 existing curves");
    27.                 }
    28.  
    29.                 List<ClipAnimationInfoCurve> allcurves = new List<ClipAnimationInfoCurve>(mi.clipAnimations[0].curves);
    30.  
    31.  
    32.  
    33.                 int keyFrame = alphaCurve.AddKey(0.2f, (float)values[i]);
    34.                 int endFrame = alphaCurve.AddKey(1f, 1f);
    35.  
    36.                 clipAnimInfoCurve.curve = alphaCurve;
    37.  
    38.                 Debug.Log("keyframe result: "+clipAnimInfoCurve.curve.keys.Length);
    39.                
    40.                 allcurves.Add(clipAnimInfoCurve);
    41.  
    42.                 Debug.Log("trying to add "+allcurves.Count+ " curves"); // this prints 1 curve.
    43.              
    44.                 mi.clipAnimations[0].curves = allcurves.ToArray();
    45.                 Debug.Log("mi clip length: "+mi.clipAnimations[0].curves.Length); // this prints 0
    46.                 AssetDatabase.WriteImportSettingsIfDirty(mi.assetPath);
    47.                 AssetDatabase.SaveAssets();
    48.                 //This results in 0 curves saved
    49.  
    50.             }
    51.         }
    52.  
    53.      
    54.     }
    55. }
    56.  
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,226
  3. Theformand

    Theformand

    Joined:
    Jan 2, 2010
    Posts:
    271
    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 :)
     
    karl_jones likes this.
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,226
    No problem, glad it works. I also learnt something new :)