[Bf-extensions-cvs] [51e15a9d] master: glTF exporter: Big gltf primitive extraction refactoring + Blender attributes export
Julien Duroure
noreply at git.blender.org
Sun Sep 25 17:08:32 CEST 2022
Commit: 51e15a9db4ce463e01c291f6ec9152efe629bab5
Author: Julien Duroure
Date: Sun Sep 25 17:08:07 2022 +0200
Branches: master
https://developer.blender.org/rBA51e15a9db4ce463e01c291f6ec9152efe629bab5
glTF exporter: Big gltf primitive extraction refactoring + Blender attributes export
===================================================================
M io_scene_gltf2/__init__.py
M io_scene_gltf2/blender/com/gltf2_blender_conversion.py
D io_scene_gltf2/blender/exp/gltf2_blender_extract.py
M io_scene_gltf2/blender/exp/gltf2_blender_gather_primitive_attributes.py
M io_scene_gltf2/blender/exp/gltf2_blender_gather_primitives.py
A io_scene_gltf2/blender/exp/gltf2_blender_gather_primitives_extract.py
M 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 4d8acd68..610c061a 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, 4, 22),
+ "version": (3, 4, 23),
'blender': (3, 3, 0),
'location': 'File > Import-Export',
'description': 'Import-Export as glTF 2.0',
@@ -273,6 +273,12 @@ class ExportGLTF2_Base:
default=True
)
+ export_attributes: BoolProperty(
+ name='Attributes',
+ description='Export Attributes',
+ default=False
+ )
+
use_mesh_edges: BoolProperty(
name='Loose Edges',
description=(
@@ -579,6 +585,7 @@ class ExportGLTF2_Base:
export_settings['gltf_materials'] = self.export_materials
export_settings['gltf_colors'] = self.export_colors
+ export_settings['gltf_attributes'] = self.export_attributes
export_settings['gltf_cameras'] = self.export_cameras
export_settings['gltf_original_specular'] = self.export_original_specular
@@ -808,6 +815,7 @@ class GLTF_PT_export_geometry_mesh(bpy.types.Panel):
col.active = operator.export_normals
col.prop(operator, 'export_tangents')
layout.prop(operator, 'export_colors')
+ layout.prop(operator, 'export_attributes')
col = layout.column()
col.prop(operator, 'use_mesh_edges')
diff --git a/io_scene_gltf2/blender/com/gltf2_blender_conversion.py b/io_scene_gltf2/blender/com/gltf2_blender_conversion.py
index ecb91c8f..85ab654a 100755
--- a/io_scene_gltf2/blender/com/gltf2_blender_conversion.py
+++ b/io_scene_gltf2/blender/com/gltf2_blender_conversion.py
@@ -2,6 +2,8 @@
# Copyright 2018-2021 The glTF-Blender-IO authors.
from math import sin, cos
+import numpy as np
+from io_scene_gltf2.io.com import gltf2_io_constants
def texture_transform_blender_to_gltf(mapping_transform):
"""
@@ -48,3 +50,55 @@ def get_target(property):
"scale": "scale",
"value": "weights"
}.get(property)
+
+def get_component_type(attribute_component_type):
+ return {
+ "INT8": gltf2_io_constants.ComponentType.Float,
+ "BYTE_COLOR": gltf2_io_constants.ComponentType.UnsignedShort,
+ "FLOAT2": gltf2_io_constants.ComponentType.Float,
+ "FLOAT_COLOR": gltf2_io_constants.ComponentType.Float,
+ "FLOAT_VECTOR": gltf2_io_constants.ComponentType.Float,
+ "FLOAT_VECTOR_4": gltf2_io_constants.ComponentType.Float,
+ "INT": gltf2_io_constants.ComponentType.Float, # No signed Int in glTF accessor
+ "FLOAT": gltf2_io_constants.ComponentType.Float,
+ "BOOLEAN": gltf2_io_constants.ComponentType.Float
+ }.get(attribute_component_type)
+
+def get_data_type(attribute_component_type):
+ return {
+ "INT8": gltf2_io_constants.DataType.Scalar,
+ "BYTE_COLOR": gltf2_io_constants.DataType.Vec4,
+ "FLOAT2": gltf2_io_constants.DataType.Vec2,
+ "FLOAT_COLOR": gltf2_io_constants.DataType.Vec4,
+ "FLOAT_VECTOR": gltf2_io_constants.DataType.Vec3,
+ "FLOAT_VECTOR_4": gltf2_io_constants.DataType.Vec4,
+ "INT": gltf2_io_constants.DataType.Scalar,
+ "FLOAT": gltf2_io_constants.DataType.Scalar,
+ "BOOLEAN": gltf2_io_constants.DataType.Scalar,
+ }.get(attribute_component_type)
+
+def get_data_length(attribute_component_type):
+ return {
+ "INT8": 1,
+ "BYTE_COLOR": 4,
+ "FLOAT2": 2,
+ "FLOAT_COLOR": 4,
+ "FLOAT_VECTOR": 3,
+ "FLOAT_VECTOR_4": 4,
+ "INT": 1,
+ "FLOAT": 1,
+ "BOOLEAN": 1
+ }.get(attribute_component_type)
+
+def get_numpy_type(attribute_component_type):
+ return {
+ "INT8": np.float32,
+ "BYTE_COLOR": np.float32,
+ "FLOAT2": np.float32,
+ "FLOAT_COLOR": np.float32,
+ "FLOAT_VECTOR": np.float32,
+ "FLOAT_VECTOR_4": np.float32,
+ "INT": np.float32, #signed integer are not supported by glTF
+ "FLOAT": np.float32,
+ "BOOLEAN": np.float32
+ }.get(attribute_component_type)
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_extract.py b/io_scene_gltf2/blender/exp/gltf2_blender_extract.py
deleted file mode 100755
index bdea2c6f..00000000
--- a/io_scene_gltf2/blender/exp/gltf2_blender_extract.py
+++ /dev/null
@@ -1,619 +0,0 @@
-# SPDX-License-Identifier: Apache-2.0
-# Copyright 2018-2021 The glTF-Blender-IO authors.
-
-import numpy as np
-from mathutils import Vector
-
-from . import gltf2_blender_export_keys
-from ...io.com.gltf2_io_debug import print_console
-from io_scene_gltf2.blender.exp import gltf2_blender_gather_nodes
-
-
-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()
-
- use_tangents = False
- if use_normals and export_settings[gltf2_blender_export_keys.TANGENTS]:
- if blender_mesh.uv_layers.active and len(blender_mesh.uv_layers) > 0:
- try:
- blender_mesh.calc_tangents()
- use_tangents = True
- except Exception:
- print_console('WARNING', 'Could not calculate tangents. Please try to triangulate the mesh first.')
-
- tex_coord_max = 0
- if export_settings[gltf2_blender_export_keys.TEX_COORDS]:
- if blender_mesh.uv_layers.active:
- tex_coord_max = len(blender_mesh.uv_layers)
-
- color_max = 0
- if export_settings[gltf2_blender_export_keys.COLORS]:
- color_max = len(blender_mesh.vertex_colors)
-
- colors_attributes = []
- rendered_color_idx = blender_mesh.attributes.render_color_index
-
- if color_max > 0:
- colors_attributes.append(rendered_color_idx)
- # Then find other ones
- colors_attributes.extend([
- i for i in range(len(blender_mesh.color_attributes)) if i != rendered_color_idx \
- and blender_mesh.vertex_colors.find(blender_mesh.color_attributes[i].name) != -1
- ])
-
-
- armature = None
- skin = None
- if blender_vertex_groups and export_settings[gltf2_blender_export_keys.SKINS]:
- if modifiers is not None:
- modifiers_dict = {m.type: m for m in modifiers}
- if "ARMATURE" in modifiers_dict:
- modifier = modifiers_dict["ARMATURE"]
- armature = modifier.object
-
- # Skin must be ignored if the object is parented to a bone of the armature
- # (This creates an infinite recursive error)
- # So ignoring skin in that case
- is_child_of_arma = (
- armature and
- blender_object and
- blender_object.parent_type == "BONE" and
- blender_object.parent.name == armature.name
- )
- if is_child_of_arma:
- armature = None
-
- if armature:
- skin = gltf2_blender_gather_nodes.gather_skin(uuid_for_skined_data, export_settings)
- if not skin:
- armature = None
-
- use_morph_normals = use_normals and export_settings[gltf2_blender_export_keys.MORPH_NORMAL]
- use_morph_tangents = use_morph_normals and use_tangents and export_settings[gltf2_blender_export_keys.MORPH_TANGENT]
-
- key_blocks = []
- # Shape Keys can't be retrieve when using Apply Modifiers (Blender/bpy limitation)
- if export_settings[gltf2_blender_export_keys.APPLY] is False and blender_mesh.shape_keys and export_settings[gltf2_blender_export_keys.MORPH]:
- key_blocks = [
- key_block
- for key_block in blender_mesh.shape_keys.key_blocks
- if not (key_block == key_block.relative_key or key_block.mute)
- ]
-
- use_materials = export_settings[gltf2_blender_export_keys.MATERIALS]
-
- # Fetch vert positions and bone data (joint,weights)
-
- locs, morph_locs = __get_positions(blender_mesh, key_blocks, armature, blender_object, export_settings)
- if skin:
- vert_bones, num_joint_sets, need_neutral_bone = __get_bone_data(blender_mesh, skin, blender_vertex_groups)
- if need_neutral_bone is True:
- # Need to create a fake joint at root of armature
- # In order to assign not assigned vertices to it
- # But for now, this is not yet possible, we need to wait the armature node is created
- # Just store this, to be used later
- armature_uuid = export_settings['vtree'].nodes[uuid_for_skined_data].armature
- export_settings['vtree'].nodes[armature_uuid].need_neutral_bone = True
-
- # In Blender there is both per-vert data, like position, and also per-loop
- # (loop=corner-of-poly) data, like normals or UVs. glTF only has per-vert
- # data, so we need to split Blender verts up into potentially-multiple glTF
- # verts.
- #
- # First, we'll collect a "dot" for every loop: a struct that stores all the
- # attributes at that loop, namely the vertex index (which determines all
- # per-vert data), and all the per-loop data like UVs, etc.
- #
- # Each unique dot will become one unique glTF vert.
-
- # List all fields the dot struct needs.
- dot_fields = [('vertex_index', np.uint32)]
- if use_normals:
- dot_fields += [('nx', np.float32), ('ny', np.float32), ('nz', np.float32)]
- if use_tangents:
- dot_fields += [('tx', np.float32), ('ty', np.float32), ('tz', np.float32), ('tw', np.float32)]
- for uv_i in range(tex_coord_max):
- dot_fields += [('uv%dx' % uv_i, np.float32), ('uv%dy' % uv_i, np.float32)]
- for col_i, _ in enumerate(colors_attributes):
- dot_fields += [
- ('color%dr' % col_i, np.float32)
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-extensions-cvs
mailing list