Skip to content
Snippets Groups Projects
Commit 19385dbc authored by Julien Duroure's avatar Julien Duroure
Browse files

glTF exporter: Use custom range on action

parent c8445b67
No related branches found
No related tags found
No related merge requests found
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
bl_info = { bl_info = {
'name': 'glTF 2.0 format', 'name': 'glTF 2.0 format',
'author': 'Julien Duroure, Scurest, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors', 'author': 'Julien Duroure, Scurest, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors',
"version": (1, 8, 11), "version": (1, 8, 12),
'blender': (3, 0, 0), 'blender': (3, 1, 0),
'location': 'File > Import-Export', 'location': 'File > Import-Export',
'description': 'Import-Export as glTF 2.0', 'description': 'Import-Export as glTF 2.0',
'warning': '', 'warning': '',
......
...@@ -40,18 +40,25 @@ def gather_animation_channels(blender_action: bpy.types.Action, ...@@ -40,18 +40,25 @@ def gather_animation_channels(blender_action: bpy.types.Action,
# This is need if user set 'Force sampling' and in case we need to bake # This is need if user set 'Force sampling' and in case we need to bake
bake_range_start = None bake_range_start = None
bake_range_end = None bake_range_end = None
groups = __get_channel_groups(blender_action, blender_object, export_settings) force_range = False
# Note: channels has some None items only for SK if some SK are not animated # If range is manually set, use it. Else, calculate it
for chans in groups: if blender_action.use_frame_range is True:
ranges = [channel.range() for channel in chans if channel is not None] bake_range_start = blender_action.frame_start
if bake_range_start is None: bake_range_end = blender_action.frame_end
bake_range_start = min([channel.range()[0] for channel in chans if channel is not None]) force_range = True # keyframe_points is read-only, we cant restrict here
else: else:
bake_range_start = min(bake_range_start, min([channel.range()[0] for channel in chans if channel is not None])) groups = __get_channel_groups(blender_action, blender_object, export_settings)
if bake_range_end is None: # Note: channels has some None items only for SK if some SK are not animated
bake_range_end = max([channel.range()[1] for channel in chans if channel is not None]) for chans in groups:
else: ranges = [channel.range() for channel in chans if channel is not None]
bake_range_end = max(bake_range_end, max([channel.range()[1] for channel in chans if channel is not None])) if bake_range_start is None:
bake_range_start = min([channel.range()[0] for channel in chans if channel is not None])
else:
bake_range_start = min(bake_range_start, min([channel.range()[0] for channel in chans if channel is not None]))
if bake_range_end is None:
bake_range_end = max([channel.range()[1] for channel in chans if channel is not None])
else:
bake_range_end = max(bake_range_end, max([channel.range()[1] for channel in chans if channel is not None]))
if blender_object.type == "ARMATURE" and export_settings['gltf_force_sampling'] is True: if blender_object.type == "ARMATURE" and export_settings['gltf_force_sampling'] is True:
...@@ -84,6 +91,7 @@ def gather_animation_channels(blender_action: bpy.types.Action, ...@@ -84,6 +91,7 @@ def gather_animation_channels(blender_action: bpy.types.Action,
p, p,
bake_range_start, bake_range_start,
bake_range_end, bake_range_end,
force_range,
blender_action.name, blender_action.name,
None, None,
(bone.name, p) in list_of_animated_bone_channels) (bone.name, p) in list_of_animated_bone_channels)
...@@ -98,7 +106,7 @@ def gather_animation_channels(blender_action: bpy.types.Action, ...@@ -98,7 +106,7 @@ def gather_animation_channels(blender_action: bpy.types.Action,
if len(channel_group) == 0: if len(channel_group) == 0:
# Only errors on channels, ignoring # Only errors on channels, ignoring
continue continue
channel = __gather_animation_channel(channel_group, blender_object, export_settings, None, None, bake_range_start, bake_range_end, blender_action.name, None, True) channel = __gather_animation_channel(channel_group, blender_object, export_settings, None, None, bake_range_start, bake_range_end, force_range, blender_action.name, None, True)
if channel is not None: if channel is not None:
channels.append(channel) channels.append(channel)
...@@ -115,6 +123,7 @@ def gather_animation_channels(blender_action: bpy.types.Action, ...@@ -115,6 +123,7 @@ def gather_animation_channels(blender_action: bpy.types.Action,
None, None,
bake_range_start, bake_range_start,
bake_range_end, bake_range_end,
force_range,
blender_action.name, blender_action.name,
obj, obj,
False) False)
...@@ -135,6 +144,7 @@ def gather_animation_channels(blender_action: bpy.types.Action, ...@@ -135,6 +144,7 @@ def gather_animation_channels(blender_action: bpy.types.Action,
None, None,
bake_range_start, bake_range_start,
bake_range_end, bake_range_end,
force_range,
blender_action.name, blender_action.name,
None, None,
False) False)
...@@ -207,6 +217,7 @@ def __gather_animation_channel(channels: typing.Tuple[bpy.types.FCurve], ...@@ -207,6 +217,7 @@ def __gather_animation_channel(channels: typing.Tuple[bpy.types.FCurve],
bake_channel: typing.Union[str, None], bake_channel: typing.Union[str, None],
bake_range_start, bake_range_start,
bake_range_end, bake_range_end,
force_range: bool,
action_name: str, action_name: str,
driver_obj, driver_obj,
node_channel_is_animated: bool node_channel_is_animated: bool
...@@ -216,7 +227,7 @@ def __gather_animation_channel(channels: typing.Tuple[bpy.types.FCurve], ...@@ -216,7 +227,7 @@ def __gather_animation_channel(channels: typing.Tuple[bpy.types.FCurve],
__target= __gather_target(channels, blender_object, export_settings, bake_bone, bake_channel, driver_obj) __target= __gather_target(channels, blender_object, export_settings, bake_bone, bake_channel, driver_obj)
if __target.path is not None: if __target.path is not None:
sampler = __gather_sampler(channels, blender_object, export_settings, bake_bone, bake_channel, bake_range_start, bake_range_end, action_name, driver_obj, node_channel_is_animated) sampler = __gather_sampler(channels, blender_object, export_settings, bake_bone, bake_channel, bake_range_start, bake_range_end, force_range, action_name, driver_obj, node_channel_is_animated)
if sampler is None: if sampler is None:
# After check, no need to animate this node for this channel # After check, no need to animate this node for this channel
...@@ -275,6 +286,7 @@ def __gather_sampler(channels: typing.Tuple[bpy.types.FCurve], ...@@ -275,6 +286,7 @@ def __gather_sampler(channels: typing.Tuple[bpy.types.FCurve],
bake_channel: typing.Union[str, None], bake_channel: typing.Union[str, None],
bake_range_start, bake_range_start,
bake_range_end, bake_range_end,
force_range: bool,
action_name, action_name,
driver_obj, driver_obj,
node_channel_is_animated: bool node_channel_is_animated: bool
...@@ -286,6 +298,7 @@ def __gather_sampler(channels: typing.Tuple[bpy.types.FCurve], ...@@ -286,6 +298,7 @@ def __gather_sampler(channels: typing.Tuple[bpy.types.FCurve],
bake_channel, bake_channel,
bake_range_start, bake_range_start,
bake_range_end, bake_range_end,
force_range,
action_name, action_name,
driver_obj, driver_obj,
node_channel_is_animated, node_channel_is_animated,
......
...@@ -192,22 +192,27 @@ def gather_keyframes(blender_object_if_armature: typing.Optional[bpy.types.Objec ...@@ -192,22 +192,27 @@ def gather_keyframes(blender_object_if_armature: typing.Optional[bpy.types.Objec
bake_channel: typing.Union[str, None], bake_channel: typing.Union[str, None],
bake_range_start, bake_range_start,
bake_range_end, bake_range_end,
force_range: bool,
action_name: str, action_name: str,
driver_obj, driver_obj,
node_channel_is_animated: bool, node_channel_is_animated: bool,
export_settings export_settings
) -> typing.List[Keyframe]: ) -> typing.List[Keyframe]:
"""Convert the blender action groups' fcurves to keyframes for use in glTF.""" """Convert the blender action groups' fcurves to keyframes for use in glTF."""
if bake_bone is None and driver_obj is None: if force_range is True:
# Find the start and end of the whole action group
# Note: channels has some None items only for SK if some SK are not animated
ranges = [channel.range() for channel in channels if channel is not None]
start_frame = min([channel.range()[0] for channel in channels if channel is not None])
end_frame = max([channel.range()[1] for channel in channels if channel is not None])
else:
start_frame = bake_range_start start_frame = bake_range_start
end_frame = bake_range_end end_frame = bake_range_end
else:
if bake_bone is None and driver_obj is None:
# Find the start and end of the whole action group
# Note: channels has some None items only for SK if some SK are not animated
ranges = [channel.range() for channel in channels if channel is not None]
start_frame = min([channel.range()[0] for channel in channels if channel is not None])
end_frame = max([channel.range()[1] for channel in channels if channel is not None])
else:
start_frame = bake_range_start
end_frame = bake_range_end
keyframes = [] keyframes = []
if needs_baking(blender_object_if_armature, channels, export_settings): if needs_baking(blender_object_if_armature, channels, export_settings):
...@@ -270,6 +275,8 @@ def gather_keyframes(blender_object_if_armature: typing.Optional[bpy.types.Objec ...@@ -270,6 +275,8 @@ def gather_keyframes(blender_object_if_armature: typing.Optional[bpy.types.Objec
frames = [keyframe.co[0] for keyframe in [c for c in channels if c is not None][0].keyframe_points] frames = [keyframe.co[0] for keyframe in [c for c in channels if c is not None][0].keyframe_points]
# some weird files have duplicate frame at same time, removed them # some weird files have duplicate frame at same time, removed them
frames = sorted(set(frames)) frames = sorted(set(frames))
if force_range is True:
frames = [f for f in frames if f >= bake_range_start and f <= bake_range_end]
for i, frame in enumerate(frames): for i, frame in enumerate(frames):
key = Keyframe(channels, frame, bake_channel) key = Keyframe(channels, frame, bake_channel)
# key.value = [c.keyframe_points[i].co[0] for c in action_group.channels] # key.value = [c.keyframe_points[i].co[0] for c in action_group.channels]
......
...@@ -37,6 +37,7 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve], ...@@ -37,6 +37,7 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve],
bake_channel: typing.Union[str, None], bake_channel: typing.Union[str, None],
bake_range_start, bake_range_start,
bake_range_end, bake_range_end,
force_range: bool,
action_name: str, action_name: str,
driver_obj, driver_obj,
node_channel_is_animated: bool, node_channel_is_animated: bool,
...@@ -63,7 +64,7 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve], ...@@ -63,7 +64,7 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve],
matrix_parent_inverse = mathutils.Matrix.Identity(4).freeze() matrix_parent_inverse = mathutils.Matrix.Identity(4).freeze()
input = __gather_input(channels, blender_object_if_armature, non_keyed_values, input = __gather_input(channels, blender_object_if_armature, non_keyed_values,
bake_bone, bake_channel, bake_range_start, bake_range_end, action_name, driver_obj, node_channel_is_animated, export_settings) bake_bone, bake_channel, bake_range_start, bake_range_end, force_range, action_name, driver_obj, node_channel_is_animated, export_settings)
if input is None: if input is None:
# After check, no need to animate this node for this channel # After check, no need to animate this node for this channel
...@@ -82,6 +83,7 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve], ...@@ -82,6 +83,7 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve],
bake_channel, bake_channel,
bake_range_start, bake_range_start,
bake_range_end, bake_range_end,
force_range,
action_name, action_name,
driver_obj, driver_obj,
node_channel_is_animated, node_channel_is_animated,
...@@ -235,6 +237,7 @@ def __gather_input(channels: typing.Tuple[bpy.types.FCurve], ...@@ -235,6 +237,7 @@ def __gather_input(channels: typing.Tuple[bpy.types.FCurve],
bake_channel: typing.Union[str, None], bake_channel: typing.Union[str, None],
bake_range_start, bake_range_start,
bake_range_end, bake_range_end,
force_range: bool,
action_name, action_name,
driver_obj, driver_obj,
node_channel_is_animated: bool, node_channel_is_animated: bool,
...@@ -248,6 +251,7 @@ def __gather_input(channels: typing.Tuple[bpy.types.FCurve], ...@@ -248,6 +251,7 @@ def __gather_input(channels: typing.Tuple[bpy.types.FCurve],
bake_channel, bake_channel,
bake_range_start, bake_range_start,
bake_range_end, bake_range_end,
force_range,
action_name, action_name,
driver_obj, driver_obj,
node_channel_is_animated, node_channel_is_animated,
...@@ -317,6 +321,7 @@ def __gather_output(channels: typing.Tuple[bpy.types.FCurve], ...@@ -317,6 +321,7 @@ def __gather_output(channels: typing.Tuple[bpy.types.FCurve],
bake_channel: typing.Union[str, None], bake_channel: typing.Union[str, None],
bake_range_start, bake_range_start,
bake_range_end, bake_range_end,
force_range: bool,
action_name, action_name,
driver_obj, driver_obj,
node_channel_is_animated: bool, node_channel_is_animated: bool,
...@@ -330,6 +335,7 @@ def __gather_output(channels: typing.Tuple[bpy.types.FCurve], ...@@ -330,6 +335,7 @@ def __gather_output(channels: typing.Tuple[bpy.types.FCurve],
bake_channel, bake_channel,
bake_range_start, bake_range_start,
bake_range_end, bake_range_end,
force_range,
action_name, action_name,
driver_obj, driver_obj,
node_channel_is_animated, node_channel_is_animated,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment