2424 PortTypeEnum ,
2525 ViewerEnum
2626)
27+ from NodeGraphQt .errors import NodeCreationError , NodeDeletionError
2728from NodeGraphQt .nodes .backdrop_node import BackdropNode
2829from NodeGraphQt .nodes .base_node import BaseNode
2930from NodeGraphQt .nodes .group_node import GroupNode
@@ -1123,7 +1124,7 @@ def format_color(clr):
11231124
11241125 self .node_created .emit (node )
11251126 return node
1126- raise TypeError ( ' \n \n >> Cannot find node:\t "{}"\n ' .format (node_type ))
1127+ raise NodeCreationError ( 'Can \' t find node: "{}"' .format (node_type ))
11271128
11281129 def add_node (self , node , pos = None , selected = True , push_undo = True ):
11291130 """
@@ -1198,6 +1199,10 @@ def delete_node(self, node, push_undo=True):
11981199 push_undo = push_undo )
11991200 p .clear_connections ()
12001201
1202+ # collapse group node before removing.
1203+ if isinstance (node , GroupNode ) and node .is_expanded :
1204+ node .collapse ()
1205+
12011206 if push_undo :
12021207 self ._undo_stack .push (NodeRemovedCmd (self , node ))
12031208 self ._undo_stack .endMacro ()
@@ -1223,6 +1228,10 @@ def remove_node(self, node, push_undo=True):
12231228 if push_undo :
12241229 self ._undo_stack .beginMacro ('delete node: "{}"' .format (node .name ()))
12251230
1231+ # collapse group node before removing.
1232+ if isinstance (node , GroupNode ) and node .is_expanded :
1233+ node .collapse ()
1234+
12261235 if isinstance (node , BaseNode ):
12271236 for p in node .input_ports ():
12281237 if p .locked ():
@@ -1260,6 +1269,11 @@ def delete_nodes(self, nodes, push_undo=True):
12601269 if push_undo :
12611270 self ._undo_stack .beginMacro ('deleted "{}" nodes' .format (len (nodes )))
12621271 for node in nodes :
1272+
1273+ # collapse group node before removing.
1274+ if isinstance (node , GroupNode ) and node .is_expanded :
1275+ node .collapse ()
1276+
12631277 if isinstance (node , BaseNode ):
12641278 for p in node .input_ports ():
12651279 if p .locked ():
@@ -2472,12 +2486,34 @@ def delete_node(self, node, push_undo=True):
24722486 if node in port_nodes and node .parent_port is not None :
24732487 # note: port nodes can only be deleted by deleting the parent
24742488 # port object.
2475- raise RuntimeError (
2476- 'Can\' t delete node "{}" it is attached to port "{}"'
2477- .format (node , node .parent_port )
2489+ raise NodeDeletionError (
2490+ '{} can\' t be deleted as it is attached to a port!' .format (node )
24782491 )
24792492 super (SubGraph , self ).delete_node (node , push_undo = push_undo )
24802493
2494+ def delete_nodes (self , nodes , push_undo = True ):
2495+ """
2496+ Remove a list of specified nodes from the node graph.
2497+
2498+ Args:
2499+ nodes (list[NodeGraphQt.BaseNode]): list of node instances.
2500+ push_undo (bool): register the command to the undo stack. (default: True)
2501+ """
2502+ if not nodes :
2503+ return
2504+
2505+ port_nodes = self .get_input_port_nodes () + self .get_output_port_nodes ()
2506+ for node in nodes :
2507+ if node in port_nodes and node .parent_port is not None :
2508+ # note: port nodes can only be deleted by deleting the parent
2509+ # port object.
2510+ raise NodeDeletionError (
2511+ '{} can\' t be deleted as it is attached to a port!'
2512+ .format (node )
2513+ )
2514+
2515+ super (SubGraph , self ).delete_nodes (nodes , push_undo = push_undo )
2516+
24812517 def collapse_graph (self , clear_session = True ):
24822518 """
24832519 Collapse the current sub graph and hide its widget.
0 commit comments