From 542dc69f6bb637c66ed7a121b7ab7e22002d7419 Mon Sep 17 00:00:00 2001 From: jchanvfx Date: Thu, 4 May 2023 11:44:18 +1200 Subject: [PATCH] extract nodes nodes logic and actions. --- NodeGraphQt/base/graph.py | 52 +++++++++++++++++++++++++++- examples/hotkeys/hotkey_functions.py | 18 ++++++++++ examples/hotkeys/hotkeys.json | 16 ++++++++- 3 files changed, 84 insertions(+), 2 deletions(-) diff --git a/NodeGraphQt/base/graph.py b/NodeGraphQt/base/graph.py index b7a3faf5..8f810aca 100644 --- a/NodeGraphQt/base/graph.py +++ b/NodeGraphQt/base/graph.py @@ -1323,7 +1323,9 @@ def delete_nodes(self, nodes, push_undo=True): return node_ids = [n.id for n in nodes] if push_undo: - self._undo_stack.beginMacro('deleted "{}" nodes'.format(len(nodes))) + self._undo_stack.beginMacro( + 'deleted "{}" node(s)'.format(len(nodes)) + ) for node in nodes: # collapse group node before removing. @@ -1351,6 +1353,54 @@ def delete_nodes(self, nodes, push_undo=True): self._undo_stack.endMacro() self.nodes_deleted.emit(node_ids) + def extract_nodes(self, nodes, push_undo=True, prompt_warning=True): + """ + Extract select nodes from it connections. + + Args: + nodes (list[NodeGraphQt.BaseNode]): list of node instances. + push_undo (bool): register the command to the undo stack. (default: True) + prompt_warning (bool): prompt warning dialog box. + """ + if not nodes: + return + + locked_ports = [] + base_nodes = [] + for node in nodes: + if not isinstance(node, BaseNode): + continue + + for port in node.input_ports() + node.output_ports(): + if port.locked(): + locked_ports.append('{0.node.name}: {0.name}'.format(port)) + + base_nodes.append(node) + + if locked_ports: + message = ( + 'Selected nodes cannot be extracted because the following ' + 'ports are locked:\n{}'.format('\n'.join(sorted(locked_ports))) + ) + if prompt_warning: + self._viewer.message_dialog(message, 'Can\'t Extract Nodes') + return + + if push_undo: + self._undo_stack.beginMacro( + 'extracted "{}" node(s)'.format(len(nodes)) + ) + + for node in base_nodes: + for port in node.input_ports() + node.output_ports(): + for connected_port in port.connected_ports(): + if connected_port.node() in base_nodes: + continue + port.disconnect_from(connected_port, push_undo=push_undo) + + if push_undo: + self._undo_stack.endMacro() + def all_nodes(self): """ Return all nodes in the node graph. diff --git a/examples/hotkeys/hotkey_functions.py b/examples/hotkeys/hotkey_functions.py index 865902eb..ab1b5233 100644 --- a/examples/hotkeys/hotkey_functions.py +++ b/examples/hotkeys/hotkey_functions.py @@ -132,6 +132,24 @@ def delete_nodes(graph): graph.delete_nodes(graph.selected_nodes()) +def extract_nodes(graph): + """ + Extract selected nodes. + """ + graph.extract_nodes(graph.selected_nodes()) + + +def clear_node_connections(graph): + """ + Clear port connection on selected nodes. + """ + graph.undo_stack().beginMacro('clear selected node connections') + for node in graph.selected_nodes(): + for port in node.input_ports() + node.output_ports(): + port.clear_connections() + graph.undo_stack().endMacro() + + def select_all_nodes(graph): """ Select all nodes. diff --git a/examples/hotkeys/hotkeys.json b/examples/hotkeys/hotkeys.json index 4630d036..82c851a0 100644 --- a/examples/hotkeys/hotkeys.json +++ b/examples/hotkeys/hotkeys.json @@ -113,6 +113,20 @@ "function_name":"duplicate_nodes", "shortcut":"Alt+C" }, + { + "type":"command", + "label":"Extract", + "file":"../examples/hotkeys/hotkey_functions.py", + "function_name":"extract_nodes", + "shortcut":"Ctrl+Shift+X" + }, + { + "type":"command", + "label":"Clear Connections", + "file":"../examples/hotkeys/hotkey_functions.py", + "function_name":"clear_node_connections", + "shortcut":"Ctrl+D" + }, { "type":"command", "label":"Fit to Selection", @@ -269,4 +283,4 @@ } ] } -] \ No newline at end of file +]