Skip to content

Commit 3158bbd

Browse files
authored
Merge pull request #95084 from Hilderin/fix-crash-reimport-scene-with-animations
Fix crash on reimport scene with animations
2 parents 9564c3d + 7694243 commit 3158bbd

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

editor/editor_data.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ void EditorSelectionHistory::add_object(ObjectID p_object, const String &p_prope
141141
current_elem_idx++;
142142
}
143143

144+
void EditorSelectionHistory::replace_object(ObjectID p_old_object, ObjectID p_new_object) {
145+
for (HistoryElement &element : history) {
146+
for (int index = 0; index < element.path.size(); index++) {
147+
if (element.path[index].object == p_old_object) {
148+
element.path.write[index].object = p_new_object;
149+
}
150+
}
151+
}
152+
}
153+
144154
int EditorSelectionHistory::get_history_len() {
145155
return history.size();
146156
}

editor/editor_data.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class EditorSelectionHistory {
7474
// Adds an object to the selection history. A property name can be passed if the target is a subresource of the given object.
7575
// If the object should not change the main screen plugin, it can be set as inspector only.
7676
void add_object(ObjectID p_object, const String &p_property = String(), bool p_inspector_only = false);
77+
void replace_object(ObjectID p_old_object, ObjectID p_new_object);
7778

7879
int get_history_len();
7980
int get_history_pos();

editor/editor_node.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4446,6 +4446,21 @@ void EditorNode::update_reimported_diff_data_for_additional_nodes(
44464446
}
44474447
}
44484448

4449+
void EditorNode::replace_history_reimported_nodes(Node *p_original_root_node, Node *p_new_root_node, Node *p_node) {
4450+
NodePath scene_path_to_node = p_original_root_node->get_path_to(p_node);
4451+
Node *new_node = p_new_root_node->get_node_or_null(scene_path_to_node);
4452+
if (new_node) {
4453+
editor_history.replace_object(p_node->get_instance_id(), new_node->get_instance_id());
4454+
} else {
4455+
editor_history.replace_object(p_node->get_instance_id(), ObjectID());
4456+
}
4457+
4458+
for (int i = 0; i < p_node->get_child_count(); i++) {
4459+
Node *child = p_node->get_child(i);
4460+
replace_history_reimported_nodes(p_original_root_node, p_new_root_node, child);
4461+
}
4462+
}
4463+
44494464
void EditorNode::open_request(const String &p_path) {
44504465
if (!opening_prev) {
44514466
List<String>::Element *prev_scene_item = previous_scenes.find(p_path);
@@ -6094,6 +6109,13 @@ void EditorNode::reload_instances_with_path_in_edited_scenes(const String &p_ins
60946109
owned_node->set_owner(nullptr);
60956110
}
60966111

6112+
// Replace the old nodes in the history with the new ones.
6113+
// Otherwise, the history will contain old nodes, and some could still be
6114+
// instantiated if used elsewhere, causing the "current edited item" to be
6115+
// linked to a node that will be destroyed later. This caused the editor to
6116+
// crash when reimporting scenes with animations when "Editable children" was enabled.
6117+
replace_history_reimported_nodes(original_node, instantiated_node, original_node);
6118+
60976119
// Delete all the remaining node children.
60986120
while (original_node->get_child_count()) {
60996121
Node *child = original_node->get_child(0);

editor/editor_node.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,8 @@ class EditorNode : public Node {
843843
List<AdditiveNodeEntry> &p_addition_list);
844844
bool is_additional_node_in_scene(Node *p_edited_scene, Node *p_reimported_root, Node *p_node);
845845

846+
void replace_history_reimported_nodes(Node *p_original_root_node, Node *p_new_root_node, Node *p_node);
847+
846848
bool is_scene_open(const String &p_path);
847849
bool is_multi_window_enabled() const;
848850

0 commit comments

Comments
 (0)