glTF importer: check that primitive exists (some invalid glTF files don't have any)

This commit is contained in:
Julien Duroure 2019-10-08 22:02:44 +02:00
parent cb4e5b248c
commit a1dbf26e37
2 changed files with 21 additions and 19 deletions

View File

@ -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": (1, 0, 1),
"version": (1, 0, 2),
'blender': (2, 81, 6),
'location': 'File > Import-Export',
'description': 'Import-Export as glTF 2.0',

View File

@ -282,27 +282,29 @@ class BlenderGlTF():
mesh.shapekey_names = []
used_names = set()
for sk, target in enumerate(mesh.primitives[0].targets or []):
if 'POSITION' not in target:
mesh.shapekey_names.append(None)
continue
# Some invalid glTF files has empty primitive tab
if len(mesh.primitives) > 0:
for sk, target in enumerate(mesh.primitives[0].targets or []):
if 'POSITION' not in target:
mesh.shapekey_names.append(None)
continue
# Check if glTF file has some extras with targetNames. Otherwise
# use the name of the POSITION accessor on the first primitive.
shapekey_name = None
if mesh.extras is not None:
if 'targetNames' in mesh.extras and sk < len(mesh.extras['targetNames']):
shapekey_name = mesh.extras['targetNames'][sk]
if shapekey_name is None:
if gltf.data.accessors[target['POSITION']].name is not None:
shapekey_name = gltf.data.accessors[target['POSITION']].name
if shapekey_name is None:
shapekey_name = "target_" + str(sk)
# Check if glTF file has some extras with targetNames. Otherwise
# use the name of the POSITION accessor on the first primitive.
shapekey_name = None
if mesh.extras is not None:
if 'targetNames' in mesh.extras and sk < len(mesh.extras['targetNames']):
shapekey_name = mesh.extras['targetNames'][sk]
if shapekey_name is None:
if gltf.data.accessors[target['POSITION']].name is not None:
shapekey_name = gltf.data.accessors[target['POSITION']].name
if shapekey_name is None:
shapekey_name = "target_" + str(sk)
shapekey_name = BlenderGlTF.find_unused_name(used_names, shapekey_name)
used_names.add(shapekey_name)
shapekey_name = BlenderGlTF.find_unused_name(used_names, shapekey_name)
used_names.add(shapekey_name)
mesh.shapekey_names.append(shapekey_name)
mesh.shapekey_names.append(shapekey_name)
@staticmethod
def find_unused_name(haystack, desired_name):