Skip to content

Commit

Permalink
extract nodes nodes logic and actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jchanvfx committed May 3, 2023
1 parent 742f5db commit 542dc69
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
52 changes: 51 additions & 1 deletion NodeGraphQt/base/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions examples/hotkeys/hotkey_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 15 additions & 1 deletion examples/hotkeys/hotkeys.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -269,4 +283,4 @@
}
]
}
]
]

0 comments on commit 542dc69

Please sign in to comment.