From 4791fe8e6451aa6ce62e530fe12904f5cfc112eb Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 5 May 2021 14:22:53 +0300 Subject: [PATCH] Minor refactoring of the Blender exporter --- blender/io_mspgl/export_light.py | 5 ++--- blender/io_mspgl/material.py | 23 +++++++++-------------- blender/io_mspgl/util.py | 8 ++++++++ 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/blender/io_mspgl/export_light.py b/blender/io_mspgl/export_light.py index 43270558..4da2ba29 100644 --- a/blender/io_mspgl/export_light.py +++ b/blender/io_mspgl/export_light.py @@ -15,8 +15,7 @@ class LightExporter: pos = obj.matrix_world@mathutils.Vector((0.0, 0.0, 0.0, 1.0)) light_res.statements.append(Statement("position", *tuple(pos))) - c = light.color - e = light.energy - light_res.statements.append(Statement("color", c.r*e, c.g*e, c.b*e)) + c = light.color*light.energy + light_res.statements.append(Statement("color", *tuple(c))) return light_res diff --git a/blender/io_mspgl/material.py b/blender/io_mspgl/material.py index 2613edac..aba58e55 100644 --- a/blender/io_mspgl/material.py +++ b/blender/io_mspgl/material.py @@ -1,14 +1,8 @@ import os -def get_linked_node_and_socket(node_tree, socket): - for l in node_tree.links: - if socket==l.to_socket: - return (l.from_node, l.from_socket) - elif socket==l.from_socket: - return (l.to_node, l.to_socket) - return (None, None) - def check_group(node_tree, group, func): + from .util import get_linked_node_and_socket + output = group.node_tree.nodes["Group Output"] from_node, _ = get_linked_node_and_socket(group.node_tree, output.inputs[0]) if from_node: @@ -23,6 +17,8 @@ def check_invert_green(node_tree, node): elif node.type!='COMBRGB': return (None, None) + from .util import get_linked_node_and_socket + green, g_sock = get_linked_node_and_socket(node_tree, node.inputs["G"]) if not green or green.type!='MATH' or green.operation!='SUBTRACT': return (None, None) @@ -55,6 +51,8 @@ class MaterialProperty: self.value = input_socket.default_value if self.tex_keyword: + from .util import get_linked_node_and_socket + from_node, _ = get_linked_node_and_socket(node_tree, input_socket) alpha_from = None if from_node: @@ -104,15 +102,12 @@ class Material: elif self.render_mode=='CUSTOM' and not self.shader: raise Exception("Invalid configuration on material {}: No shader for custom rendering".format(self.name)) - out_node = None - for n in material.node_tree.nodes: - if n.type=='OUTPUT_MATERIAL': - out_node = n - break - + out_node = next((n for n in material.node_tree.nodes if n.type=='OUTPUT_MATERIAL'), None) if not out_node: raise Exception("No output node found on material {}".format(self.name)) + from .util import get_linked_node_and_socket + surface_node, _ = get_linked_node_and_socket(material.node_tree, out_node.inputs["Surface"]) if not surface_node: if self.render_mode=='BUILTIN': diff --git a/blender/io_mspgl/util.py b/blender/io_mspgl/util.py index 3b2f718b..e77e5447 100644 --- a/blender/io_mspgl/util.py +++ b/blender/io_mspgl/util.py @@ -77,3 +77,11 @@ def make_unique(values): result.append(i) seen.add(i) return result + +def get_linked_node_and_socket(node_tree, socket): + for l in node_tree.links: + if socket==l.to_socket: + return (l.from_node, l.from_socket) + elif socket==l.from_socket: + return (l.to_node, l.to_socket) + return (None, None) -- 2.45.2