Fbx export from UE4 bug and Blender Animation Import 25 fps work around

I possibly found a bug in unreal engine 4 fbx animation export, namely that it exports it’s animation with 1 frame extra and then scales the animation to that range.
For example, when exporting “ThirdPersonWalk” animation sequence from 3rdPerson Template it should export frames 0 to 29 but it exports frames 0 to 30.

To work around this bug, for importing ue4 animation fbx exports into blender correctly you have to do the following things:

  1. change file “import_fbx.py” in folder “\scripts\addons\io_scene_fbx” line 2337 from

    real_fps = 25.0
    to

    real_fps = bpy.context.scene.render.fps

  2. Set blender’s frame rate to 30 fps in Properties Editor in the Render Tab.

  3. import exported fbx animation

  4. Scale the animation to one frame less, for example if animation is from frame 0 to 30 scale the animation so it becomes frame 0 to 29. you can do this manually in Dope Sheet Editor by setting current frame to 0 in Dope Sheet Editor and than press shortcut “S” to scale and type “0.9666” ((last frame - 1)/ last frame in this case 29/30 which is 0.9666).
    This will be a hassle if you have to do it for a lot of imported animations or someone could make a python script to do this automatically.

Edit: here’s a simple python script to automatically scale the time correctly of an armature.
Select the armature, copy-paste this script in the Text Editor and run it

import bpy

# store current area type, then change area type to Dopesheet Editor
area = bpy.context.area
old_type = area.type
area.type = 'DOPESHEET_EDITOR'

# get active object, animation range, scale animation
obj = bpy.context.scene.objects.active
obj_frame_range = obj.animation_data.action.frame_range
obj_frame_length = obj_frame_range[1] - obj_frame_range[0]
bpy.context.scene.frame_current = obj_frame_range[0]
time_scale_value = (obj_frame_length - 1)/obj_frame_length
bpy.ops.transform.transform(mode='TIME_SCALE', value=(time_scale_value, 0, 0, 0))

#restore area type to first state
area.type = old_type

Hopefully the bug will be fixed soon and you just have to set fps to 30.

This bug of ue4 exporting one extra frame and scaling the whole animation to the new range probably also is in 3ds max and maya.

UE4 seems to show two different frame counts
As a original 10 frames animation:

Animation Sequence editor: 10 frames

Content Browser (number of keys): 11 frames

Exported from UE4 FBX ASCII: 11 frames

But you actually don’t need to scale the animation, on my tests i’ve created an animation from frame 0 to 10, exported from Blender and then imported to UE4, then exported from UE4 (as ASCII FBX it shows as 11 frames), but when imported into Blender it imported as 1 frame forward, 1 to 11 (but still 10 frames in total)

In my case i’ve imported many animations, so i used this code to move all keyframes one frame back:

import bpy

for action in bpy.data.actions:
    for fcurve in action.fcurves:
        for point in fcurve.keyframe_points:
            point.co.x -= 1