blender_node_tools
==================
-A python module of functions to aid in manipulating node connections
+A python function module to aid in manipulating node connections
in Blender.
The goal is to make it simpler when adding / removing links between
link_nodes(node_tree, 'From Node', 'To Node:Input')
-Even more roundabout was the removal of links:
+Even more roundabout was the removal of links::
for link in node_tree.links:
if link.to_node == node_tree.nodes['Node'] \
Something like a compositor mix node have two inputs of the same name
-"Image". These may be targeting with a subscript index notation::
+"Image". These may be targeting with array index notation::
link_nodes(node_tree, "Image Texture", "Mix:Image[0]")
link_nodes(node_tree, "Fade Texture", "Mix:Image[1]")
] # all have a #node_tree property and are Nodes
NodeType = Union[str, Node, NodeSocket]
+INPUT_SYMBOL = '+'
+OUTPUT_SYMBOL = '-'
+SOCKET_DELIMETER = ':'
+PATH_DELIMETER = '/'
+
def is_group(node: Node) -> bool:
"""
Check if node is a *GroupNode of some sort
if input != False and output != False:
raise ValueError("Path cannot be both input and output")
- node_path = '/'.join(nodes)
+ node_path = PATH_DELIMETER.join(nodes)
+
+ def _format_socket_path(socket, symbol):
+ if isinstance(socket, str):
+ return '{}{}{}{}'.format(
+ symbol, node_path, SOCKET_DELIMETER, socket)
- if type(input) == str:
- return '+{}:{}'.format(node_path, input)
- elif input:
- return '+{}'.format(node_path)
+ return '{}{}'.format(symbol, node_path)
- if type(output) == str:
- return '-{}:{}'.format(node_path, output)
- elif input:
- return '-{}'.format(node_path)
+ if input != False:
+ return _format_socket_path(input, INPUT_SYMBOL)
+ elif output != False:
+ return _format_socket_path(output, OUTPUT_SYMBOL)
return node_path
socket_index = -1
path_parts = None
- if node_path[0] == '+':
+ if node_path[0] == INPUT_SYMBOL:
output = False
node_path = node_path[1:]
- elif node_path[0] == '-':
+ elif node_path[0] == OUTPUT_SYMBOL:
input = False
node_path = node_path[1:]
- parts = node_path.split(':')
+ parts = node_path.split(SOCKET_DELIMETER)
if len(parts) > 1:
socket = parts[-1]
- path_parts = ':'.join(parts[:-1]).split('/')
+ path_parts = SOCKET_DELIMETER.join(parts[:-1])\
+ .split(PATH_DELIMETER)
else:
- path_parts = node_path.split('/')
+ path_parts = node_path.split(PATH_DELIMETER)
if socket is not None:
left_bracket = socket.find('[')