From d359975bd668c4b3f216fc988c194b7bb508a010 Mon Sep 17 00:00:00 2001 From: William Herald Snyder Date: Thu, 29 Sep 2022 07:11:47 -0400 Subject: [PATCH] Generation op will abort if diffuse texture isn't found + options for failing silently or with error message. TODO: found out how to create a pop-up panel with error message that fades away if not interacted with for a couple seconds... --- .../msh_material_operators.py | 36 +++++++++++++------ .../msh_material_properties.py | 1 + .../msh_material_to_blend.py | 12 +++---- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/addons/io_scene_swbf_msh/msh_material_operators.py b/addons/io_scene_swbf_msh/msh_material_operators.py index cd2d8e3..86647fc 100644 --- a/addons/io_scene_swbf_msh/msh_material_operators.py +++ b/addons/io_scene_swbf_msh/msh_material_operators.py @@ -11,6 +11,8 @@ from bpy.props import BoolProperty, EnumProperty, StringProperty from bpy.types import Operator, Menu +import os + # FillSWBFMaterialProperties @@ -42,7 +44,8 @@ class FillSWBFMaterialProperties(bpy.types.Operator): if link_node.type != 'TEX_IMAGE': continue - tex_name = link_node.image.name + tex_name = link_node.image.filepath + print(tex_name) i = tex_name.find(".tga") @@ -96,24 +99,28 @@ to provide an exact emulation""" name = "Material Name", description = "Name of material whose SWBF properties the generated nodes will emulate." ) + + fail_silently: BoolProperty( + name = "Fail Silently" + ) def execute(self, context): material = bpy.data.materials[self.material_name] - if material and material.swbf_msh_mat: - material.use_nodes = True mat_props = material.swbf_msh_mat - material.node_tree.nodes.clear() - - bsdf = material.node_tree.nodes.new("ShaderNodeBsdfPrincipled") - diffuse_texture_path = mat_props.diffuse_map - if diffuse_texture_path: + if diffuse_texture_path and os.path.exists(diffuse_texture_path): + + material.use_nodes = True + material.node_tree.nodes.clear() + + bsdf = material.node_tree.nodes.new("ShaderNodeBsdfPrincipled") + texImage = material.node_tree.nodes.new('ShaderNodeTexImage') texImage.image = bpy.data.images.load(diffuse_texture_path) texImage.image.alpha_mode = 'CHANNEL_PACKED' @@ -127,10 +134,17 @@ to provide an exact emulation""" material.node_tree.links.new(bsdf.inputs['Alpha'], texImage.outputs['Alpha']) material.use_backface_culling = not bool(mat_props.doublesided) - - output = material.node_tree.nodes.new("ShaderNodeOutputMaterial") - material.node_tree.links.new(output.inputs['Surface'], bsdf.outputs['BSDF']) + output = material.node_tree.nodes.new("ShaderNodeOutputMaterial") + material.node_tree.links.new(output.inputs['Surface'], bsdf.outputs['BSDF']) + + else: + + # Todo: figure out some way to raise an error but continue operator execution... + if self.fail_silently: + return {'CANCELLED'} + else: + raise RuntimeError(f"Diffuse texture at path: '{diffuse_texture_path}' was not found.") return {'FINISHED'} diff --git a/addons/io_scene_swbf_msh/msh_material_properties.py b/addons/io_scene_swbf_msh/msh_material_properties.py index 81d1e06..8b284ac 100644 --- a/addons/io_scene_swbf_msh/msh_material_properties.py +++ b/addons/io_scene_swbf_msh/msh_material_properties.py @@ -297,4 +297,5 @@ class MaterialPropertiesPanel(bpy.types.Panel): op_props = layout.operator("swbf_msh.generate_material_nodes", text="Generate Nodes") op_props.material_name = context.material.name + op_props.fail_silently = False diff --git a/addons/io_scene_swbf_msh/msh_material_to_blend.py b/addons/io_scene_swbf_msh/msh_material_to_blend.py index 2e1a73f..9e0da96 100644 --- a/addons/io_scene_swbf_msh/msh_material_to_blend.py +++ b/addons/io_scene_swbf_msh/msh_material_to_blend.py @@ -31,7 +31,7 @@ def find_texture_path(folder_path : str, name : str) -> str: if os.path.exists(possible_path): return possible_path - return "" + return name @@ -41,7 +41,7 @@ def swbf_material_to_blend(material_name : str, material : Material, folder_path fill_material_props(material, new_mat.swbf_msh_mat, folder_path) - bpy.ops.swbf_msh.generate_material_nodes('EXEC_DEFAULT', material_name=new_mat.name) + bpy.ops.swbf_msh.generate_material_nodes('EXEC_DEFAULT', material_name=new_mat.name, fail_silently=True) return new_mat @@ -122,10 +122,10 @@ def _fill_material_props_texture_maps(material, material_properties, folder_path t2path = find_texture_path(folder_path, material.texture2) t3path = find_texture_path(folder_path, material.texture3) - material_properties.texture_0 = t0path if t0path else material.texture0 - material_properties.texture_1 = t1path if t1path else material.texture1 - material_properties.texture_2 = t2path if t2path else material.texture2 - material_properties.texture_3 = t3path if t3path else material.texture3 + material_properties.texture_0 = t0path + material_properties.texture_1 = t1path + material_properties.texture_2 = t2path + material_properties.texture_3 = t3path material_properties.diffuse_map = t0path material_properties.distortion_map = t1path