Search Unity

access AnimationCurve info from AnimationClip?

Discussion in 'Scripting' started by Chien, Jul 25, 2010.

  1. Chien

    Chien

    Joined:
    May 1, 2009
    Posts:
    42
    Hi
    Could I get the AnimationCurve information from AnimationClip? I imported a asset from Maya, and I can only see the AnimationClip object in the editor, but I want to use the evaluation function of the AnimationCurve on the AnimationClip that I imported. Any idea? Thanks for the help!
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
  3. Chien

    Chien

    Joined:
    May 1, 2009
    Posts:
    42
    Thanks for the pointer. I wrote an editor script which will import Maya file and put the shader's transparency animation back to where it belongs. But it will require that to put the transparency animation to the y axis of a empty node which name is "xxx_alpha". See the maya file for details.


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. public class setAnimAlpha1 : AssetPostprocessor
    7. {
    8.    void OnPostprocessModel (GameObject go )
    9.     {
    10.         Animation animation = go.animation;
    11.  
    12.             if (animation != null  animation.clip != null)
    13.             {
    14.                 AnimationClipCurveData[] data = AnimationUtility.GetAllCurves(animation.clip, true);
    15.                
    16.                 foreach (AnimationClipCurveData cd in data)
    17.                 {
    18.                     if (cd.curve != null)
    19.                     {
    20.                         Debug.Log(cd.propertyName+" "+getGOName(cd.path));
    21.                         Debug.Log(cd.path);
    22.                         string goName=getGOName(cd.path);
    23.                         if ( (strContains(goName, "_alpha"))  (cd.propertyName=="m_LocalPosition.y") )
    24.                         {
    25.                             Debug.Log("----------->  "+nameNoAlpha(goName));
    26.                             cd.curve.postWrapMode = WrapMode.Loop;
    27.                             animation.clip.SetCurve (nameNoAlpha(goName), typeof(Material), "_Color.a", cd.curve);
    28.                             GameObject.Find(nameNoAlpha(goName)).renderer.sharedMaterial.shader = Shader.Find("Transparent/Diffuse");
    29.  
    30.                         }
    31.                        
    32.                    
    33.                     }
    34.                 }
    35.                
    36.             }
    37.     }
    38.    
    39.    
    40.     bool strContains(string myStr, string findStr)
    41.     {
    42.         int found=0;
    43.         int totalFinds=0;
    44.         for (int i= 0; i < myStr.Length; i++)
    45.         {
    46.           found = myStr.IndexOf(findStr, i);
    47.           if (found>0)
    48.           {
    49.              totalFinds++;
    50.              i = found;  
    51.           }
    52.          
    53.         }
    54.         if (totalFinds>0)
    55.             return true;
    56.         else
    57.             return false;
    58.     }
    59.    
    60.     string nameNoAlpha(string orgStr)
    61.     {
    62.         int found=0;
    63.         int lastFound=0;
    64.         for (int i= 0; i < orgStr.Length; i++)
    65.         {
    66.           found = orgStr.IndexOf("_alpha", i);
    67.             //Debug.Log(i+" "+found);
    68.           if (found>0)
    69.               lastFound=i=found;
    70.           else
    71.               break;
    72.            
    73.          
    74.         }
    75.         return(orgStr.Substring(0,lastFound));
    76.        
    77.     }
    78.  
    79.     string getGOName(string orgStr)
    80.     {
    81.         int found=0;
    82.         int lastFound=-1;
    83.         for (int i= 0; i < orgStr.Length; i++)
    84.         {
    85.           found = orgStr.IndexOf("/", i);
    86.             //Debug.Log(i+" "+found);
    87.           if (found>0)
    88.               lastFound=i=found;
    89.           else
    90.               break;
    91.  
    92.            
    93.          
    94.         }
    95.         return(orgStr.Substring(lastFound+1,orgStr.Length-lastFound-1));
    96.        
    97.     }
    98. }
    99.  
     

    Attached Files: