From acf3bcb5b5c763edb83f511ecf7873a04ae23f96 Mon Sep 17 00:00:00 2001 From: Julien Duroure Date: Wed, 23 Jan 2019 21:08:22 +0100 Subject: glTF importer: fix bug when texture file is not found --- ..._blender_KHR_materials_pbrSpecularGlossiness.py | 39 ++++++++++++++-------- io_scene_gltf2/blender/imp/gltf2_blender_image.py | 21 ++++++------ .../blender/imp/gltf2_blender_map_emissive.py | 7 ++-- .../blender/imp/gltf2_blender_map_normal.py | 7 ++-- .../blender/imp/gltf2_blender_map_occlusion.py | 7 ++-- .../imp/gltf2_blender_pbrMetallicRoughness.py | 28 +++++++++++----- io_scene_gltf2/io/imp/gltf2_io_binary.py | 2 +- 7 files changed, 72 insertions(+), 39 deletions(-) diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_KHR_materials_pbrSpecularGlossiness.py b/io_scene_gltf2/blender/imp/gltf2_blender_KHR_materials_pbrSpecularGlossiness.py index 1854f45d..fec65ee4 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_KHR_materials_pbrSpecularGlossiness.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_KHR_materials_pbrSpecularGlossiness.py @@ -90,10 +90,13 @@ class BlenderKHR_materials_pbrSpecularGlossiness(): # create UV Map / Mapping / Texture nodes / separate & math and combine text_node = node_tree.nodes.new('ShaderNodeTexImage') - text_node.image = \ - bpy.data.images[ - gltf.data.images[gltf.data.textures[pbrSG['diffuseTexture']['index']].source].blender_image_name - ] + if gltf.data.images[ + gltf.data.textures[pbrSG['diffuseTexture']['index']].source].blender_image_name is not None: + text_node.image = \ + bpy.data.images[ + gltf.data.images[ + gltf.data.textures[pbrSG['diffuseTexture']['index']].source].blender_image_name + ] text_node.location = -1000, 500 combine = node_tree.nodes.new('ShaderNodeCombineRGB') @@ -191,9 +194,11 @@ class BlenderKHR_materials_pbrSpecularGlossiness(): # create UV Map / Mapping / Texture nodes / separate & math and combine text_node = node_tree.nodes.new('ShaderNodeTexImage') - text_node.image = bpy.data.images[ - gltf.data.images[gltf.data.textures[pbrSG['diffuseTexture']['index']].source].blender_image_name - ] + if gltf.data.images[ + gltf.data.textures[pbrSG['diffuseTexture']['index']].source].blender_image_name is not None: + text_node.image = bpy.data.images[ + gltf.data.images[gltf.data.textures[pbrSG['diffuseTexture']['index']].source].blender_image_name + ] if vertex_color: text_node.location = -2000, 500 else: @@ -257,11 +262,14 @@ class BlenderKHR_materials_pbrSpecularGlossiness(): elif pbrSG['specgloss_type'] == gltf.TEXTURE: BlenderTextureInfo.create(gltf, pbrSG['specularGlossinessTexture']['index']) spec_text = node_tree.nodes.new('ShaderNodeTexImage') - spec_text.image = bpy.data.images[ - gltf.data.images[ - gltf.data.textures[pbrSG['specularGlossinessTexture']['index']].source - ].blender_image_name - ] + if gltf.data.images[ + gltf.data.textures[pbrSG['specularGlossinessTexture']['index']].source + ].blender_image_name is not None: + spec_text.image = bpy.data.images[ + gltf.data.images[ + gltf.data.textures[pbrSG['specularGlossinessTexture']['index']].source + ].blender_image_name + ] spec_text.color_space = 'NONE' spec_text.location = -500, 0 @@ -288,9 +296,12 @@ class BlenderKHR_materials_pbrSpecularGlossiness(): BlenderTextureInfo.create(gltf, pbrSG['specularGlossinessTexture']['index']) spec_text = node_tree.nodes.new('ShaderNodeTexImage') - spec_text.image = bpy.data.images[gltf.data.images[ + if gltf.data.images[ gltf.data.textures[pbrSG['specularGlossinessTexture']['index']].source - ].blender_image_name] + ].blender_image_name is not None: + spec_text.image = bpy.data.images[gltf.data.images[ + gltf.data.textures[pbrSG['specularGlossinessTexture']['index']].source + ].blender_image_name] spec_text.color_space = 'NONE' spec_text.location = -1000, 0 diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_image.py b/io_scene_gltf2/blender/imp/gltf2_blender_image.py index ca1eb626..d51c463e 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_image.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_image.py @@ -43,7 +43,7 @@ class BlenderImage(): if isfile(join(dirname(gltf.filename), pyimage.uri)): return True, join(dirname(gltf.filename), pyimage.uri), basename(join(dirname(gltf.filename), pyimage.uri)) else: - pyimage.gltf.log.error("Missing file (index " + str(img_idx) + "): " + pyimage.uri) + gltf.log.error("Missing file (index " + str(img_idx) + "): " + pyimage.uri) return False, None, None if pyimage.buffer_view is None: @@ -89,13 +89,14 @@ class BlenderImage(): # Create a temp image, pack, and delete image tmp_image = tempfile.NamedTemporaryFile(delete=False) img_data, img_name = BinaryData.get_image_data(gltf, img_idx) - tmp_image.write(img_data) - tmp_image.close() - - blender_image = bpy.data.images.load(tmp_image.name) - blender_image.pack() - blender_image.name = img_name - img.blender_image_name = blender_image.name - blender_image['gltf_index'] = img_idx - os.remove(tmp_image.name) + if img_name is not None: + tmp_image.write(img_data) + tmp_image.close() + + blender_image = bpy.data.images.load(tmp_image.name) + blender_image.pack() + blender_image.name = img_name + img.blender_image_name = blender_image.name + blender_image['gltf_index'] = img_idx + os.remove(tmp_image.name) diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_map_emissive.py b/io_scene_gltf2/blender/imp/gltf2_blender_map_emissive.py index 22af5ccb..79cc93cd 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_map_emissive.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_map_emissive.py @@ -71,9 +71,12 @@ class BlenderEmissiveMap(): uvmap["gltf2_texcoord"] = 0 # TODO: set in precompute instead of here? text = node_tree.nodes.new('ShaderNodeTexImage') - text.image = bpy.data.images[gltf.data.images[ + if gltf.data.images[ gltf.data.textures[pymaterial.emissive_texture.index].source - ].blender_image_name] + ].blender_image_name is not None: + text.image = bpy.data.images[gltf.data.images[ + gltf.data.textures[pymaterial.emissive_texture.index].source + ].blender_image_name] text.label = 'EMISSIVE' text.location = -1000, 1000 add = node_tree.nodes.new('ShaderNodeAddShader') diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_map_normal.py b/io_scene_gltf2/blender/imp/gltf2_blender_map_normal.py index 0f7bab81..ba8302b7 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_map_normal.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_map_normal.py @@ -59,9 +59,12 @@ class BlenderNormalMap(): uvmap["gltf2_texcoord"] = 0 # TODO set in pre_compute instead of here text = node_tree.nodes.new('ShaderNodeTexImage') - text.image = bpy.data.images[gltf.data.images[ + if gltf.data.images[ gltf.data.textures[pymaterial.normal_texture.index].source - ].blender_image_name] + ].blender_image_name is not None: + text.image = bpy.data.images[gltf.data.images[ + gltf.data.textures[pymaterial.normal_texture.index].source + ].blender_image_name] text.label = 'NORMALMAP' text.color_space = 'NONE' text.location = -500, -500 diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_map_occlusion.py b/io_scene_gltf2/blender/imp/gltf2_blender_map_occlusion.py index 70e1e54a..e98c48ec 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_map_occlusion.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_map_occlusion.py @@ -35,7 +35,10 @@ class BlenderOcclusionMap(): BlenderTextureInfo.create(gltf, pymaterial.occlusion_texture.index) # Pack texture, but doesn't use it for now. Occlusion is calculated from Cycles. - bpy.data.images[gltf.data.images[gltf.data.textures[ + if gltf.data.images[gltf.data.textures[ pymaterial.occlusion_texture.index - ].source].blender_image_name].use_fake_user = True + ].source].blender_image_name is not None: + bpy.data.images[gltf.data.images[gltf.data.textures[ + pymaterial.occlusion_texture.index + ].source].blender_image_name].use_fake_user = True diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_pbrMetallicRoughness.py b/io_scene_gltf2/blender/imp/gltf2_blender_pbrMetallicRoughness.py index 2893c467..dcc9b3fd 100755 --- a/io_scene_gltf2/blender/imp/gltf2_blender_pbrMetallicRoughness.py +++ b/io_scene_gltf2/blender/imp/gltf2_blender_pbrMetallicRoughness.py @@ -95,9 +95,12 @@ class BlenderPbr(): # create UV Map / Mapping / Texture nodes / separate & math and combine text_node = node_tree.nodes.new('ShaderNodeTexImage') - text_node.image = bpy.data.images[gltf.data.images[ + if gltf.data.images[ gltf.data.textures[pypbr.base_color_texture.index].source - ].blender_image_name] + ].blender_image_name is not None: + text_node.image = bpy.data.images[gltf.data.images[ + gltf.data.textures[pypbr.base_color_texture.index].source + ].blender_image_name] text_node.label = 'BASE COLOR' text_node.location = -1000, 500 @@ -153,9 +156,12 @@ class BlenderPbr(): # create UV Map / Mapping / Texture nodes / separate & math and combine text_node = node_tree.nodes.new('ShaderNodeTexImage') - text_node.image = bpy.data.images[gltf.data.images[ + if gltf.data.images[ gltf.data.textures[pypbr.base_color_texture.index].source - ].blender_image_name] + ].blender_image_name is not None: + text_node.image = bpy.data.images[gltf.data.images[ + gltf.data.textures[pypbr.base_color_texture.index].source + ].blender_image_name] text_node.label = 'BASE COLOR' if vertex_color: text_node.location = -2000, 500 @@ -201,9 +207,12 @@ class BlenderPbr(): elif pypbr.metallic_type == gltf.TEXTURE: BlenderTextureInfo.create(gltf, pypbr.metallic_roughness_texture.index) metallic_text = node_tree.nodes.new('ShaderNodeTexImage') - metallic_text.image = bpy.data.images[gltf.data.images[ + if gltf.data.images[ gltf.data.textures[pypbr.metallic_roughness_texture.index].source - ].blender_image_name] + ].blender_image_name is not None: + metallic_text.image = bpy.data.images[gltf.data.images[ + gltf.data.textures[pypbr.metallic_roughness_texture.index].source + ].blender_image_name] metallic_text.color_space = 'NONE' metallic_text.label = 'METALLIC ROUGHNESS' metallic_text.location = -500, 0 @@ -234,9 +243,12 @@ class BlenderPbr(): BlenderTextureInfo.create(gltf, pypbr.metallic_roughness_texture.index) metallic_text = node_tree.nodes.new('ShaderNodeTexImage') - metallic_text.image = bpy.data.images[gltf.data.images[ + if gltf.data.images[ gltf.data.textures[pypbr.metallic_roughness_texture.index].source - ].blender_image_name] + ].blender_image_name is not None: + metallic_text.image = bpy.data.images[gltf.data.images[ + gltf.data.textures[pypbr.metallic_roughness_texture.index].source + ].blender_image_name] metallic_text.color_space = 'NONE' metallic_text.label = 'METALLIC ROUGHNESS' metallic_text.location = -1000, 0 diff --git a/io_scene_gltf2/io/imp/gltf2_io_binary.py b/io_scene_gltf2/io/imp/gltf2_io_binary.py index 9bf04fa5..7a5dd13d 100755 --- a/io_scene_gltf2/io/imp/gltf2_io_binary.py +++ b/io_scene_gltf2/io/imp/gltf2_io_binary.py @@ -163,7 +163,7 @@ class BinaryData(): with open(join(dirname(gltf.filename), pyimage.uri), 'rb') as f_: return f_.read(), basename(join(dirname(gltf.filename), pyimage.uri)) else: - pyimage.gltf.log.error("Missing file (index " + str(img_idx) + "): " + pyimage.uri) + gltf.log.error("Missing file (index " + str(img_idx) + "): " + pyimage.uri) return None, None if pyimage.buffer_view is None: -- cgit v1.2.3