[Bf-extensions-cvs] [eba6fe23] master: glTF importer: refactoring accessor caching

Julien Duroure noreply at git.blender.org
Thu Sep 26 21:43:43 CEST 2019


Commit: eba6fe23d3bb35175df5fd8fe3cfa25d1588c61a
Author: Julien Duroure
Date:   Thu Sep 26 21:43:14 2019 +0200
Branches: master
https://developer.blender.org/rBAeba6fe23d3bb35175df5fd8fe3cfa25d1588c61a

glTF importer: refactoring accessor caching

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

M	io_scene_gltf2/__init__.py
M	io_scene_gltf2/blender/imp/gltf2_blender_mesh.py
M	io_scene_gltf2/blender/imp/gltf2_blender_primitive.py
M	io_scene_gltf2/io/imp/gltf2_io_binary.py
M	io_scene_gltf2/io/imp/gltf2_io_gltf.py

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

diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py
index 8ce7d155..56732ad4 100755
--- a/io_scene_gltf2/__init__.py
+++ b/io_scene_gltf2/__init__.py
@@ -15,7 +15,7 @@
 bl_info = {
     'name': 'glTF 2.0 format',
     'author': 'Julien Duroure, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors',
-    "version": (0, 9, 77),
+    "version": (0, 9, 78),
     'blender': (2, 81, 6),
     'location': 'File > Import-Export',
     'description': 'Import-Export as glTF 2.0',
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py b/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py
index 0ba34dd5..2d3abb6c 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_mesh.py
@@ -40,7 +40,6 @@ class BlenderMesh():
         # primitive uses is set by giving an index into this list.
         materials = []
 
-        gltf.accessor_cache = {} # cache accessor data for primtives that share accessors
         # Process all primitives
         for prim in pymesh.primitives:
             prim.blender_texcoord = {}
@@ -77,7 +76,10 @@ class BlenderMesh():
         mesh.update()
 
         pymesh.blender_name = mesh.name
-        del gltf.accessor_cache # Remove cache
+
+        # Clear accessor cache after all primitives are done
+        gltf.accessor_cache = {}
+
         return mesh
 
     @staticmethod
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_primitive.py b/io_scene_gltf2/blender/imp/gltf2_blender_primitive.py
index 254e5c59..455cbc18 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_primitive.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_primitive.py
@@ -44,14 +44,10 @@ class BlenderPrimitive():
             pyprimitive.num_faces = 0
             return
 
-        if attributes['POSITION'] not in gltf.accessor_cache.keys():
-            positions = BinaryData.get_data_from_accessor(gltf, attributes['POSITION'])
-            gltf.accessor_cache[attributes['POSITION']] = positions
-        else:
-            positions = gltf.accessor_cache[attributes['POSITION']]
+        positions = BinaryData.get_data_from_accessor(gltf, attributes['POSITION'], cache=True)
 
         if pyprimitive.indices is not None:
-            # Not using cache, this is no usefull for indices
+            # Not using cache, this is not useful for indices
             indices = BinaryData.get_data_from_accessor(gltf, pyprimitive.indices)
             indices = [i[0] for i in indices]
         else:
@@ -114,11 +110,7 @@ class BlenderPrimitive():
 
         # Set normals
         if 'NORMAL' in attributes:
-            if attributes['NORMAL'] not in gltf.accessor_cache.keys():
-                normals = BinaryData.get_data_from_accessor(gltf, attributes['NORMAL'])
-                gltf.accessor_cache[attributes['NORMAL']] = normals
-            else:
-                normals = gltf.accessor_cache[attributes['NORMAL']]
+            normals = BinaryData.get_data_from_accessor(gltf, attributes['NORMAL'], cache=True)
 
             for bidx, pidx in vert_idxs:
                 bme_verts[bidx].normal = normals[pidx]
@@ -135,11 +127,7 @@ class BlenderPrimitive():
             layer_name = 'COLOR_%d' % set_num
             layer = BlenderPrimitive.get_layer(bme.loops.layers.color, layer_name)
 
-            if attributes[layer_name] not in gltf.accessor_cache.keys():
-                colors = BinaryData.get_data_from_accessor(gltf, attributes[layer_name])
-                gltf.accessor_cache[attributes[layer_name]] = colors
-            else:
-                colors = gltf.accessor_cache[attributes[layer_name]]
+            colors = BinaryData.get_data_from_accessor(gltf, attributes[layer_name], cache=True)
 
             # Check whether Blender takes RGB or RGBA colors (old versions only take RGB)
             num_components = len(colors[0])
@@ -177,11 +165,7 @@ class BlenderPrimitive():
 
             pyprimitive.blender_texcoord[set_num] = layer_name
 
-            if attributes[layer_name] not in gltf.accessor_cache.keys():
-                uvs = BinaryData.get_data_from_accessor(gltf, attributes[layer_name])
-                gltf.accessor_cache[attributes[layer_name]] = uvs
-            else:
-                uvs = gltf.accessor_cache[attributes[layer_name]]
+            uvs = BinaryData.get_data_from_accessor(gltf, attributes[layer_name], cache=True)
 
             for bidx, pidx in vert_idxs:
                 # UV transform
@@ -198,17 +182,8 @@ class BlenderPrimitive():
         weight_sets = []
         set_num = 0
         while 'JOINTS_%d' % set_num in attributes and 'WEIGHTS_%d' % set_num in attributes:
-            if attributes['JOINTS_%d' % set_num] not in gltf.accessor_cache.keys():
-                joint_data = BinaryData.get_data_from_accessor(gltf, attributes['JOINTS_%d' % set_num])
-                gltf.accessor_cache[attributes['JOINTS_%d' % set_num]] = joint_data
-            else:
-                joint_data = gltf.accessor_cache[attributes['JOINTS_%d' % set_num]]
-
-            if attributes['WEIGHTS_%d' % set_num] not in gltf.accessor_cache.keys():
-                weight_data = BinaryData.get_data_from_accessor(gltf, attributes['WEIGHTS_%d' % set_num])
-                gltf.accessor_cache[attributes['WEIGHTS_%d' % set_num]] = weight_data
-            else:
-                weight_data = gltf.accessor_cache[attributes['WEIGHTS_%d' % set_num]]
+            joint_data = BinaryData.get_data_from_accessor(gltf, attributes['JOINTS_%d' % set_num], cache=True)
+            weight_data = BinaryData.get_data_from_accessor(gltf, attributes['WEIGHTS_%d' % set_num], cache=True)
 
             joint_sets.append(joint_data)
             weight_sets.append(weight_data)
@@ -234,11 +209,7 @@ class BlenderPrimitive():
             layer_name = pymesh.shapekey_names[sk]
             layer = BlenderPrimitive.get_layer(bme.verts.layers.shape, layer_name)
 
-            if target['POSITION'] not in gltf.accessor_cache.keys():
-                morph_positions = BinaryData.get_data_from_accessor(gltf, target['POSITION'])
-                gltf.accessor_cache[target['POSITION']] = morph_positions
-            else:
-                morph_positions = gltf.accessor_cache[target['POSITION']]
+            morph_positions = BinaryData.get_data_from_accessor(gltf, target['POSITION'], cache=True)
 
             for bidx, pidx in vert_idxs:
                 bme_verts[bidx][layer] = (
diff --git a/io_scene_gltf2/io/imp/gltf2_io_binary.py b/io_scene_gltf2/io/imp/gltf2_io_binary.py
index 92450672..f7a101df 100755
--- a/io_scene_gltf2/io/imp/gltf2_io_binary.py
+++ b/io_scene_gltf2/io/imp/gltf2_io_binary.py
@@ -46,8 +46,11 @@ class BinaryData():
         return buffer[accessor_offset + bufferview_offset:accessor_offset + bufferview_offset + bufferView.byte_length]
 
     @staticmethod
-    def get_data_from_accessor(gltf, accessor_idx):
+    def get_data_from_accessor(gltf, accessor_idx, cache=False):
         """Get data from accessor."""
+        if accessor_idx in gltf.accessor_cache:
+            return gltf.accessor_cache[accessor_idx]
+
         accessor = gltf.data.accessors[accessor_idx]
 
         bufferView = gltf.data.buffer_views[accessor.buffer_view]  # TODO initialize with 0 when not present!
@@ -102,6 +105,9 @@ class BinaryData():
                         new_tuple += (float(i),)
                 data[idx] = new_tuple
 
+        if cache:
+            gltf.accessor_cache[accessor_idx] = data
+
         return data
 
     @staticmethod
diff --git a/io_scene_gltf2/io/imp/gltf2_io_gltf.py b/io_scene_gltf2/io/imp/gltf2_io_gltf.py
index f430027c..34f205f9 100755
--- a/io_scene_gltf2/io/imp/gltf2_io_gltf.py
+++ b/io_scene_gltf2/io/imp/gltf2_io_gltf.py
@@ -29,6 +29,7 @@ class glTFImporter():
         self.filename = filename
         self.import_settings = import_settings
         self.buffers = {}
+        self.accessor_cache = {}
 
         if 'loglevel' not in self.import_settings.keys():
             self.import_settings['loglevel'] = logging.ERROR



More information about the Bf-extensions-cvs mailing list