Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created March 15, 2017 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/21f1d403d44cbd998a808928ad5a5dc1 to your computer and use it in GitHub Desktop.
Save zeffii/21f1d403d44cbd998a808928ad5a5dc1 to your computer and use it in GitHub Desktop.
reverse lookup
import bpy
def get_socket_from_input(idx, NodeGroup, parent_material):
for n in parent_material.node_tree.nodes:
if n.bl_idname == "ShaderNodeGroup" and n.node_tree == NodeGroup:
return n.inputs[idx].links[0].from_socket
custom_ng = bpy.data.node_groups['NodeGroup.001']
t = get_socket_from_input(0, custom_ng, bpy.data.materials['Material'])
print(t)
@Secrop
Copy link

Secrop commented Mar 15, 2017

import bpy

class CleanNode(bpy.types.NodeCustomGroup):
    bl_name='cleanNode'
    bl_label='cleanNode'
    bl_icon='NONE'
    
    def getNodeTree(self, name):
        #The NodeTree is common to every instance of cleanNode,
        #Only create a new one if original is not in bpy.data
        if bpy.data.node_groups.find(name)==-1:
            #make a nodetree and add the Input/Output nodes 
            nt= bpy.data.node_groups.new(name, 'ShaderNodeTree')
            nt.nodes.new('NodeGroupInput')
            nt.nodes.new('NodeGroupOutput')
        else:
            nt= bpy.data.node_groups[name]
        return nt    
    
    def init(self, context):
        # setup the private nodetree
        self.node_tree=self.getNodeTree('MyPrivateNodeTree')
        
        # add a simple math node to the node_tree
        mathnode=self.node_tree.nodes.new('ShaderNodeMath')    

        # add a simple input to the nodetree.inputs
        # this way I can set min_value and max_value because the ntinput will be a socket interface
        ntinput=self.node_tree.inputs.new('NodeSocketFloatFactor', 'Fac01')
        ntinput.min_value=0
        ntinput.max_value=1
        
        # but I can't use ntinput to connect to anything.
        # So this will fail:
        
        # self.node_tree.links.new(ntinput, mathnode.inputs[0])
        
        
        #I must use the self.node_tree.nodes['Group Input'].outputs['Fac01']
        
        self.node_tree.links.new(self.node_tree.nodes['Group Input'].outputs['Fac01'], mathnode.inputs[0])
                    
        # The problem is that Sockets can have the same name. And the best is to use the socket.identifier
        # which we don't know, and we cannot access through ntinput.                    
            
            
            
######   Intructions for the console  ######
# import CleanNode
# bpy.utils.register_class(CleanNode.CleanNode)
# bpy.data.materials['A material to add the custom node'].node_tree.nodes.new('CleanNode')   `

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment