Search Unity

Unity EditorScript to parse animation clip and store in a JSON file.

Discussion in 'Scripting' started by appearance, Jan 30, 2013.

  1. appearance

    appearance

    Joined:
    Sep 3, 2012
    Posts:
    7
    Hi everyone,

    I was in need of such a editor script which can parse the animation clip(s) and store it's curve / keyframe values to a text file (preferably in JSON format). I failed to find such script. Here is the script I wrote myself. Hope some one would benefit from it.

    (Usage: Drag and drop all the animation clips that you like to parse onto a game object in Hierarchy window. Then, select that game object and run this script from 'Animation/Parse Clip(s)' menu.)

    (Upcoming: Possibly in my next post, I will write a script which will read this JSON file and create the animation clip on the fly to use it with game object(s).

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5. using System.Text;
    6. using System.IO;
    7.  
    8. public class AnimReaderEditor {
    9.     private static StringBuilder sb;
    10.     private static string folder;
    11.     private static string jsonFileExt = ".json";
    12.     private static float progress = 0.0f;
    13.    
    14.     [MenuItem("Animation/Parse Clip(s)")]
    15.     public static void ParseAnimation () {
    16.         if (Selection.gameObjects.Length < 1) {
    17.             EditorUtility.DisplayDialog ("Animation Reader", "Select a game object which has 'Animation' component with " +
    18.                 "Animation Clips in it to parse the clips.", "Back");
    19.             return;
    20.         }
    21.         Animation animation = Selection.activeGameObject.animation;
    22.         if (animation == null) {
    23.             EditorUtility.DisplayDialog ("Animation Reader", "Selected object (" + Selection.activeGameObject.name + ") doesn't have \"Animation\" component.", "Back");
    24.             return;
    25.         }
    26.         AnimationClip[] clips = AnimationUtility.GetAnimationClips (animation);
    27.         if (clips.Length > 0) {
    28.             folder = EditorUtility.SaveFolderPanel ("Save Clip", "", animation.gameObject.name);
    29.             //foreach (AnimationClip clip in clips) {
    30.             for (int i=0; i<clips.Length; i++) {
    31.                 AnimationClip clip = clips [i];
    32.                 progress = ((float)clips.Length) / (float)i;
    33.                 EditorUtility.DisplayProgressBar ("Animation Reader", clip.name, progress);
    34.                 ParseClip (clip);
    35.             }
    36.             EditorUtility.ClearProgressBar ();
    37.         }
    38.     }
    39.    
    40.     private static void ParseClip (AnimationClip clip) {
    41.         sb = new StringBuilder ();
    42.         BeginObject ();
    43.         WriteString ("clipname", clip.name); Next ();
    44.         WriteString ("wrapmode", clip.wrapMode.ToString ()); Next ();
    45.         BeginArray ("curves");
    46.        
    47.         AnimationClipCurveData[] cdataarray = AnimationUtility.GetAllCurves (clip, true);
    48.         int l = ((AnimationClipCurveData[])cdataarray).Length;
    49.         for (int x=0; x<l; x++) {
    50.             AnimationClipCurveData cdata = cdataarray [x];
    51.             BeginObject ();
    52.             WriteString ("property_name", cdata.propertyName); Next ();
    53.             WriteString ("type", cdata.type.Name); Next ();
    54.             BeginArray ("keys");
    55.             Keyframe[] keys = cdata.curve.keys;
    56.             for (int i=0; i<keys.Length; i++) {
    57.                 Keyframe kf = keys [i];
    58.                 BeginObject ();
    59.                 WriteFloat ("time", kf.time); Next ();
    60.                 WriteFloat ("value", kf.value); Next ();
    61.                 WriteFloat ("intangent", kf.inTangent); Next ();
    62.                 WriteFloat ("outtangent", kf.outTangent);
    63.                 EndObject ();
    64.                 if (i<keys.Length-1)
    65.                     Next ();
    66.             }
    67.             EndArray ();
    68.             EndObject ();
    69.             if (x < l-1)
    70.                 Next ();
    71.         }
    72.         EndArray ();
    73.         EndObject ();
    74.         if (folder.Length > 0) {
    75.             string filename = folder + "/" + clip.name + jsonFileExt;
    76.             FileStream f = new FileStream (filename, FileMode.OpenOrCreate, FileAccess.Write);
    77.             StreamWriter sw = new StreamWriter (f);
    78.             sw.Write (sb.ToString ());
    79.             sw.Close ();
    80.             f.Close ();
    81.         }
    82.     }
    83.     private static void BeginObject () { sb.Append ("{ "); }
    84.     private static void EndObject () { sb.Append (" }"); }
    85.     private static void BeginArray (string keyname) { sb.AppendFormat ("\"{0}\" : [", keyname); }
    86.     private static void EndArray () { sb.Append (" ]"); }
    87.     private static void WriteString (string key, string value) { sb.AppendFormat ("\"{0}\" : \"{1}\"", key, value); }
    88.     private static void WriteFloat (string key, float val) { sb.AppendFormat ("\"{0}\" : {1}", key, val); }
    89.     private static void Next () { sb.Append (", "); }
    90. }
    91.  
     
    Last edited: Jan 30, 2013
    vanger1 and sp958857 like this.
  2. unity_6H9mo6XthbhibA

    unity_6H9mo6XthbhibA

    Joined:
    Nov 18, 2020
    Posts:
    2
    Hello, I've tried your script, but it is exporting to the json file without sprites. I have skeletal animation that I've made in unity. And I expected to export the whole animation with sprites completely, in order to Improt that to the xcode with swift.
    Can someone help?
     
    sagar0707 likes this.
  3. sagar0707

    sagar0707

    Joined:
    Jan 15, 2021
    Posts:
    1
    if you find any solution of this type json requirement then share please !