Skip to content

Commit c9bf25b

Browse files
authored
Merge pull request #331 from jchanvfx/extract_node_actions_#46
extract nodes nodes logic and actions.
2 parents e3d4523 + 542dc69 commit c9bf25b

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

NodeGraphQt/base/graph.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,9 @@ def delete_nodes(self, nodes, push_undo=True):
13231323
return
13241324
node_ids = [n.id for n in nodes]
13251325
if push_undo:
1326-
self._undo_stack.beginMacro('deleted "{}" nodes'.format(len(nodes)))
1326+
self._undo_stack.beginMacro(
1327+
'deleted "{}" node(s)'.format(len(nodes))
1328+
)
13271329
for node in nodes:
13281330

13291331
# collapse group node before removing.
@@ -1351,6 +1353,54 @@ def delete_nodes(self, nodes, push_undo=True):
13511353
self._undo_stack.endMacro()
13521354
self.nodes_deleted.emit(node_ids)
13531355

1356+
def extract_nodes(self, nodes, push_undo=True, prompt_warning=True):
1357+
"""
1358+
Extract select nodes from it connections.
1359+
1360+
Args:
1361+
nodes (list[NodeGraphQt.BaseNode]): list of node instances.
1362+
push_undo (bool): register the command to the undo stack. (default: True)
1363+
prompt_warning (bool): prompt warning dialog box.
1364+
"""
1365+
if not nodes:
1366+
return
1367+
1368+
locked_ports = []
1369+
base_nodes = []
1370+
for node in nodes:
1371+
if not isinstance(node, BaseNode):
1372+
continue
1373+
1374+
for port in node.input_ports() + node.output_ports():
1375+
if port.locked():
1376+
locked_ports.append('{0.node.name}: {0.name}'.format(port))
1377+
1378+
base_nodes.append(node)
1379+
1380+
if locked_ports:
1381+
message = (
1382+
'Selected nodes cannot be extracted because the following '
1383+
'ports are locked:\n{}'.format('\n'.join(sorted(locked_ports)))
1384+
)
1385+
if prompt_warning:
1386+
self._viewer.message_dialog(message, 'Can\'t Extract Nodes')
1387+
return
1388+
1389+
if push_undo:
1390+
self._undo_stack.beginMacro(
1391+
'extracted "{}" node(s)'.format(len(nodes))
1392+
)
1393+
1394+
for node in base_nodes:
1395+
for port in node.input_ports() + node.output_ports():
1396+
for connected_port in port.connected_ports():
1397+
if connected_port.node() in base_nodes:
1398+
continue
1399+
port.disconnect_from(connected_port, push_undo=push_undo)
1400+
1401+
if push_undo:
1402+
self._undo_stack.endMacro()
1403+
13541404
def all_nodes(self):
13551405
"""
13561406
Return all nodes in the node graph.

examples/hotkeys/hotkey_functions.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ def delete_nodes(graph):
132132
graph.delete_nodes(graph.selected_nodes())
133133

134134

135+
def extract_nodes(graph):
136+
"""
137+
Extract selected nodes.
138+
"""
139+
graph.extract_nodes(graph.selected_nodes())
140+
141+
142+
def clear_node_connections(graph):
143+
"""
144+
Clear port connection on selected nodes.
145+
"""
146+
graph.undo_stack().beginMacro('clear selected node connections')
147+
for node in graph.selected_nodes():
148+
for port in node.input_ports() + node.output_ports():
149+
port.clear_connections()
150+
graph.undo_stack().endMacro()
151+
152+
135153
def select_all_nodes(graph):
136154
"""
137155
Select all nodes.

examples/hotkeys/hotkeys.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@
113113
"function_name":"duplicate_nodes",
114114
"shortcut":"Alt+C"
115115
},
116+
{
117+
"type":"command",
118+
"label":"Extract",
119+
"file":"../examples/hotkeys/hotkey_functions.py",
120+
"function_name":"extract_nodes",
121+
"shortcut":"Ctrl+Shift+X"
122+
},
123+
{
124+
"type":"command",
125+
"label":"Clear Connections",
126+
"file":"../examples/hotkeys/hotkey_functions.py",
127+
"function_name":"clear_node_connections",
128+
"shortcut":"Ctrl+D"
129+
},
116130
{
117131
"type":"command",
118132
"label":"Fit to Selection",
@@ -269,4 +283,4 @@
269283
}
270284
]
271285
}
272-
]
286+
]

0 commit comments

Comments
 (0)