From 45b8bb745e8074373ecad6b3ca9c482154a57262 Mon Sep 17 00:00:00 2001 From: SaracenOne Date: Thu, 30 Dec 2021 14:07:56 +0000 Subject: Clean preview_node of all none VisualInstances --- editor/plugins/node_3d_editor_plugin.cpp | 32 ++++++++++++++++++++++++++++++++ editor/plugins/node_3d_editor_plugin.h | 3 +++ 2 files changed, 35 insertions(+) diff --git a/editor/plugins/node_3d_editor_plugin.cpp b/editor/plugins/node_3d_editor_plugin.cpp index f49b749046..7133bf3f63 100644 --- a/editor/plugins/node_3d_editor_plugin.cpp +++ b/editor/plugins/node_3d_editor_plugin.cpp @@ -3984,6 +3984,37 @@ AABB Node3DEditorViewport::_calculate_spatial_bounds(const Node3D *p_parent, boo return bounds; } +Node *Node3DEditorViewport::_sanitize_preview_node(Node *p_node) const { + Node3D *node_3d = Object::cast_to(p_node); + if (node_3d == nullptr) { + Node3D *replacement_node = memnew(Node3D); + replacement_node->set_name(p_node->get_name()); + p_node->replace_by(replacement_node); + memdelete(p_node); + p_node = replacement_node; + } else { + VisualInstance3D *visual_instance = Object::cast_to(node_3d); + if (visual_instance == nullptr) { + Node3D *replacement_node = memnew(Node3D); + replacement_node->set_name(node_3d->get_name()); + replacement_node->set_visible(node_3d->is_visible()); + replacement_node->set_transform(node_3d->get_transform()); + replacement_node->set_rotation_edit_mode(node_3d->get_rotation_edit_mode()); + replacement_node->set_rotation_order(node_3d->get_rotation_order()); + replacement_node->set_as_top_level(node_3d->is_set_as_top_level()); + p_node->replace_by(replacement_node); + memdelete(p_node); + p_node = replacement_node; + } + } + + for (int i = 0; i < p_node->get_child_count(); i++) { + _sanitize_preview_node(p_node->get_child(i)); + } + + return p_node; +} + void Node3DEditorViewport::_create_preview(const Vector &files) const { for (int i = 0; i < files.size(); i++) { String path = files[i]; @@ -4000,6 +4031,7 @@ void Node3DEditorViewport::_create_preview(const Vector &files) const { if (scene.is_valid()) { Node *instance = scene->instantiate(); if (instance) { + instance = _sanitize_preview_node(instance); preview_node->add_child(instance); } } diff --git a/editor/plugins/node_3d_editor_plugin.h b/editor/plugins/node_3d_editor_plugin.h index 8d647808ba..cd14855291 100644 --- a/editor/plugins/node_3d_editor_plugin.h +++ b/editor/plugins/node_3d_editor_plugin.h @@ -384,6 +384,9 @@ private: Vector3 _get_instance_position(const Point2 &p_pos) const; static AABB _calculate_spatial_bounds(const Node3D *p_parent, bool p_exclude_top_level_transform = true); + + Node *_sanitize_preview_node(Node *p_node) const; + void _create_preview(const Vector &files) const; void _remove_preview(); bool _cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node); -- cgit v1.2.3