@@ -6747,8 +6747,35 @@ class VisualShaderNodePluginDefaultEditor : public VBoxContainer {
67476747 undo_redo->add_do_property (node.ptr (), p_property, p_value);
67486748 undo_redo->add_undo_property (node.ptr (), p_property, node->get (p_property));
67496749
6750+ Ref<VisualShaderNode> vsnode = editor->get_visual_shader ()->get_node (shader_type, node_id);
6751+ ERR_FAIL_COND (vsnode.is_null ());
6752+
6753+ // Check for invalid connections due to removed ports.
6754+ // We need to know the new state of the node to generate the proper undo/redo instructions.
6755+ // Quite hacky but the best way I could come up with for now.
6756+ Ref<VisualShaderNode> vsnode_new = vsnode->duplicate ();
6757+ vsnode_new->set (p_property, p_value);
6758+ const int input_port_count = vsnode_new->get_input_port_count ();
6759+ const int output_port_count = vsnode_new->get_output_port_count ();
6760+
6761+ List<VisualShader::Connection> conns;
6762+ editor->get_visual_shader ()->get_node_connections (shader_type, &conns);
6763+ VisualShaderGraphPlugin *graph_plugin = editor->get_graph_plugin ();
6764+ bool undo_node_already_updated = false ;
6765+ for (const VisualShader::Connection &c : conns) {
6766+ if ((c.from_node == node_id && c.from_port >= output_port_count) || (c.to_node == node_id && c.to_port >= input_port_count)) {
6767+ undo_redo->add_do_method (editor->get_visual_shader ().ptr (), " disconnect_nodes" , shader_type, c.from_node , c.from_port , c.to_node , c.to_port );
6768+ undo_redo->add_do_method (graph_plugin, " disconnect_nodes" , shader_type, c.from_node , c.from_port , c.to_node , c.to_port );
6769+ // We need to update the node before reconnecting to avoid accessing a non-existing port.
6770+ undo_redo->add_undo_method (graph_plugin, " update_node_deferred" , shader_type, node_id);
6771+ undo_node_already_updated = true ;
6772+ undo_redo->add_undo_method (editor->get_visual_shader ().ptr (), " connect_nodes" , shader_type, c.from_node , c.from_port , c.to_node , c.to_port );
6773+ undo_redo->add_undo_method (graph_plugin, " connect_nodes" , shader_type, c.from_node , c.from_port , c.to_node , c.to_port );
6774+ }
6775+ }
6776+
67506777 if (p_value.get_type () == Variant::OBJECT) {
6751- Ref<Resource> prev_res = node ->get (p_property);
6778+ Ref<Resource> prev_res = vsnode ->get (p_property);
67526779 Ref<Resource> curr_res = p_value;
67536780
67546781 if (curr_res.is_null ()) {
@@ -6763,14 +6790,16 @@ class VisualShaderNodePluginDefaultEditor : public VBoxContainer {
67636790 }
67646791 }
67656792 if (p_property != " constant" ) {
6766- VisualShaderGraphPlugin *graph_plugin = editor->get_graph_plugin ();
67676793 if (graph_plugin) {
67686794 undo_redo->add_do_method (editor, " _update_next_previews" , node_id);
67696795 undo_redo->add_undo_method (editor, " _update_next_previews" , node_id);
67706796 undo_redo->add_do_method (graph_plugin, " update_node_deferred" , shader_type, node_id);
6771- undo_redo->add_undo_method (graph_plugin, " update_node_deferred" , shader_type, node_id);
6797+ if (!undo_node_already_updated) {
6798+ undo_redo->add_undo_method (graph_plugin, " update_node_deferred" , shader_type, node_id);
6799+ }
67726800 }
67736801 }
6802+
67746803 undo_redo->commit_action ();
67756804
67766805 updating = false ;
0 commit comments