- Home /
How to Compress & Keyframe-reduce a Generated Animation?
Hi all, I wrote a script to remove the Scale curves from an animation because often I don't need them. I'm using AnimationUtility GetAllCurves and SetCurve to make a new animation with only the curves I want to keep.
The generated AnimationClip does have fewer curves, but the file is about 4x larger! I suspect this is because I'm no longer benefitting from the Model Importer's Animation Compression and Keyframe Reduction.
How can I reduce the file size and take advantage of these optimizations??
Script: using UnityEngine; using UnityEditor; using System; using System.IO;
public class RemoveAnimScaleCurves : ScriptableObject
{
[MenuItem("Util/Remove Anim Scale Curves")]
public static void CleanCurves() {
foreach ( UnityEngine.Object o in Selection.objects) {
if (o != null && o is AnimationClip) {
AnimationClip selectedClip = (AnimationClip) o;
AnimationClipCurveData[] curves = AnimationUtility.GetAllCurves(selectedClip);
AnimationClip newClip = new AnimationClip();
foreach (AnimationClipCurveData curve in curves) {
// Weed out Scale
if ( ! curve.propertyName.Contains("Scale") && curve.curve.keys.Length > 0 )
newClip.SetCurve( curve.path, curve.type, curve.propertyName, curve.curve );
}
newClip.name = selectedClip.name + "_NoScale";
newClip.wrapMode = selectedClip.wrapMode;
Debug.Log ( "Old Clip " + selectedClip.name + " has " + curves.Length + " curves" );
Debug.Log ( "New Clip " + newClip.name + " has " + AnimationUtility.GetAllCurves(newClip).Length + " curves" );
SaveAnimationClip( newClip );
AssetDatabase.SaveAssets();
}
}
}
public static void SaveAnimationClip(AnimationClip a) {
if(!Directory.Exists("Assets/Animations Generated")) {
AssetDatabase.CreateFolder("Assets", "Animations Generated");
AssetDatabase.Refresh();
}
string path = AssetDatabase.GenerateUniqueAssetPath("Assets/Animations Generated/"+a.name+".asset");
AssetDatabase.CreateAsset(a, path);
}
}
Answer by Bunny83 · Oct 21, 2012 at 12:10 AM
You might be able to create an AssetPostprocessor and fix the curves while importing the model, but i guess the compression happens between the Pre and Post callback. You can't access the animations in the Pre callback afaik.
The best solution is to fix the animation outside of Unity. Afaik you can't invoke the compression / optimisation manually, so if you need it, the animation should be as it should be when you import it.
Thank you! Not what I'd hoped, but a relief to know with some clarity.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082658im_/https://answers.unity.com/themes/thub/images/avi.jpg)