Add a root Node3D automatically if absent when adding preview sun and sky

This makes for a smoother prototyping process compared to displaying
an error message.
This commit is contained in:
Hugo Locurcio 2021-07-07 01:59:46 +02:00
parent 506daf6d5d
commit 99909cb4f7
No known key found for this signature in database
GPG key ID: 39E8F8BE30B0A49C
3 changed files with 17 additions and 10 deletions

View file

@ -6223,8 +6223,9 @@ void Node3DEditor::_add_sun_to_scene() {
Node *base = get_tree()->get_edited_scene_root();
if (!base) {
EditorNode::get_singleton()->show_warning(TTR("A root node is needed for this operation"));
return;
// Create a root node so we can add child nodes to it.
EditorNode::get_singleton()->get_scene_tree_dock()->add_root_node(memnew(Node3D));
base = get_tree()->get_edited_scene_root();
}
ERR_FAIL_COND(!base);
Node *new_sun = preview_sun->duplicate();
@ -6241,8 +6242,9 @@ void Node3DEditor::_add_environment_to_scene() {
Node *base = get_tree()->get_edited_scene_root();
if (!base) {
EditorNode::get_singleton()->show_warning(TTR("A root node is needed for this operation"));
return;
// Create a root node so we can add child nodes to it.
EditorNode::get_singleton()->get_scene_tree_dock()->add_root_node(memnew(Node3D));
base = get_tree()->get_edited_scene_root();
}
ERR_FAIL_COND(!base);

View file

@ -1134,12 +1134,7 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
}
}
editor_data->get_undo_redo().create_action(TTR("New Scene Root"));
editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", new_node);
editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
editor_data->get_undo_redo().add_do_reference(new_node);
editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", (Object *)nullptr);
editor_data->get_undo_redo().commit_action();
add_root_node(new_node);
editor->edit_node(new_node);
editor_selection->clear();
@ -1162,6 +1157,15 @@ void SceneTreeDock::_tool_selected(int p_tool, bool p_confirm_override) {
}
}
void SceneTreeDock::add_root_node(Node *p_node) {
editor_data->get_undo_redo().create_action(TTR("New Scene Root"));
editor_data->get_undo_redo().add_do_method(editor, "set_edited_scene", p_node);
editor_data->get_undo_redo().add_do_method(scene_tree, "update_tree");
editor_data->get_undo_redo().add_do_reference(p_node);
editor_data->get_undo_redo().add_undo_method(editor, "set_edited_scene", (Object *)nullptr);
editor_data->get_undo_redo().commit_action();
}
void SceneTreeDock::_node_collapsed(Object *p_obj) {
TreeItem *ti = Object::cast_to<TreeItem>(p_obj);
if (!ti) {

View file

@ -261,6 +261,7 @@ public:
void _focus_node();
void import_subscene();
void add_root_node(Node *p_node);
void set_edited_scene(Node *p_scene);
void instantiate(const String &p_file);
void instantiate_scenes(const Vector<String> &p_files, Node *p_parent = nullptr);