[Bf-extensions-cvs] [782f8585] master: glTF exporter: Big refactoring

Julien Duroure noreply at git.blender.org
Wed Mar 2 21:37:54 CET 2022


Commit: 782f8585f4cc131a7043269ed5ccb14a36742e3d
Author: Julien Duroure
Date:   Wed Mar 2 21:36:18 2022 +0100
Branches: master
https://developer.blender.org/rBA782f8585f4cc131a7043269ed5ccb14a36742e3d

glTF exporter: Big refactoring

- precompute tree before export
- manage collections / instances / linked
- use custom cache to avoid name collision
- animations are baked from world matrix

More info on https://github.com/KhronosGroup/glTF-Blender-IO

===================================================================

M	io_scene_gltf2/__init__.py
M	io_scene_gltf2/blender/com/gltf2_blender_math.py
M	io_scene_gltf2/blender/exp/gltf2_blender_export_keys.py
M	io_scene_gltf2/blender/exp/gltf2_blender_extract.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channel_target.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channels.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_sampler_keyframes.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_animations.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_cache.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_drivers.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_joints.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_materials.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_mesh.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_nodes.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_primitives.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_skins.py
A	io_scene_gltf2/blender/exp/gltf2_blender_gather_tree.py

===================================================================

diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py
index a672be22..1b3c0cc2 100755
--- a/io_scene_gltf2/__init__.py
+++ b/io_scene_gltf2/__init__.py
@@ -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, 2, 7),
+    "version": (3, 2, 8),
     'blender': (3, 1, 0),
     'location': 'File > Import-Export',
     'description': 'Import-Export as glTF 2.0',
@@ -879,6 +879,8 @@ class GLTF_PT_export_animation_export(bpy.types.Panel):
         row = layout.row()
         row.active = operator.export_force_sampling
         row.prop(operator, 'export_def_bones')
+        if operator.export_force_sampling is False and operator.export_def_bones is True:
+            layout.label(text="Export only deformation bones is not possible when not sampling animation")
 
 
 class GLTF_PT_export_animation_shapekeys(bpy.types.Panel):
diff --git a/io_scene_gltf2/blender/com/gltf2_blender_math.py b/io_scene_gltf2/blender/com/gltf2_blender_math.py
index 0498e8f8..d2a018a8 100755
--- a/io_scene_gltf2/blender/com/gltf2_blender_math.py
+++ b/io_scene_gltf2/blender/com/gltf2_blender_math.py
@@ -98,7 +98,7 @@ def swizzle_yup_value(value: typing.Any) -> typing.Any:
     return value
 
 
-def transform(v: typing.Union[Vector, Quaternion], data_path: str, transform: Matrix = Matrix.Identity(4)) -> typing \
+def transform(v: typing.Union[Vector, Quaternion], data_path: str, transform: Matrix = Matrix.Identity(4), need_rotation_correction: bool = False) -> typing \
         .Union[Vector, Quaternion]:
     """Manage transformations."""
     target = get_target_property_name(data_path)
