Skip to content

Instantly share code, notes, and snippets.

@heybdj
Created October 1, 2017 22:33
Show Gist options
  • Save heybdj/f8c47b00aa2e4a6d647d3d903b71dd1c to your computer and use it in GitHub Desktop.
Save heybdj/f8c47b00aa2e4a6d647d3d903b71dd1c to your computer and use it in GitHub Desktop.
Set constant tangets on a Unity animation clip
// This script is published under the CC0 1.0 Universal License
// https://creativecommons.org/publicdomain/zero/1.0/
// To the extent possible under law, Spryly® Ltd. has waived all copyright
// and related or neighboring rights to this script. This work is published from: Canada.
using UnityEngine;
using UnityEditor;
using System.Collections;
public static class SetConstantTangents {
// Takes the selected animation clips and converts all keyframe tangents to constant / step tangents
[MenuItem ("Maple/Animation/Set Constant Tangents")]
static public void SetConstantTangentsOnAnimationClip() {
Object[] clips = Selection.GetFiltered( typeof( AnimationClip ), SelectionMode.TopLevel );
for( int i = 0; i < clips.Length; ++i ) {
AnimationClip clip = clips[ i ] as AnimationClip;
EditorCurveBinding[] floatBindings = AnimationUtility.GetCurveBindings( clip );
for( int j = 0; j < floatBindings.Length; ++j ) {
EditorCurveBinding binding = floatBindings[ j ];
AnimationCurve curve = AnimationUtility.GetEditorCurve( clip, binding );
for( int k = 0; k < curve.keys.Length; ++k ) {
AnimationUtility.SetKeyLeftTangentMode( curve, k, AnimationUtility.TangentMode.Constant );
AnimationUtility.SetKeyRightTangentMode( curve, k, AnimationUtility.TangentMode.Constant );
}
Undo.RecordObject( clip, "Set Constant Tangents" );
clip.SetCurve( binding.path, typeof( Transform ), binding.propertyName, curve );
}
}
}
}
@diegodelarocha
Copy link

Life saver! <3

@diegodelarocha
Copy link

SetConstantTangents works the following way:

  1. Add SetConstantTangents.cs to the editor
  2. Impport Animation on prefab
  3. Resample curves = False
  4. Anim. Compression = Off
  5. Hit Apply
  6. Select all Animation Clips on the Project's Prefab
  7. Duplicate (Command + D)
  8. Add the new duplicates on a folder (to keep organized)
  9. Select them all and go to Tools > SelectConstantTangents

Once the operation is done, the selected Animation scripts should have Constant Tangents

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment