[Bf-extensions-cvs] [bf867f50] blender2.8: glTF exporter: various fixes & enhancement

Julien Duroure noreply at git.blender.org
Tue Dec 18 21:35:51 CET 2018


Commit: bf867f50228505710c51eb7d76832415c36d9f74
Author: Julien Duroure
Date:   Tue Dec 18 21:31:29 2018 +0100
Branches: blender2.8
https://developer.blender.org/rBAbf867f50228505710c51eb7d76832415c36d9f74

glTF exporter: various fixes & enhancement

* Fix some Yup conversions
* reading material from glTF node group material if exists
* Fix normal export
* Round transforms near 0 and 1
* Fix exporting from Edit mode
* Various image format management

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

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.py
M	io_scene_gltf2/blender/exp/gltf2_blender_extract.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channel_target.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_materials.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_materials_pbr_metallic_roughness.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gather_nodes.py
M	io_scene_gltf2/blender/exp/gltf2_blender_get.py
M	io_scene_gltf2/blender/exp/gltf2_blender_gltf2_exporter.py
M	io_scene_gltf2/io/exp/gltf2_io_image_data.py

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

diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py
index e27ec443..0318919a 100755
--- a/io_scene_gltf2/__init__.py
+++ b/io_scene_gltf2/__init__.py
@@ -36,7 +36,7 @@ from bpy.props import (CollectionProperty,
 
 bl_info = {
     'name': 'glTF 2.0 format',
-    'author': 'Julien Duroure, Norbert Nopper, Urs Hanselmann & Moritz Becher',
+    'author': 'Julien Duroure, Norbert Nopper, Urs Hanselmann Moritz Becher, Benjamin Schmithüsen',
     "version": (0, 0, 1),
     'blender': (2, 80, 0),
     'location': 'File > Import-Export',
diff --git a/io_scene_gltf2/blender/com/gltf2_blender_math.py b/io_scene_gltf2/blender/com/gltf2_blender_math.py
index 26eb6396..dd15ce2f 100755
--- a/io_scene_gltf2/blender/com/gltf2_blender_math.py
+++ b/io_scene_gltf2/blender/com/gltf2_blender_math.py
@@ -29,7 +29,11 @@ def list_to_mathutils(values: typing.List[float], data_path: str) -> typing.Unio
     """Transform a list to blender py object."""
     target = get_target_property_name(data_path)
 
-    if target == 'location':
+    if target == 'delta_location':
+        return Vector(values)  # TODO Should be Vector(values) - Vector(something)?
+    elif target == 'delta_rotation_euler':
+        return Euler(values).to_quaternion()  # TODO Should be multiply(Euler(values).to_quaternion(), something)?
+    elif target == 'location':
         return Vector(values)
     elif target == 'rotation_axis_angle':
         angle = values[0]
@@ -75,6 +79,8 @@ def swizzle_yup(v: typing.Union[Vector, Quaternion], data_path: str) -> typing.U
     """Manage Yup."""
     target = get_target_property_name(data_path)
     swizzle_func = {
+        "delta_location": swizzle_yup_location,
+        "delta_rotation_euler": swizzle_yup_rotation,
         "location": swizzle_yup_location,
         "rotation_axis_angle": swizzle_yup_rotation,
         "rotation_euler": swizzle_yup_rotation,
@@ -114,6 +120,8 @@ def transform(v: typing.Union[Vector, Quaternion], data_path: str, transform: Ma
     """Manage transformations."""
     target = get_target_property_name(data_path)
     transform_func = {
+        "delta_location": transform_location,
+        "delta_rotation_euler": transform_rotation,
         "location": transform_location,
         "rotation_axis_angle": transform_rotation,
         "rotation_euler": transform_rotation,
@@ -157,3 +165,8 @@ def transform_value(value: Vector, _: Matrix = Matrix.Identity(4)) -> Vector:
     """Transform value."""
     return value
 
+
+def round_if_near(value: float, target: float) -> float:
+    """If value is very close to target, round to target."""
+    return value if abs(value - target) > 2.0e-6 else target
+
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_export.py b/io_scene_gltf2/blender/exp/gltf2_blender_export.py
index 1ddeb6a5..418453c2 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_export.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_export.py
@@ -12,6 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import bpy
 import sys
 import traceback
 
@@ -25,6 +26,9 @@ from io_scene_gltf2.io.exp import gltf2_io_export
 
 def save(context, export_settings):
     """Start the glTF 2.0 export and saves to content either to a .gltf or .glb file."""
+    if bpy.context.active_object is not None:
+        bpy.ops.object.mode_set(mode='OBJECT')
+
     __notify_start(context)
     json, buffer = __export(export_settings)
     __write_file(json, buffer, export_settings)
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_extract.py b/io_scene_gltf2/blender/exp/gltf2_blender_extract.py
index 8762a90f..87c9d426 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_extract.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_extract.py
@@ -648,7 +648,7 @@ def extract_primitives(glTF, blender_mesh, blender_vertex_groups, modifiers, exp
 
             bone_count = 0
 
-            if vertex.groups is not None and len(vertex.groups) > 0 and export_settings[gltf2_blender_export_keys.SKINS]:
+            if blender_vertex_groups is not None and vertex.groups is not None and len(vertex.groups) > 0 and export_settings[gltf2_blender_export_keys.SKINS]:
                 joint = []
                 weight = []
                 for group_element in vertex.groups:
@@ -668,13 +668,15 @@ def extract_primitives(glTF, blender_mesh, blender_vertex_groups, modifiers, exp
                     #
 
                     joint_index = 0
-                    modifiers_dict = {m.type: m for m in modifiers}
-                    if "ARMATURE" in modifiers_dict:
-                        armature = modifiers_dict["ARMATURE"].object
-                        skin = gltf2_blender_gather_skins.gather_skin(armature, export_settings)
-                        for index, j in enumerate(skin.joints):
-                            if j.name == vertex_group_name:
-                                joint_index = index
+
+                    if modifiers is not None:
+                        modifiers_dict = {m.type: m for m in modifiers}
+                        if "ARMATURE" in modifiers_dict:
+                            armature = modifiers_dict["ARMATURE"].object
+                            skin = gltf2_blender_gather_skins.gather_skin(armature, export_settings)
+                            for index, j in enumerate(skin.joints):
+                                if j.name == vertex_group_name:
+                                    joint_index = index
 
                     joint_weight = group_element.weight
 
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channel_target.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channel_target.py
index fbe18323..2e4ac1d7 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channel_target.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channel_target.py
@@ -67,6 +67,8 @@ def __gather_path(channels: typing.Tuple[bpy.types.FCurve],
                   ) -> str:
     target = channels[0].data_path.split('.')[-1]
     path = {
+        "delta_location": "translation",
+        "delta_rotation_euler": "rotation",
         "location": "translation",
         "rotation_axis_angle": "rotation",
         "rotation_euler": "rotation",
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_sampler_keyframes.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_sampler_keyframes.py
index 7562d8c2..6ef7fb0b 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_sampler_keyframes.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_sampler_keyframes.py
@@ -35,6 +35,8 @@ class Keyframe:
 
     def __get_target_len(self):
         length = {
+            "delta_location": 3,
+            "delta_rotation_euler": 3,
             "location": 3,
             "rotation_axis_angle": 4,
             "rotation_euler": 3,
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py
index c94f1528..6846128d 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py
@@ -107,37 +107,40 @@ def __gather_output(channels: typing.Tuple[bpy.types.FCurve],
 
     target_datapath = channels[0].data_path
 
-    transform = Matrix.Identity(4)
+    transform = blender_object.matrix_parent_inverse
+
+    isYup = export_settings[gltf2_blender_export_keys.YUP]
 
     if blender_object.type == "ARMATURE":
         bone = blender_object.path_resolve(get_target_object_path(target_datapath))
         if isinstance(bone, bpy.types.PoseBone):
-            transform = bone.bone.matrix_local
             if bone.parent is not None:
                 parent_transform = bone.parent.bone.matrix_local
-                transform = gltf2_blender_math.multiply(parent_transform.inverted(), transform)
-                # if not export_settings[gltf2_blender_export_keys.YUP]:
-                #     transform = gltf2_blender_math.multiply(gltf2_blender_math.to_zup(), transform)
+                transform = gltf2_blender_math.multiply(transform, parent_transform.inverted())
+                # if not isYup:
+                #     transform = gltf2_blender_math.multiply(transform, gltf2_blender_math.to_zup())
             else:
                 # only apply the y-up conversion to root bones, as child bones already are in the y-up space
-                if export_settings[gltf2_blender_export_keys.YUP]:
-                    transform = gltf2_blender_math.multiply(gltf2_blender_math.to_yup(), transform)
+                if isYup:
+                    transform = gltf2_blender_math.multiply(transform, gltf2_blender_math.to_yup())
+            local_transform = bone.bone.matrix_local
+            transform = gltf2_blender_math.multiply(transform, local_transform)
 
     values = []
     for keyframe in keyframes:
         # Transform the data and extract
         value = gltf2_blender_math.transform(keyframe.value, target_datapath, transform)
-        if export_settings[gltf2_blender_export_keys.YUP] and not blender_object.type == "ARMATURE":
+        if isYup and not blender_object.type == "ARMATURE":
             value = gltf2_blender_math.swizzle_yup(value, target_datapath)
         keyframe_value = gltf2_blender_math.mathutils_to_gltf(value)
         if keyframe.in_tangent is not None:
             in_tangent = gltf2_blender_math.transform(keyframe.in_tangent, target_datapath, transform)
-            if export_settings[gltf2_blender_export_keys.YUP] and not blender_object.type == "ARMATURE":
+            if isYup and not blender_object.type == "ARMATURE":
                 in_tangent = gltf2_blender_math.swizzle_yup(in_tangent, target_datapath)
             keyframe_value = gltf2_blender_math.mathutils_to_gltf(in_tangent) + keyframe_value
         if keyframe.out_tangent is not None:
             out_tangent = gltf2_blender_math.transform(keyframe.out_tangent, target_datapath, transform)
-            if export_settings[gltf2_blender_export_keys.YUP] and not blender_object.type == "ARMATURE":
+        

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list