Skip to content

Instantly share code, notes, and snippets.

@Theverat
Forked from IARI/nodes.py
Created July 23, 2018 23:30
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 Theverat/f6c30469e22e03202dd429fe19b24c83 to your computer and use it in GitHub Desktop.
Save Theverat/f6c30469e22e03202dd429fe19b24c83 to your computer and use it in GitHub Desktop.
Test Blender PYthon Nodes
bl_info = {
'name': 'Node Test',
'category': 'User',
'author': 'Julian Jarecki',
'blender': (2, 79, 0),
# 'location': 'File > Export',
'description': "Test custom Nodes and sockets",
}
import bpy
class MyNT(bpy.types.NodeTree):
""" This operator is only visible when Cycles is the selected render engine"""
bl_idname = "test_TNT"
bl_label = "Test Node Tree"
bl_icon = "QUESTION"
new_links = []
def update(self):
for link in self.new_links:
print("New link:", link,
"from socket:", link.from_socket.bl_idname,
"to socket:", link.to_socket.bl_idname)
# if the socket type strings aren't equal, delete the link
if link.from_socket.bl_idname != link.to_socket.bl_idname:
self.links.remove(link)
print("Types didn't match, deleted link")
# We have checked all new links, clear and wait for the next update
self.new_links.clear()
class Sock:
def draw(self, context, layout, node, x):
layout.label(self.name)
def draw_color(self, context, node):
return (1, 1, 1, 1)
class SockA(bpy.types.NodeSocket, Sock):
bl_idname = "test_sockA"
bl_label = "Socket A"
class SockB(bpy.types.NodeSocket, Sock):
bl_idname = "test_sockB"
bl_label = "Socket B"
class TestNode(bpy.types.Node):
bl_idname = "test_node"
bl_label = "Test Node"
def init(self, context):
self.inputs.new("test_sockA", "in A")
self.outputs.new("test_sockA", "out A")
self.inputs.new("test_sockB", "in B")
self.outputs.new("test_sockB", "out B")
def insert_link(self, link):
# Note that this function is called BEFORE the new link is inserted into the node tree
# So you can't delete the link in this function!
print("a link was created (Node %s)" % self.name)
node_tree = self.id_data
if link in node_tree.new_links:
print("link already flagged as new")
else:
node_tree.new_links.append(link)
def insertNode(layout, type, text, settings = {}, icon = "NONE"):
operator = layout.operator("node.add_node", text = text, icon = icon)
operator.type = type
operator.use_transform = True
for name, value in settings.items():
item = operator.settings.add()
item.name = name
item.value = value
return operator
def drawMenu(self, context):
layout = self.layout
layout.operator_context = "INVOKE_DEFAULT"
insertNode(layout, "test_node", "Test Node")
types = [MyNT,SockA,SockB,TestNode]
def register():
bpy.types.NODE_MT_add.append(drawMenu)
for t in types:
bpy.utils.register_class(t)
def unregister():
bpy.types.NODE_MT_add.remove(drawMenu)
for t in types:
bpy.utils.unregister_class(t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment