Skip to content

Commit

Permalink
Merge pull request #2458 from KhronosGroup/action_slot
Browse files Browse the repository at this point in the history
Manage Blender Action slot
  • Loading branch information
julienduroure authored Jan 13, 2025
2 parents 7779886 + a516ea0 commit 42ac3fe
Show file tree
Hide file tree
Showing 33 changed files with 1,498 additions and 909 deletions.
47 changes: 38 additions & 9 deletions addons/io_scene_gltf2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
bl_info = {
'name': 'glTF 2.0 format',
'author': 'Julien Duroure, Scurest, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors',
"version": (4, 4, 26),
"version": (4, 4, 27),
'blender': (4, 4, 0),
'location': 'File > Import-Export',
'description': 'Import-Export as glTF 2.0',
Expand Down Expand Up @@ -813,6 +813,16 @@ class ExportGLTF2_Base(ConvertGLTF2_Base):
default=False
)

export_merge_animation: EnumProperty(
name='Merge Animation',
items=(('NLA_TRACK', 'NLA Track Names', 'Merge by NLA Track Names'),
('ACTION', 'Actions', 'Merge by Actions'),
('NONE', 'No Merge', 'Do not merge animations'),
),
description='Merge animations',
default='ACTION'
)

export_anim_single_armature: BoolProperty(
name='Export all Armature Actions',
description=(
Expand Down Expand Up @@ -1194,6 +1204,11 @@ def execute(self, context):
export_settings['gltf_trs_w_animation_pointer'] = False
export_settings['gltf_export_anim_pointer'] = False

if export_settings['gltf_animation_mode'] != "ACTIONS":
export_settings['gltf_merge_animation'] = "NLA_TRACK"
else:
export_settings['gltf_merge_animation'] = self.export_merge_animation

export_settings['gltf_nla_strips_merged_animation_name'] = self.export_nla_strips_merged_animation_name
export_settings['gltf_optimize_animation'] = self.export_optimize_animation_size
export_settings['gltf_optimize_animation_keep_armature'] = self.export_optimize_animation_keep_anim_armature
Expand Down Expand Up @@ -1597,16 +1612,9 @@ def export_panel_animation(layout, operator):
if operator.export_animation_mode == "ACTIVE_ACTIONS":
layout.prop(operator, 'export_nla_strips_merged_animation_name')

row = body.row()
row.active = operator.export_force_sampling and operator.export_animation_mode in [
'ACTIONS', 'ACTIVE_ACTIONS', 'BROACAST']
row.prop(operator, 'export_bake_animation')
if operator.export_animation_mode == "SCENE":
body.prop(operator, 'export_anim_scene_split_object')
row = body.row()

if operator.export_animation_mode in ["NLA_TRACKS", "SCENE"]:
export_panel_animation_notes(body, operator)
export_panel_animation_bake_and_merge(body, operator)
export_panel_animation_ranges(body, operator)
export_panel_animation_armature(body, operator)
export_panel_animation_shapekeys(body, operator)
Expand Down Expand Up @@ -1634,6 +1642,27 @@ def export_panel_animation_notes(layout, operator):
body.label(text="- sampling is active")
body.label(text="- baking all objects is active")

def export_panel_animation_bake_and_merge(layout, operator):
header, body = layout.panel("GLTF_export_animation_bake_and_merge", default_closed=False)
header.label(text="Bake & Merge")
if body:
body.active = operator.export_animations

row = body.row()
row.active = operator.export_force_sampling and operator.export_animation_mode in [
'ACTIONS', 'ACTIVE_ACTIONS', 'BROACAST']
row.prop(operator, 'export_bake_animation')

if operator.export_animation_mode == "SCENE":
row = body.row()
row.prop(operator, 'export_anim_scene_split_object')

row = body.row()
row.active = operator.export_force_sampling and operator.export_animation_mode in ['ACTIONS']
row.prop(operator, 'export_merge_animation')

row = body.row()


def export_panel_animation_ranges(layout, operator):
header, body = layout.panel("GLTF_export_animation_ranges", default_closed=True)
Expand Down
11 changes: 11 additions & 0 deletions addons/io_scene_gltf2/blender/com/gltf2_blender_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,8 @@ def execute(self, context):
# remove all actions from objects
for obj in bpy.context.scene.objects:
if obj.animation_data:
if obj.animation_data.action is not None:
obj.animation_data.action_slot = None
obj.animation_data.action = None
obj.matrix_world = obj.gltf2_animation_rest

Expand All @@ -542,28 +544,37 @@ def execute(self, context):
obj.animation_data.action = track.strips[0].action

if obj.type == "MESH" and obj.data and obj.data.shape_keys and obj.data.shape_keys.animation_data:
if obj.data.shape_keys.animation_data.action is not None:
obj.data.shape_keys.animation_data.action_slot = None
obj.data.shape_keys.animation_data.action = None
for idx, data in enumerate(obj.gltf2_animation_weight_rest):
obj.data.shape_keys.key_blocks[idx + 1].value = data.val

for track in [track for track in obj.data.shape_keys.animation_data.nla_tracks if track.name ==
track_name and len(track.strips) > 0 and track.strips[0].action is not None]:
obj.data.shape_keys.animation_data.action = track.strips[0].action
obj.data.shape_keys.animation_data.action_slot = track.strips[0].action_slot

if obj.type in ["LIGHT", "CAMERA"] and obj.data and obj.data.animation_data:
if obj.data.animation_data.action is not None:
obj.data.animation_data.action_slot = None
obj.data.animation_data.action = None
for track in [track for track in obj.data.animation_data.nla_tracks if track.name ==
track_name and len(track.strips) > 0 and track.strips[0].action is not None]:
obj.data.animation_data.action = track.strips[0].action
obj.data.animation_data.action_slot = track.strips[0].action_slot

for mat in bpy.data.materials:
if not mat.node_tree:
continue
if mat.node_tree.animation_data:
if mat.node_tree.animation_data.action is not None:
mat.node_tree.animation_data.action_slot = None
mat.node_tree.animation_data.action = None
for track in [track for track in mat.node_tree.animation_data.nla_tracks if track.name ==
track_name and len(track.strips) > 0 and track.strips[0].action is not None]:
mat.node_tree.animation_data.action = track.strips[0].action
mat.node_tree.animation_data.action_slot = track.strips[0].action_slot

bpy.data.scenes[0].gltf2_animation_applied = self.index
return {'FINISHED'}
Expand Down
Loading

0 comments on commit 42ac3fe

Please sign in to comment.