Skip to content

Instantly share code, notes, and snippets.

@Volantk
Last active December 8, 2016 11:35
Show Gist options
  • Save Volantk/9217c61ff849017bf8a016fde78a78b6 to your computer and use it in GitHub Desktop.
Save Volantk/9217c61ff849017bf8a016fde78a78b6 to your computer and use it in GitHub Desktop.
def export_single_action(context, filepath):
armature = bpy.context.active_object
action = armature.animation_data.action
scene = bpy.context.scene
export_action_internal(context, armature, action, scene, filepath)
def export_all_actions(context, filepath):
start_time = time.clock()
armature = bpy.context.active_object
scene = bpy.context.scene
original_action = armature.animation_data.action
exported_actions = []
# Loop through all actions in blend file and export all with fake user flag on
for action in bpy.data.actions:
if(action.use_fake_user != True):
continue
export_action_internal(context, armature, action, scene, filepath)
exported_actions.append(action.name)
armature.animation_data.action = original_action
print("\nEXPORTED ACTIONS:")
for action in exported_actions:
print(action)
print("--- %s seconds ---" % (time.clock() - start_time))
def export_action_internal(context, armature, action, scene, filepath):
# Store original values
original_frame_start = scene.frame_start
original_frame_end = scene.frame_end
# Set frame range to action max min
scene.frame_start = action.frame_range[0]
scene.frame_end = action.frame_range[1]
# Prep filename
filename = filepath + "@" + action.name + ".fbx"
# Add dummy object
# This is done to have control over the final hierarchy in Unity.
bpy.ops.object.empty_add(type='PLAIN_AXES')
empty = bpy.context.scene.objects[-1]
empty.name = "_DUMMY"
# Prep selection for export
empty.select = True
armature.select = True
armature.animation_data.action = action
export_selected(filename)
# Restore original values
scene.frame_start = int(original_frame_start)
scene.frame_end = int(original_frame_end)
# Delete dummy object
armature.select = False
bpy.ops.object.delete()
armature.select = True
bpy.context.scene.objects.active = armature
class class_export_single_action(bpy.types.Operator, ExportHelper):
"""Export the selected rig's currently active action"""
bl_idname = "bear.export_single_action"
bl_label = "Export Current Action"
filename_ext = ""
filepath = ""
@classmethod
def poll(cls, context):
return len(context.selected_objects) is not 0
def execute(self, context):
export_single_action(context, self.filepath)
return {'FINISHED'}
class class_export_all_actions(bpy.types.Operator, ExportHelper):
"""Export all actions with Fake User enabled"""
bl_idname = "bear.export_all_actions"
bl_label = "Export All Actions"
filename_ext = ""
filepath = ""
@classmethod
def poll(cls, context):
return len(context.selected_objects) is not 0
def execute(self, context):
export_all_actions(context, self.filepath)
return {'FINISHED'}
def export_selected(filename, include_animation=True, override_bake_anim_step=1.0):
bpy.ops.export_scene.fbx(
filepath=filename,
check_existing=True,
axis_forward='-Z',
axis_up='Y',
filter_glob="*.fbx",
version='BIN7400',
ui_tab='MAIN',
use_selection=True,
global_scale=1.0,
apply_unit_scale=False, #Set to false to avoid rig being scaled to 100 in Unity
bake_space_transform=True,
object_types={'ARMATURE',
#'CAMERA',
'EMPTY',
#'LAMP',
'MESH',
#'OTHER'
},
use_mesh_modifiers=True,
mesh_smooth_type='OFF',
use_mesh_edges=False,
use_tspace=False,
use_custom_props=False,
add_leaf_bones=False, #Disabledee
primary_bone_axis='Y',
secondary_bone_axis='X',
use_armature_deform_only=True,
armature_nodetype='NULL',
bake_anim=True,
bake_anim_use_all_bones=True,
bake_anim_use_nla_strips=False,
bake_anim_use_all_actions=False,
bake_anim_force_startend_keying=True,
bake_anim_step=override_bake_anim_step,
bake_anim_simplify_factor=0.0,
use_anim=include_animation,
use_anim_action_all=False,
use_default_take=False,
use_anim_optimize=False,
anim_optimize_precision=1.0,
path_mode='AUTO',
embed_textures=False,
batch_mode='OFF',
use_batch_own_dir=True,
use_metadata=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment