From f33612ba5ad6c65cc5cd1c101a816188e5955969 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 13 Feb 2021 20:41:33 +0200 Subject: [PATCH] Refactor material property handling in the Blender exporter --- blender/io_mspgl/export_material.py | 17 ++++++++--------- blender/io_mspgl/material.py | 27 +++++++++++++-------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/blender/io_mspgl/export_material.py b/blender/io_mspgl/export_material.py index 4ce892db..2df04ff6 100644 --- a/blender/io_mspgl/export_material.py +++ b/blender/io_mspgl/export_material.py @@ -36,7 +36,7 @@ class MaterialExporter: material = Material(material) if self.use_textures: - for p in material.properties: + for p in material.properties.values(): if p.texture: tex_name = p.texture.image.name+".tex2d" if tex_name not in resources: @@ -58,13 +58,12 @@ class MaterialExporter: mat_res = Resource(material.name+".mat", "material") st = Statement("pbr") - st.sub.append(self.create_property_statement(mat_res, material.base_color, "base_color", resources)) - st.sub.append(self.create_property_statement(mat_res, material.metalness, "metalness", resources)) - st.sub.append(self.create_property_statement(mat_res, material.roughness, "roughness", resources)) - st.sub.append(self.create_property_statement(mat_res, material.normal, "normal", resources, tex_only=True)) - st.sub.append(self.create_property_statement(mat_res, material.emission, "emission", resources)) + for kw, p in material.properties.items(): + ss = self.create_property_statement(mat_res, p, kw, resources) + if ss: + st.sub.append(ss) if self.use_textures: - first_tex = (p.texture for p in material.properties if p.texture).__next__ + first_tex = (p.texture for p in material.properties.values() if p.texture).__next__() if first_tex and not first_tex.default_filter: from .export_texture import SamplerExporter sampler_export = SamplerExporter() @@ -73,7 +72,7 @@ class MaterialExporter: return mat_res - def create_property_statement(self, mat_res, prop, keyword, resources, *, tex_only=False): + def create_property_statement(self, mat_res, prop, keyword, resources): from .datafile import Statement if self.use_textures and prop.texture: tex_res = resources[prop.texture.image.name+".tex2d"] @@ -82,7 +81,7 @@ class MaterialExporter: return Statement(keyword+"_map", fn) else: return mat_res.create_reference_statement(keyword+"_map", tex_res) - elif tex_only: + elif prop.value is None: return elif type(prop.value)==tuple: return Statement(keyword, *prop.value) diff --git a/blender/io_mspgl/material.py b/blender/io_mspgl/material.py index 0c021c22..e8a9089a 100644 --- a/blender/io_mspgl/material.py +++ b/blender/io_mspgl/material.py @@ -20,7 +20,7 @@ class MaterialProperty: self.value = input_socket.default_value[:len(self.value)-1]+(alpha_socket.default_value,) else: self.value = input_socket.default_value[:len(self.value)] - else: + elif self.value is not None: self.value = input_socket.default_value from_node, _ = get_linked_node_and_socket(node_tree, input_socket) @@ -48,12 +48,7 @@ class MaterialProperty: class Material: def __init__(self, material): self.name = material.name - - self.base_color = MaterialProperty((0.8, 0.8, 0.8, 1.0)) - self.metalness = MaterialProperty(0.0) - self.roughness = MaterialProperty(0.5) - self.normal = MaterialProperty((0.0, 0.0, 0.1)) - self.emission = MaterialProperty((0.0, 0.0, 0.0)) + self.properties = {} self.render_mode = material.render_mode self.technique = material.technique @@ -79,16 +74,20 @@ class Material: elif surface_node.type!='BSDF_PRINCIPLED': raise Exception("Unsupported surface node type "+surface_node.type) - self.base_color.set_from_input(material.node_tree, surface_node.inputs["Base Color"], surface_node.inputs["Alpha"]) - self.metalness.set_from_input(material.node_tree, surface_node.inputs["Metallic"]) - self.roughness.set_from_input(material.node_tree, surface_node.inputs["Roughness"]) - self.normal.set_from_input(material.node_tree, surface_node.inputs["Normal"]) - self.emission.set_from_input(material.node_tree, surface_node.inputs["Emission"]) + base_color = self.properties["base_color"] = MaterialProperty((0.8, 0.8, 0.8, 1.0)) + metalness = self.properties["metalness"] = MaterialProperty(0.0) + roughness = self.properties["roughness"] = MaterialProperty(0.5) + normal = self.properties["normal"] = MaterialProperty(None) + emission = self.properties["emission"] = MaterialProperty((0.0, 0.0, 0.0)) - self.properties = (self.base_color, self.metalness, self.roughness, self.normal, self.emission) + base_color.set_from_input(material.node_tree, surface_node.inputs["Base Color"], surface_node.inputs["Alpha"]) + metalness.set_from_input(material.node_tree, surface_node.inputs["Metallic"]) + roughness.set_from_input(material.node_tree, surface_node.inputs["Roughness"]) + normal.set_from_input(material.node_tree, surface_node.inputs["Normal"]) + emission.set_from_input(material.node_tree, surface_node.inputs["Emission"]) sampler_settings = None - for p in self.properties: + for p in self.properties.values(): if p.texture: settings = (p.texture.default_filter, p.texture.interpolation, p.texture.use_mipmap, p.texture.max_anisotropy) if sampler_settings is None: -- 2.45.2