From b8d989a4198145b509acd4a7f4bc633f7dde94c8 Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Fri, 17 Mar 2023 16:59:32 +0300 Subject: [PATCH 1/2] BLEN-358: Error in console after closing Blender --- materialx/bl_nodes/shader.py | 53 ++++++++++++++++++++++++++++++------ materialx/utils.py | 16 +++++++++++ 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/materialx/bl_nodes/shader.py b/materialx/bl_nodes/shader.py index 219d32135..72e973b50 100644 --- a/materialx/bl_nodes/shader.py +++ b/materialx/bl_nodes/shader.py @@ -5,6 +5,7 @@ import math import MaterialX as mx from .node_parser import NodeParser +from ..utils import get_mx_node_input_types from . import log @@ -227,18 +228,31 @@ class ShaderNodeMixShader(NodeParser): shader2 = self.get_input_link(2) mix = None + input_types = get_mx_node_input_types('mix', 'PBR') if shader1 is None and shader2 is None: return None if shader2 is None: - mix = self.create_node('mix', get_node_type(shader1).lower(), prefix='PBR', inputs={ + shader1_type = get_node_type(shader1) + if shader1_type not in input_types: + log.warn(f'Node {self.node.bl_idname} ({self.material.name_full}) ignored. ' + f'Input type must be of types {input_types}, actual: {shader1_type}') + return None + + mix = self.create_node('mix', shader1_type.lower(), prefix='PBR', inputs={ 'fg': shader1, 'mix': factor }) if shader1 is None: - mix = self.create_node('mix', get_node_type(shader2).lower(), prefix='PBR', inputs={ + shader2_type = get_node_type(shader2) + if shader2_type not in input_types: + log.warn(f'Node {self.node.bl_idname} ({self.material.name_full}) ignored. ' + f'Input type must be of types {input_types}, actual: {shader2_type}') + return None + + mix = self.create_node('mix', shader2_type.lower(), prefix='PBR', inputs={ 'bg': shader2, 'mix': factor }) @@ -247,11 +261,16 @@ class ShaderNodeMixShader(NodeParser): shader1_type = get_node_type(shader1) shader2_type = get_node_type(shader2) if shader1_type != shader2_type: - log.warn(f'Types of input shaders must be the same.' - f' First shader type: {shader1_type}, second shader type: {shader2_type}') + log.warn(f'Types of input shaders must be the same. ' + f'First shader type: {shader1_type}, second shader type: {shader2_type}') return None + if shader1_type not in input_types: + log.warn(f'Node {self.node.bl_idname} ({self.material.name_full}) ignored. ' + f'Input type must be of types {input_types}, actual: {shader1_type}, {shader2_type}') + return None + mix = self.create_node('mix', shader1_type.lower(), prefix='PBR', inputs={ 'fg': shader1, 'bg': shader2, @@ -277,17 +296,30 @@ class ShaderNodeAddShader(NodeParser): shader2 = self.get_input_link(1) add = None + input_types = get_mx_node_input_types('add', 'PBR') if shader1 is None and shader2 is None: return None if shader2 is None: - add = self.create_node('add', get_node_type(shader1).lower(), prefix='PBR', inputs={ + shader1_type = get_node_type(shader1) + if shader1_type not in input_types: + log.warn(f'Node {self.node.bl_idname} ({self.material.name_full}) ignored. ' + f'Input type must be of types {input_types}, actual: {shader1_type}') + return None + + add = self.create_node('add', shader1_type.lower(), prefix='PBR', inputs={ 'in1': shader1 }) if shader1 is None: - add = self.create_node('add', get_node_type(shader2).lower(), prefix='PBR', inputs={ + shader2_type = get_node_type(shader2) + if shader2_type not in input_types: + log.warn(f'Node {self.node.bl_idname} ({self.material.name_full}) ignored. ' + f'Input type must be of types {input_types}, actual: {shader2_type}') + return None + + add = self.create_node('add', shader2_type.lower(), prefix='PBR', inputs={ 'in2': shader2 }) @@ -295,11 +327,16 @@ class ShaderNodeAddShader(NodeParser): shader1_type = get_node_type(shader1) shader2_type = get_node_type(shader2) if shader1_type != shader2_type: - log.warn(f'Types of input shaders must be the same.' - f' First shader type: {shader1_type}, second shader type: {shader2_type}') + log.warn(f'Types of input shaders must be the same. ' + f'First shader type: {shader1_type}, second shader type: {shader2_type}') return None + if shader1_type not in input_types: + log.warn(f'Node {self.node.bl_idname} ({self.material.name_full}) ignored. ' + f'Input type must be of types {input_types}, actual: {shader1_type}, {shader2_type}') + return None + add = self.create_node('add', shader1_type.lower(), prefix='PBR', inputs={ 'in1': shader1, 'in2': shader2 diff --git a/materialx/utils.py b/materialx/utils.py index fbb1bb21d..6964386a4 100644 --- a/materialx/utils.py +++ b/materialx/utils.py @@ -662,3 +662,19 @@ def get_output_node(material): node.inputs['surfaceshader'].links), None) return mx_output_node + + +def get_mx_node_input_types(node_name, prefix=''): + from .nodes import mx_node_classes + + suffix = f'_{node_name}' + if prefix: + suffix = prefix + suffix + + input_types = set() + classes = tuple(cls for cls in mx_node_classes if cls.__name__.endswith(suffix)) + for cls in classes: + for nodedef, data_type in cls.get_nodedefs(): + input_types |= {p.getType() for p in nodedef.getActiveInputs()} + + return input_types -- 2.30.2 From 6fa90046d8b0406d8c0ef2e28488526f05508e90 Mon Sep 17 00:00:00 2001 From: "georgiy.m.markelov@gmail.com" Date: Mon, 20 Mar 2023 17:06:42 +0300 Subject: [PATCH 2/2] return first input for Add and Mix nodes in case of wrong links --- materialx/bl_nodes/shader.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/materialx/bl_nodes/shader.py b/materialx/bl_nodes/shader.py index 72e973b50..6ab39ce68 100644 --- a/materialx/bl_nodes/shader.py +++ b/materialx/bl_nodes/shader.py @@ -238,7 +238,7 @@ class ShaderNodeMixShader(NodeParser): if shader1_type not in input_types: log.warn(f'Node {self.node.bl_idname} ({self.material.name_full}) ignored. ' f'Input type must be of types {input_types}, actual: {shader1_type}') - return None + return shader1 mix = self.create_node('mix', shader1_type.lower(), prefix='PBR', inputs={ 'fg': shader1, @@ -250,7 +250,7 @@ class ShaderNodeMixShader(NodeParser): if shader2_type not in input_types: log.warn(f'Node {self.node.bl_idname} ({self.material.name_full}) ignored. ' f'Input type must be of types {input_types}, actual: {shader2_type}') - return None + return shader2 mix = self.create_node('mix', shader2_type.lower(), prefix='PBR', inputs={ 'bg': shader2, @@ -264,12 +264,12 @@ class ShaderNodeMixShader(NodeParser): log.warn(f'Types of input shaders must be the same. ' f'First shader type: {shader1_type}, second shader type: {shader2_type}') - return None + return shader1 if shader1_type not in input_types: log.warn(f'Node {self.node.bl_idname} ({self.material.name_full}) ignored. ' f'Input type must be of types {input_types}, actual: {shader1_type}, {shader2_type}') - return None + return shader1 mix = self.create_node('mix', shader1_type.lower(), prefix='PBR', inputs={ 'fg': shader1, @@ -306,7 +306,7 @@ class ShaderNodeAddShader(NodeParser): if shader1_type not in input_types: log.warn(f'Node {self.node.bl_idname} ({self.material.name_full}) ignored. ' f'Input type must be of types {input_types}, actual: {shader1_type}') - return None + return shader1 add = self.create_node('add', shader1_type.lower(), prefix='PBR', inputs={ 'in1': shader1 @@ -317,7 +317,7 @@ class ShaderNodeAddShader(NodeParser): if shader2_type not in input_types: log.warn(f'Node {self.node.bl_idname} ({self.material.name_full}) ignored. ' f'Input type must be of types {input_types}, actual: {shader2_type}') - return None + return shader2 add = self.create_node('add', shader2_type.lower(), prefix='PBR', inputs={ 'in2': shader2 @@ -330,12 +330,12 @@ class ShaderNodeAddShader(NodeParser): log.warn(f'Types of input shaders must be the same. ' f'First shader type: {shader1_type}, second shader type: {shader2_type}') - return None + return shader1 if shader1_type not in input_types: log.warn(f'Node {self.node.bl_idname} ({self.material.name_full}) ignored. ' f'Input type must be of types {input_types}, actual: {shader1_type}, {shader2_type}') - return None + return shader1 add = self.create_node('add', shader1_type.lower(), prefix='PBR', inputs={ 'in1': shader1, -- 2.30.2