From: Mikko Rasa Date: Sat, 5 Mar 2022 17:48:51 +0000 (+0200) Subject: Refactor the material node processing code X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=9dffddef4118104fae810c5ec74fc55c00dee7ba;p=libs%2Fgl.git Refactor the material node processing code --- diff --git a/blender/io_mspgl/material.py b/blender/io_mspgl/material.py index 51915444..db68d0ba 100644 --- a/blender/io_mspgl/material.py +++ b/blender/io_mspgl/material.py @@ -8,51 +8,90 @@ def compute_render_method_hash(material): descr += "{}={}".format(m.tag, m.shader) return hash(descr) -def check_group(node_tree, group, func): - from .util import get_linked_node_and_socket +class PropertyNode: + def __init__(self, node_tree, node, socket): + self.node_tree = node_tree + self.node = node + self.socket = socket + self.type = None + self.data = None + self.input = None + + checks = [self.check_group, + self.check_gray, + self.check_normal, + self.check_invert_green, + self.check_additive_blend, + self.check_texture] + for c in checks: + if c(): + break + + def set_input_from_linked(self, input_sock): + 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: - from_node, _ = func(group.node_tree, from_node) - if from_node and from_node.type=='GROUP_INPUT': - return get_linked_node_and_socket(node_tree, group.inputs[0]) - return (None, None) + from_node, from_sock = get_linked_node_and_socket(self.node_tree, input_sock) + self.input = PropertyNode(self.node_tree, from_node, from_sock) + return self.input -def check_invert_green(node_tree, node): - if node.type=='GROUP': - return check_group(node_tree, node, check_invert_green) - elif node.type!='COMBRGB': - return (None, None) + def check_group(self): + if self.node.type!='GROUP': + return - from .util import get_linked_node_and_socket + from .util import get_linked_node_and_socket + + output = self.node.node_tree.nodes["Group Output"] + from_node, from_sock = get_linked_node_and_socket(self.node.node_tree, output.inputs[0]) + inner = PropertyNode(self.node.node_tree, from_node, from_sock) + if inner.input: + # TODO This currently only supports a single operation inside the group + if inner.input.node.type=='GROUP_INPUT': + self.type = inner.type + self.data = inner.data + return self.set_input_from_linked(self.node.inputs[0]) + + def check_gray(self): + if self.node.type=='RGBTOBW': + self.type = 'GRAY' + self.set_input_from_linked(self.node.inputs["Color"]) + + def check_normal(self): + if self.node.type=='NORMAL_MAP': + self.type = 'NORMAL' + return self.set_input_from_linked(self.node.inputs["Color"]) + + def check_invert_green(self): + if self.node.type!='COMBRGB': + return + + from .util import get_linked_node_and_socket - green, _ = get_linked_node_and_socket(node_tree, node.inputs["G"]) - if not green or green.type!='MATH' or green.operation!='SUBTRACT': - return (None, None) - green, _ = get_linked_node_and_socket(node_tree, green.inputs[1]) + green, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs["G"]) + if not green or green.type!='MATH' or green.operation!='SUBTRACT': + return + green, _ = get_linked_node_and_socket(self.node_tree, green.inputs[1]) - red, _ = get_linked_node_and_socket(node_tree, node.inputs["R"]) - blue, _ = get_linked_node_and_socket(node_tree, node.inputs["B"]) - if not red or red.type!='SEPRGB' or blue!=red or green!=red: - return (None, None) + red, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs["R"]) + blue, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs["B"]) + if not red or red.type!='SEPRGB' or blue!=red or green!=red: + return - return get_linked_node_and_socket(node_tree, red.inputs["Image"]) + self.type = 'INVERT_GREEN' + return self.set_input_from_linked(red.inputs["Image"]) -def check_additive_blend(node_tree, node): - if node.type=='GROUP': - return check_group(node_tree, node, check_additive_blend) - elif node.type=='ADD_SHADER': - from .util import get_linked_node_and_socket + def check_additive_blend(self): + if self.node.type=='ADD_SHADER': + from .util import get_linked_node_and_socket - shader1, _ = get_linked_node_and_socket(node_tree, node.inputs[0]) - shader2, _ = get_linked_node_and_socket(node_tree, node.inputs[1]) - if shader1.type=='BSDF_TRANSPARENT': - return get_linked_node_and_socket(node_tree, node.inputs[1]) - elif shader2.type=='BSDF_TRANSPARENT': - return get_linked_node_and_socket(node_tree, node.inputs[0]) + for i in range(2): + shader, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs[i]) + if shader.type=='BSDF_TRANSPARENT': + self.type = 'ADDITIVE' + return self.set_input_from_linked(self.node.inputs[1-i]) - return (None, None) + def check_texture(self): + if self.node.type=='TEX_IMAGE': + self.type = 'TEXTURE' def get_unlit_inputs(node_tree, node, additive): from .util import get_linked_node_and_socket @@ -102,28 +141,29 @@ class MaterialProperty: if self.tex_keyword: from .util import get_linked_node_and_socket - from_node, _ = get_linked_node_and_socket(node_tree, input_socket) + from_node, from_sock = get_linked_node_and_socket(node_tree, input_socket) alpha_from = None if from_node: channels = None - if from_node.type=='NORMAL_MAP': - from_node, _ = get_linked_node_and_socket(node_tree, from_node.inputs["Color"]) - invert, _ = check_invert_green(node_tree, from_node) - channels = ['R', 'G', 'B'] - if invert: - from_node = invert - channels[1] = '~G'; - elif from_node.type=='RGBTOBW': - from_node, _ = get_linked_node_and_socket(node_tree, from_node.inputs["Color"]) - channels = ['Y'] + top_node = PropertyNode(node_tree, from_node, from_sock) + n = top_node + while n: + if n.type=='NORMAL': + channels = ['R', 'G', 'B'] + elif n.type=='GRAY': + channels = ['Y'] + elif n.type=='INVERT_GREEN': + channels[1] = '~G' + elif n.type=='TEXTURE': + self.texture = n.node + n = n.input if alpha_socket: alpha_from, _ = get_linked_node_and_socket(node_tree, alpha_socket) - if alpha_from and alpha_from!=from_node: + if alpha_from and alpha_from!=self.texture: raise Exception("Separate textures for color and alpha are not supported") - if from_node.type=='TEX_IMAGE': - self.texture = from_node + if self.texture: if channels: self.tex_channels = channels elif alpha_from: