Unity: stepped animation from Maya 2018

It is sometimes desireable to use stepped keys or tangents, to achieve a stop-motion quality in your animation. If you are unsure what I’m referring to, it is the style of animation seen in games like Harold Halibut or Armikrog.

For a while it seemed impossible to keep those stepped tangents that you have in Maya. Fortunately, more recent versions of Unity gave us an option to keep the keys the way they are from Maya. This technique should also apply to other 3D animation software, like Blender.

Stepped keyframes in baked animation

If you’re using any kind of rig, where animation is baked down to the joints, there are some extra steps involved in keeping the stepped tangents when exporting your FBX file.

  1. Bake your animation with a “Sample by” value that is appropriate to your animation. If you’ve set Maya to work in 24 frames per second, but you’re consistently only working on 2’s. Then there’s no need to sample every frame of your animation, and a sample value of 2 would suffice.
  2. After baking your animation, select all of your baked objects, open the Graph Editor and set tangents back to being stepped. Check your animation to verify it is still the same as before.
  3. Disable the “Bake Animation” option in the FBX exporter. It will convert all animation to auto tangents, which we definitely don’t want!

Download the FBX file used in the video above: cube_stepped.fbx

Optimising the baked animation

You can either rely on Unity’s keyframe optimisation or use this small script, which removes all identical keyframes, so only the minimum remains.

The tolerance value is how much keys are allowed to differentiate before they are removed.
Do not set it to 0 as it will likely cause the script to do nothing.

import pymel.core as pm
import sys

def cleanup_stepped_curves(tolerance=0.001):
    total_keys = 0
    with pm.UndoChunk():
        for obj in pm.ls(sl=True):
            for curve in pm.listConnections(obj, type="animCurve"):
                keys = pm.keyframe(curve, q=True, timeChange=True, valueChange=True)
                to_remove = []
                count = len(keys)
                for i, (time, value) in enumerate(keys):
                    if i < count - 2:
                        next_value = keys[i + 1][1]
                        if abs(value - next_value) < tolerance:
                            to_remove.append(keys[i + 1][0])
                for t in to_remove:
                    pm.cutKey(curve, time=t)
                    total_keys += 1
        sys.stdout.write("# %d keys remove!\n" % total_keys)

Keeping keyframe tangents in Unity

To keep the keyframes the way they were exported from Maya, disable the “Resample Curves” option in the Animation tab of the model import settings. “Anim. Compression” may also need to be turned off, however, in some cases you can apply some animation compression, while maintaining the exact same animation. So play with these settings until you get the result you’re looking for.

Custom FBX exporter

Since I often have trouble remembering the right settings when exporting FBX files from Maya to Unity, I decided to write myself a tool to ease the process. It can also help bake your animation and potentially keep the stepped keys for more complex animation.

You can download the latest version from GitHub: https://github.com/mortenblaa/maya/tree/master/scripts/fileIO/export_fbx_to_unity