From: Dustin Walde Date: Wed, 27 Oct 2021 15:28:13 +0000 (-0700) Subject: Constant cleanup and docs X-Git-Url: https://git.walde.dev/?a=commitdiff_plain;h=e14e5a8b266c693953542259542741c6441c5daa;p=blender_node_tools Constant cleanup and docs --- diff --git a/README.rst b/README.rst index ea34ca6..f34cbe1 100644 --- a/README.rst +++ b/README.rst @@ -1,7 +1,7 @@ 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 @@ -17,7 +17,7 @@ Now this can look like:: 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'] \ @@ -38,7 +38,7 @@ Outputs:: 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]") diff --git a/blender_node_tools.py b/blender_node_tools.py index 529b881..deb7beb 100644 --- a/blender_node_tools.py +++ b/blender_node_tools.py @@ -49,6 +49,11 @@ NodeGroupType = Union[ ] # 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 @@ -62,17 +67,19 @@ def build_path(*nodes: str, 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 @@ -261,19 +268,20 @@ def _parse_path(node_path: str) -> Tuple[List[str], str, int, bool, bool]: 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('[')