diff --git a/io_scene_gltf2/__init__.py b/io_scene_gltf2/__init__.py index 8ce7d155132ad55d8e38dc972dd1b7234a74b883..56732ad41cc365270fb9f0ff105bb16d2d493b3f 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 0ba34dd5748404d090f4658269b2e8ca672ee720..2d3abb6c311942f0749a271d6dfaf11be379ff8d 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 254e5c5923f06e4e4adc106152a38e08b146a7d2..455cbc18bb4f454830453097efabbecc16780984 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 92450672a314718dc4554acdf3197ab636e9460d..f7a101df471475802f9cf9be972918e50f12d642 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 f430027c3684a13280dc516474a4d2131ebdef25..34f205f97cc60ce0452bb3d8049d91db44a2d4ad 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