@@ -116,25 +116,31 @@ def transform(v: typing.Union[Vector, Quaternion], data_path: str, transform: Ma
     if transform_func is None:
         raise RuntimeError("Cannot transform values at {}".format(data_path))
 
-    return transform_func(v, transform)
+    return transform_func(v, transform, need_rotation_correction)
 
 
-def transform_location(location: Vector, transform: Matrix = Matrix.Identity(4)) -> Vector:
+def transform_location(location: Vector, transform: Matrix = Matrix.Identity(4), need_rotation_correction:bool = False) -> Vector:
     """Transform location."""
+    correction = Quaternion((2**0.5/2, -2**0.5/2, 0.0, 0.0))
     m = Matrix.Translation(location)
+    if need_rotation_correction:
+        m @= correction.to_matrix().to_4x4()
     m = transform @ m
     return m.to_translation()
 
 
-def transform_rotation(rotation: Quaternion, transform: Matrix = Matrix.Identity(4)) -> Quaternion:
+def transform_rotation(rotation: Quaternion, transform: Matrix = Matrix.Identity(4), need_rotation_correction: bool = False) -> Quaternion:
     """Transform rotation."""
     rotation.normalize()
+    correction = Quaternion((2**0.5/2, -2**0.5/2, 0.0, 0.0))
     m = rotation.to_matrix().to_4x4()
+    if need_rotation_correction:
+        m @= correction.to_matrix().to_4x4()
     m = transform @ m
     return m.to_quaternion()
 
 
-def transform_scale(scale: Vector, transform: Matrix = Matrix.Identity(4)) -> Vector:
+def transform_scale(scale: Vector, transform: Matrix = Matrix.Identity(4), need_rotation_correction: bool  = False) -> Vector:
     """Transform scale."""
     m = Matrix.Identity(4)
     m[0][0] = scale.x
@@ -145,7 +151,7 @@ def transform_scale(scale: Vector, transform: Matrix = Matrix.Identity(4)) -> Ve
     return m.to_scale()
 
 
-def transform_value(value: Vector, _: Matrix = Matrix.Identity(4)) -> Vector:
+def transform_value(value: Vector, _: Matrix = Matrix.Identity(4), need_rotation_correction: bool = False) -> Vector:
     """Transform value."""
     return value
 
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_export_keys.py b/io_scene_gltf2/blender/exp/gltf2_blender_export_keys.py
index 61a9f5bf..812db3f9 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_export_keys.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_export_keys.py
@@ -19,6 +19,7 @@ VISIBLE = 'gltf_visible'
 RENDERABLE = 'gltf_renderable'
 ACTIVE_COLLECTION = 'gltf_active_collection'
 SKINS = 'gltf_skins'
+DEF_BONES_ONLY = 'gltf_def_bones'
 DISPLACEMENT = 'gltf_displacement'
 FORCE_SAMPLING = 'gltf_force_sampling'
 FRAME_RANGE = 'gltf_frame_range'
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_extract.py b/io_scene_gltf2/blender/exp/gltf2_blender_extract.py
index f5b69f13..d81bd706 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_extract.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_extract.py
@@ -9,10 +9,14 @@ from ...io.com.gltf2_io_debug import print_console
 from io_scene_gltf2.blender.exp import gltf2_blender_gather_skins
 
 
-def extract_primitives(glTF, blender_mesh, library, blender_object, blender_vertex_groups, modifiers, export_settings):
+def extract_primitives(blender_mesh, uuid_for_skined_data, blender_vertex_groups, modifiers, export_settings):
     """Extract primitives from a mesh."""
     print_console('INFO', 'Extracting primitive: ' + blender_mesh.name)
 
+    blender_object = None
+    if uuid_for_skined_data:
+        blender_object = export_settings['vtree'].nodes[uuid_for_skined_data].blender_object
+
     use_normals = export_settings[gltf2_blender_export_keys.NORMALS]
     if use_normals:
         blender_mesh.calc_normals_split()
@@ -57,7 +61,7 @@ def extract_primitives(glTF, blender_mesh, library, blender_object, blender_vert
             armature = None
 
         if armature:
-            skin = gltf2_blender_gather_skins.gather_skin(armature, export_settings)
+            skin = gltf2_blender_gather_skins.gather_skin(export_settings['vtree'].nodes[uuid_for_skined_data].armature, export_settings)
             if not skin:
                 armature = None
 
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather.py
index 31c0fa62..f515da8c 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gather.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather.py
@@ -7,10 +7,12 @@ from io_scene_gltf2.io.com import gltf2_io
 from io_scene_gltf2.io.com.gltf2_io_debug import print_console
 from io_scene_gltf2.blender.exp import gltf2_blender_gather_nodes
 from io_scene_gltf2.blender.exp import gltf2_blender_gather_animations
+from io_scene_gltf2.blender.exp import gltf2_blender_gather_animation_sampler_keyframes
 from io_scene_gltf2.blender.exp.gltf2_blender_gather_cache import cached
 from ..com.gltf2_blender_extras import generate_extras
 from io_scene_gltf2.blender.exp import gltf2_blender_export_keys
 from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
+from io_scene_gltf2.blender.exp import gltf2_blender_gather_tree
 
 
 def gather_gltf2(export_settings):
@@ -22,12 +24,18 @@ def gather_gltf2(export_settings):
     scenes = []
     animations = []  # unfortunately animations in gltf2 are just as 'root' as scenes.
     active_scene = None
+    store_user_scene = bpy.context.scene
     for blender_scene in bpy.data.scenes:
         scenes.append(__gather_scene(blender_scene, export_settings))
         if export_settings[gltf2_blender_export_keys.ANIMATIONS]:
+            # resetting object cache
+            gltf2_blender_gather_animation_sampler_keyframes.get_object_matrix.reset_cache()
             animations += __gather_animations(blender_scene, export_settings)
         if bpy.context.scene.name == blender_scene.name:
             active_scene = len(scenes) -1
+
+    # restore user scene
+    bpy.context.window.scene = store_user_scene
     return active_scene, scenes, animations
 
 
@@ -40,14 +48,25 @@ def __gather_scene(blender_scene, export_settings):
         nodes=[]
     )
 
-    for blender_object in blender_scene.objects:
-        if blender_object.parent is None:
-            node = gltf2_blender_gather_nodes.gather_node(
-                blender_object,
-                blender_object.library.name if blender_object.library else None,
-                blender_scene, None, export_settings)
-            if node is not None:
-                scene.nodes.append(node)
+
+    vtree = gltf2_blender_gather_tree.VExportTree(export_settings)
+    vtree.construct(blender_scene)
+    vtree.search_missing_armature() # In case armature are no parented correctly
+
+    export_user_extensions('vtree_before_filter_hook', export_settings, vtree)
+
+    # Now, we can filter tree if needed
+    vtree.filter()
+
+    export_user_extensions('vtree_after_filter_hook', export_settings, vtree)
+
+    export_settings['vtree'] = vtree
+
+    for r in [vtree.nodes[r] for r in vtree.roots]:
+        node = gltf2_blender_gather_nodes.gather_node(
+            r, export_settings)
+        if node is not None:
+            scene.nodes.append(node)
 
     export_user_extensions('gather_scene_hook', export_settings, scene, blender_scene)
 
@@ -58,15 +77,16 @@ def __gather_animations(blender_scene, export_settings):
     animations = []
     merged_tracks = {}
 
-    for blender_object in blender_scene.objects:
+    vtree = export_settings['vtree']
+    for obj_uuid in vtree.get_all_objects():
+        blender_object = vtree.nodes[obj_uuid].blender_object
+
+        # Do not manage not exported objects
+        if vtree.nodes[obj_uuid].node is None:
+            continue
 
-        # First check if this object is exported or not. Do not export animation of not exported object
-        obj_node = gltf2_blender_gather_nodes.gather_node(blender_object,
-            blender_object.library.name if blender_object.library else None,
-            blender_scene, None, export_settings)
-        if obj_node is not None:
-            animations_, merged_tracks = gltf2_blender_gather_animations.gather_animations(blender_object, merged_tracks, len(animations), export_settings)
-            animations += animations_
+        animations_, merged_tracks = gltf2_blender_gather_animations.gather_animations(obj_uuid, merged_tracks, len(animations), export_settings)
+        animations += animations_
 
     if export_settings['gltf_nla_strips'] is False:
         # Fake an animation with all animations of the sc

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list