glTF exporter: better solution for mesh parented to bone management, when using rest pose as joint node default TRS

This commit is contained in:
Julien Duroure 2022-11-20 21:55:17 +01:00
parent 0b4844ede9
commit eb07144ea7
3 changed files with 11 additions and 39 deletions

View File

@ -4,7 +4,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": (3, 4, 47),
"version": (3, 4, 48),
'blender': (3, 3, 0),
'location': 'File > Import-Export',
'description': 'Import-Export as glTF 2.0',

View File

@ -26,28 +26,11 @@ def gather_channels_baked(obj_uuid, frame_range, export_settings):
if len(bpy.data.actions) == 0:
return None
blender_obj = export_settings['vtree'].nodes[obj_uuid].blender_object
if frame_range is None:
start_frame = min([v[0] for v in [a.frame_range for a in bpy.data.actions]])
end_frame = max([v[1] for v in [a.frame_range for a in bpy.data.actions]])
else:
if blender_obj.animation_data and blender_obj.animation_data.action:
# Coming from object parented to bone, and object is also animated. So using range action
start_frame, end_frame = blender_obj.animation_data.action.frame_range[0], blender_obj.animation_data.action.frame_range[1]
else:
# Coming from object parented to bone, and object is not animated. So using range from armature
start_frame, end_frame = frame_range
# use action if exists, else obj_uuid
# When an object need some forced baked, there are 2 situations:
# - Non animated object, but there are some selection, so we need to bake
# - Object parented to bone. So we need to bake, because of inverse transforms on non default TRS armatures
# In this last case, there are 2 situations :
# - Object is also animated, so use the action name as key for caching
# - Object is not animated, so use obj_uuid as key for caching, like for non animated object (case 1)
key_action = blender_obj.animation_data.action.name if blender_obj.animation_data and blender_obj.animation_data.action else obj_uuid
start_frame, end_frame = frame_range
for p in ["location", "rotation_quaternion", "scale"]:
channel = gather_animation_channel(
@ -59,7 +42,7 @@ def gather_channels_baked(obj_uuid, frame_range, export_settings):
start_frame,
end_frame,
False,
key_action, # Use obj uuid as action name for caching (or action name if case of object parented to bone and animated)
obj_uuid, # Use obj uuid as action name for caching
None,
False #If Object is not animated, don't keep animation for this channel
)
@ -167,21 +150,6 @@ def gather_animation_channels(obj_uuid: int,
if channel is not None:
channels.append(channel)
# When An Object is parented to bone, and rest pose is used (not current frame)
# If parenting is not done with same TRS than rest pose, this can lead to inconsistencies
# So we need to bake object animation too, to be sure that correct TRS animation are used
# Here, we want add these channels to same action that the armature
if export_settings['gltf_selected'] is False and export_settings['gltf_current_frame'] is False:
children_obj_parent_to_bones = []
for bone_uuid in bones_uuid:
children_obj_parent_to_bones.extend([child for child in export_settings['vtree'].nodes[bone_uuid].children if export_settings['vtree'].nodes[child].blender_type not in [VExportNode.BONE, VExportNode.ARMATURE]])
for child_uuid in children_obj_parent_to_bones:
channels_baked = gather_channels_baked(child_uuid, (bake_range_start, bake_range_end), export_settings)
if channels_baked is not None:
channels.extend(channels_baked)
else:
done_paths = []
for channel_group in __get_channel_groups(blender_action, blender_object, export_settings):

View File

@ -35,7 +35,7 @@ class VExportNode:
def __init__(self):
self.children = []
self.blender_type = None
self.world_matrix = None
self.matrix_world = None
self.parent_type = None
self.blender_object = None
@ -62,9 +62,6 @@ class VExportNode:
def add_child(self, uuid):
self.children.append(uuid)
def set_world_matrix(self, matrix):
self.world_matrix = matrix
def set_blender_data(self, blender_object, blender_bone):
self.blender_object = blender_object
self.blender_bone = blender_bone
@ -170,6 +167,13 @@ class VExportTree:
# Matrix World of object is expressed based on collection instance objects are
# So real world matrix is collection world_matrix @ "world_matrix" of object
node.matrix_world = parent_coll_matrix_world @ blender_object.matrix_world.copy()
# If object is parented to bone, and Rest pose is used, we need to keep the world matrix
# Of the rest pose, not the current world matrix
if parent_uuid and self.nodes[parent_uuid].blender_type == VExportNode.BONE and self.export_settings['gltf_current_frame'] is False:
blender_bone = self.nodes[parent_uuid].blender_bone
node.matrix_world = (blender_bone.matrix @ blender_bone.bone.matrix_local.inverted_safe()).inverted_safe() @ node.matrix_world
if node.blender_type == VExportNode.CAMERA and self.export_settings[gltf2_blender_export_keys.CAMERAS]:
if self.export_settings[gltf2_blender_export_keys.YUP]:
correction = Quaternion((2**0.5/2, -2**0.5/2, 0.0, 0.0))