Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Duroure <julien.duroure@gmail.com>2020-07-21 21:19:01 +0300
committerJulien Duroure <julien.duroure@gmail.com>2020-07-21 21:19:01 +0300
commit3ea1673580ab68ecb713da3233a4f6beaafff5d9 (patch)
tree2464ed49e12a5d244b5003013b45f085cea63893 /io_scene_gltf2/blender/exp/gltf2_blender_gather_primitive_attributes.py
parent2b4bf943d0a323e6b0430179cee7fa7ffb5907d3 (diff)
glTF exporter: performance: using numpy
Thanks scurest!
Diffstat (limited to 'io_scene_gltf2/blender/exp/gltf2_blender_gather_primitive_attributes.py')
-rwxr-xr-xio_scene_gltf2/blender/exp/gltf2_blender_gather_primitive_attributes.py185
1 files changed, 76 insertions, 109 deletions
diff --git a/io_scene_gltf2/blender/exp/gltf2_blender_gather_primitive_attributes.py b/io_scene_gltf2/blender/exp/gltf2_blender_gather_primitive_attributes.py
index f5856257..8912d921 100755
--- a/io_scene_gltf2/blender/exp/gltf2_blender_gather_primitive_attributes.py
+++ b/io_scene_gltf2/blender/exp/gltf2_blender_gather_primitive_attributes.py
@@ -12,12 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
+import numpy as np
+
from . import gltf2_blender_export_keys
from io_scene_gltf2.io.com import gltf2_io
from io_scene_gltf2.io.com import gltf2_io_constants
from io_scene_gltf2.io.com import gltf2_io_debug
from io_scene_gltf2.io.exp import gltf2_io_binary_data
-from io_scene_gltf2.blender.exp import gltf2_blender_utils
def gather_primitive_attributes(blender_primitive, export_settings):
@@ -36,72 +37,79 @@ def gather_primitive_attributes(blender_primitive, export_settings):
return attributes
+def array_to_accessor(array, component_type, data_type, include_max_and_min=False):
+ dtype = gltf2_io_constants.ComponentType.to_numpy_dtype(component_type)
+ num_elems = gltf2_io_constants.DataType.num_elements(data_type)
+
+ if type(array) is not np.ndarray:
+ array = np.array(array, dtype=dtype)
+ array = array.reshape(len(array) // num_elems, num_elems)
+
+ assert array.dtype == dtype
+ assert array.shape[1] == num_elems
+
+ amax = None
+ amin = None
+ if include_max_and_min:
+ amax = np.amax(array, axis=0).tolist()
+ amin = np.amin(array, axis=0).tolist()
+
+ return gltf2_io.Accessor(
+ buffer_view=gltf2_io_binary_data.BinaryData(array.tobytes()),
+ byte_offset=None,
+ component_type=component_type,
+ count=len(array),
+ extensions=None,
+ extras=None,
+ max=amax,
+ min=amin,
+ name=None,
+ normalized=None,
+ sparse=None,
+ type=data_type,
+ )
+
+
def __gather_position(blender_primitive, export_settings):
position = blender_primitive["attributes"]["POSITION"]
- componentType = gltf2_io_constants.ComponentType.Float
return {
- "POSITION": gltf2_io.Accessor(
- buffer_view=gltf2_io_binary_data.BinaryData.from_list(position, componentType),
- byte_offset=None,
- component_type=componentType,
- count=len(position) // gltf2_io_constants.DataType.num_elements(gltf2_io_constants.DataType.Vec3),
- extensions=None,
- extras=None,
- max=gltf2_blender_utils.max_components(position, gltf2_io_constants.DataType.Vec3),
- min=gltf2_blender_utils.min_components(position, gltf2_io_constants.DataType.Vec3),
- name=None,
- normalized=None,
- sparse=None,
- type=gltf2_io_constants.DataType.Vec3
+ "POSITION": array_to_accessor(
+ position,
+ component_type=gltf2_io_constants.ComponentType.Float,
+ data_type=gltf2_io_constants.DataType.Vec3,
+ include_max_and_min=True
)
}
def __gather_normal(blender_primitive, export_settings):
- if export_settings[gltf2_blender_export_keys.NORMALS]:
- normal = blender_primitive["attributes"]['NORMAL']
- return {
- "NORMAL": gltf2_io.Accessor(
- buffer_view=gltf2_io_binary_data.BinaryData.from_list(normal, gltf2_io_constants.ComponentType.Float),
- byte_offset=None,
- component_type=gltf2_io_constants.ComponentType.Float,
- count=len(normal) // gltf2_io_constants.DataType.num_elements(gltf2_io_constants.DataType.Vec3),
- extensions=None,
- extras=None,
- max=None,
- min=None,
- name=None,
- normalized=None,
- sparse=None,
- type=gltf2_io_constants.DataType.Vec3
- )
- }
- return {}
+ if not export_settings[gltf2_blender_export_keys.NORMALS]:
+ return {}
+ normal = blender_primitive["attributes"].get('NORMAL')
+ if not normal:
+ return {}
+ return {
+ "NORMAL": array_to_accessor(
+ normal,
+ component_type=gltf2_io_constants.ComponentType.Float,
+ data_type=gltf2_io_constants.DataType.Vec3,
+ )
+ }
def __gather_tangent(blender_primitive, export_settings):
- if export_settings[gltf2_blender_export_keys.TANGENTS]:
- if blender_primitive["attributes"].get('TANGENT') is not None:
- tangent = blender_primitive["attributes"]['TANGENT']
- return {
- "TANGENT": gltf2_io.Accessor(
- buffer_view=gltf2_io_binary_data.BinaryData.from_list(
- tangent, gltf2_io_constants.ComponentType.Float),
- byte_offset=None,
- component_type=gltf2_io_constants.ComponentType.Float,
- count=len(tangent) // gltf2_io_constants.DataType.num_elements(gltf2_io_constants.DataType.Vec4),
- extensions=None,
- extras=None,
- max=None,
- min=None,
- name=None,
- normalized=None,
- sparse=None,
- type=gltf2_io_constants.DataType.Vec4
- )
- }
-
- return {}
+ if not export_settings[gltf2_blender_export_keys.TANGENTS]:
+ return {}
+ tangent = blender_primitive["attributes"].get('TANGENT')
+ if not tangent:
+ return {}
+ return {
+ "TANGENT": array_to_accessor(
+ tangent,
+ component_type=gltf2_io_constants.ComponentType.Float,
+ data_type=gltf2_io_constants.DataType.Vec4,
+ )
+ }
def __gather_texcoord(blender_primitive, export_settings):
@@ -111,20 +119,10 @@ def __gather_texcoord(blender_primitive, export_settings):
tex_coord_id = 'TEXCOORD_' + str(tex_coord_index)
while blender_primitive["attributes"].get(tex_coord_id) is not None:
tex_coord = blender_primitive["attributes"][tex_coord_id]
- attributes[tex_coord_id] = gltf2_io.Accessor(
- buffer_view=gltf2_io_binary_data.BinaryData.from_list(
- tex_coord, gltf2_io_constants.ComponentType.Float),
- byte_offset=None,
+ attributes[tex_coord_id] = array_to_accessor(
+ tex_coord,
component_type=gltf2_io_constants.ComponentType.Float,
- count=len(tex_coord) // gltf2_io_constants.DataType.num_elements(gltf2_io_constants.DataType.Vec2),
- extensions=None,
- extras=None,
- max=None,
- min=None,
- name=None,
- normalized=None,
- sparse=None,
- type=gltf2_io_constants.DataType.Vec2
+ data_type=gltf2_io_constants.DataType.Vec2,
)
tex_coord_index += 1
tex_coord_id = 'TEXCOORD_' + str(tex_coord_index)
@@ -138,20 +136,10 @@ def __gather_colors(blender_primitive, export_settings):
color_id = 'COLOR_' + str(color_index)
while blender_primitive["attributes"].get(color_id) is not None:
internal_color = blender_primitive["attributes"][color_id]
- attributes[color_id] = gltf2_io.Accessor(
- buffer_view=gltf2_io_binary_data.BinaryData.from_list(
- internal_color, gltf2_io_constants.ComponentType.Float),
- byte_offset=None,
+ attributes[color_id] = array_to_accessor(
+ internal_color,
component_type=gltf2_io_constants.ComponentType.Float,
- count=len(internal_color) // gltf2_io_constants.DataType.num_elements(gltf2_io_constants.DataType.Vec4),
- extensions=None,
- extras=None,
- max=None,
- min=None,
- name=None,
- normalized=None,
- sparse=None,
- type=gltf2_io_constants.DataType.Vec4
+ data_type=gltf2_io_constants.DataType.Vec4,
)
color_index += 1
color_id = 'COLOR_' + str(color_index)
@@ -173,20 +161,10 @@ def __gather_skins(blender_primitive, export_settings):
# joints
internal_joint = blender_primitive["attributes"][joint_id]
- joint = gltf2_io.Accessor(
- buffer_view=gltf2_io_binary_data.BinaryData.from_list(
- internal_joint, gltf2_io_constants.ComponentType.UnsignedShort),
- byte_offset=None,
+ joint = array_to_accessor(
+ internal_joint,
component_type=gltf2_io_constants.ComponentType.UnsignedShort,
- count=len(internal_joint) // gltf2_io_constants.DataType.num_elements(gltf2_io_constants.DataType.Vec4),
- extensions=None,
- extras=None,
- max=None,
- min=None,
- name=None,
- normalized=None,
- sparse=None,
- type=gltf2_io_constants.DataType.Vec4
+ data_type=gltf2_io_constants.DataType.Vec4,
)
attributes[joint_id] = joint
@@ -201,21 +179,10 @@ def __gather_skins(blender_primitive, export_settings):
factor = 1.0 / total
internal_weight[idx:idx + 4] = [w * factor for w in weight_slice]
- weight = gltf2_io.Accessor(
- buffer_view=gltf2_io_binary_data.BinaryData.from_list(
- internal_weight, gltf2_io_constants.ComponentType.Float),
- byte_offset=None,
+ weight = array_to_accessor(
+ internal_weight,
component_type=gltf2_io_constants.ComponentType.Float,
- count=len(internal_weight) // gltf2_io_constants.DataType.num_elements(
- gltf2_io_constants.DataType.Vec4),
- extensions=None,
- extras=None,
- max=None,
- min=None,
- name=None,
- normalized=None,
- sparse=None,
- type=gltf2_io_constants.DataType.Vec4
+ data_type=gltf2_io_constants.DataType.Vec4,
)
attributes[weight_id] = weight