Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sampling step alignement #2389

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions addons/io_scene_gltf2/blender/exp/animation/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import bpy
import typing
from math import ceil
from ....io.com import gltf2_io
from ....io.exp.user_extensions import export_user_extensions
from ....blender.com.conversion import get_gltf_interpolation
Expand Down Expand Up @@ -75,11 +76,27 @@ def gather_actions_animations(export_settings):

return new_animations

# We need to align if step is not 1
# For example, cache will get frame 1/4/7/10 if step is 3, with an action starting at frame 1
# If all backing is enabled, and scene start at 0, we will get frame 0/3/6/9 => Cache will fail
# Set the reference frame from the first action retrieve, and align all actions to this frame
def _align_frame_start(reference_frame_start, frame, export_settings):

if reference_frame_start is None:
return frame

if export_settings['gltf_frame_step'] == 1:
return frame

return reference_frame_start + export_settings['gltf_frame_step'] * ceil((frame - reference_frame_start) / export_settings['gltf_frame_step'])


def prepare_actions_range(export_settings):

track_slide = {}

start_frame_reference = None

vtree = export_settings['vtree']
for obj_uuid in vtree.get_all_objects():

Expand Down Expand Up @@ -124,9 +141,17 @@ def prepare_actions_range(export_settings):
start_frame = max(bpy.context.scene.frame_start, start_frame)
end_frame = min(bpy.context.scene.frame_end, end_frame)

export_settings['ranges'][obj_uuid][blender_action.name]['start'] = start_frame
export_settings['ranges'][obj_uuid][blender_action.name]['start'] = _align_frame_start(start_frame_reference, start_frame, export_settings)
export_settings['ranges'][obj_uuid][blender_action.name]['end'] = end_frame

if start_frame_reference is None:
start_frame_reference = start_frame

# Recheck all actions to align to this frame
for obj_uuid_tmp in export_settings['ranges'].keys():
for action_name_tmp in export_settings['ranges'][obj_uuid_tmp].keys():
export_settings['ranges'][obj_uuid_tmp][action_name_tmp]['start'] = _align_frame_start(start_frame_reference, export_settings['ranges'][obj_uuid_tmp][action_name_tmp]['start'], export_settings)

if export_settings['gltf_negative_frames'] == "SLIDE":
if track is not None:
if not (track.startswith("NlaTrack") or track.startswith("[Action Stash]")):
Expand Down Expand Up @@ -163,7 +188,7 @@ def prepare_actions_range(export_settings):

if type_ == "SHAPEKEY" and export_settings['gltf_bake_animation']:
export_settings['ranges'][obj_uuid][obj_uuid] = {}
export_settings['ranges'][obj_uuid][obj_uuid]['start'] = bpy.context.scene.frame_start
export_settings['ranges'][obj_uuid][obj_uuid]['start'] = _align_frame_start(start_frame_reference, bpy.context.scene.frame_start, export_settings)
export_settings['ranges'][obj_uuid][obj_uuid]['end'] = bpy.context.scene.frame_end

# For baking drivers
Expand All @@ -173,15 +198,15 @@ def prepare_actions_range(export_settings):
if obj_dr not in export_settings['ranges']:
export_settings['ranges'][obj_dr] = {}
export_settings['ranges'][obj_dr][obj_uuid + "_" + blender_action.name] = {}
export_settings['ranges'][obj_dr][obj_uuid + "_" + blender_action.name]['start'] = start_frame
export_settings['ranges'][obj_dr][obj_uuid + "_" + blender_action.name]['start'] = _align_frame_start(start_frame_reference, start_frame, export_settings)
export_settings['ranges'][obj_dr][obj_uuid + "_" + blender_action.name]['end'] = end_frame

if len(blender_actions) == 0 and export_settings['gltf_bake_animation']:
# No animation on this object
# In case of baking animation, we will use scene frame range
# Will be calculated later if max range. Can be set here if scene frame range
export_settings['ranges'][obj_uuid][obj_uuid] = {}
export_settings['ranges'][obj_uuid][obj_uuid]['start'] = bpy.context.scene.frame_start
export_settings['ranges'][obj_uuid][obj_uuid]['start'] = _align_frame_start(start_frame_reference, bpy.context.scene.frame_start, export_settings)
export_settings['ranges'][obj_uuid][obj_uuid]['end'] = bpy.context.scene.frame_end

# For baking drivers
Expand All @@ -192,7 +217,7 @@ def prepare_actions_range(export_settings):
export_settings['ranges'][obj_dr] = {}
export_settings['ranges'][obj_dr][obj_uuid + "_" + obj_uuid] = {}
export_settings['ranges'][obj_dr][obj_uuid + "_" +
obj_uuid]['start'] = bpy.context.scene.frame_start
obj_uuid]['start'] = _align_frame_start(start_frame_reference, bpy.context.scene.frame_start, export_settings)
export_settings['ranges'][obj_dr][obj_uuid + "_" + obj_uuid]['end'] = bpy.context.scene.frame_end

if (export_settings['gltf_negative_frames'] == "SLIDE"
Expand Down
Loading