]> git.tdb.fi Git - libs/gl.git/commitdiff
Handle arbitrary channel inversions in material inputs.
authorMikko Rasa <tdb@tdb.fi>
Sat, 5 Mar 2022 17:56:34 +0000 (19:56 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 5 Mar 2022 17:57:27 +0000 (19:57 +0200)
blender/io_mspgl/material.py

index db68d0ba2a41553ebc5ee1817fb6768f00d0c7f6..842261303ddb01c0ea412fc5e5bde2a6f3b6973f 100644 (file)
@@ -20,7 +20,7 @@ class PropertyNode:
                checks = [self.check_group,
                        self.check_gray,
                        self.check_normal,
-                       self.check_invert_green,
+                       self.check_invert_channels,
                        self.check_additive_blend,
                        self.check_texture]
                for c in checks:
@@ -60,24 +60,29 @@ class PropertyNode:
                        self.type = 'NORMAL'
                        return self.set_input_from_linked(self.node.inputs["Color"])
 
-       def check_invert_green(self):
+       def check_invert_channels(self):
                if self.node.type!='COMBRGB':
                        return
 
                from .util import get_linked_node_and_socket
 
-               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])
+               separate = None
+               invert_channels = ""
+               for c in ("R", "G", "B"):
+                       from_node, _ = get_linked_node_and_socket(self.node_tree, self.node.inputs[c])
+                       if from_node.type=='MATH' and from_node.operation=='SUBTRACT' and not from_node.inputs[0].is_linked and from_node.inputs[0].default_value==1.0:
+                               invert_channels += c
+                               from_node, _ = get_linked_node_and_socket(self.node_tree, from_node.inputs[1])
 
-               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
+                       if from_node.type=='SEPRGB' and (separate is None or from_node==separate):
+                               separate = from_node
+                       else:
+                               return
+
+               self.type = 'INVERT'
+               self.data = invert_channels
 
-               self.type = 'INVERT_GREEN'
-               return self.set_input_from_linked(red.inputs["Image"])
+               return self.set_input_from_linked(separate.inputs["Image"])
 
        def check_additive_blend(self):
                if self.node.type=='ADD_SHADER':
@@ -152,8 +157,8 @@ class MaterialProperty:
                                                channels = ['R', 'G', 'B']
                                        elif n.type=='GRAY':
                                                channels = ['Y']
-                                       elif n.type=='INVERT_GREEN':
-                                               channels[1] = '~G'
+                                       elif n.type=='INVERT':
+                                               channels = ['~'+c if c in n.data else c for c in channels]
                                        elif n.type=='TEXTURE':
                                                self.texture = n.node
                                        n = n.input