Skip to content

Commit 03c3c5f

Browse files
Set position to zero when saving a positioned branch as scene
1 parent 506d6e4 commit 03c3c5f

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

editor/scene_tree_dock.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,22 @@ void SceneTreeDock::_replace_with_branch_scene(const String &p_file, Node *base)
436436

437437
instantiated_scene->set_unique_name_in_owner(base->is_unique_name_in_owner());
438438

439+
Node2D *copy_2d = Object::cast_to<Node2D>(instantiated_scene);
440+
Node2D *base_2d = Object::cast_to<Node2D>(base);
441+
if (copy_2d && base_2d) {
442+
copy_2d->set_position(base_2d->get_position());
443+
copy_2d->set_rotation(base_2d->get_rotation());
444+
copy_2d->set_scale(base_2d->get_scale());
445+
}
446+
447+
Node3D *copy_3d = Object::cast_to<Node3D>(instantiated_scene);
448+
Node3D *base_3d = Object::cast_to<Node3D>(base);
449+
if (copy_3d && base_3d) {
450+
copy_3d->set_position(base_3d->get_position());
451+
copy_3d->set_rotation(base_3d->get_rotation());
452+
copy_3d->set_scale(base_3d->get_scale());
453+
}
454+
439455
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
440456
undo_redo->create_action(TTR("Replace with Branch Scene"));
441457

@@ -3259,6 +3275,36 @@ void SceneTreeDock::_new_scene_from(const String &p_file) {
32593275
// Root node cannot ever be unique name in its own Scene!
32603276
copy->set_unique_name_in_owner(false);
32613277

3278+
const Dictionary dict = new_scene_from_dialog->get_selected_options();
3279+
bool reset_position = dict.get(TTR("Reset Position"), true);
3280+
bool reset_scale = dict.get(TTR("Reset Scale"), false);
3281+
bool reset_rotation = dict.get(TTR("Reset Rotation"), false);
3282+
3283+
Node2D *copy_2d = Object::cast_to<Node2D>(copy);
3284+
if (copy_2d != nullptr) {
3285+
if (reset_position) {
3286+
copy_2d->set_position(Vector2(0, 0));
3287+
}
3288+
if (reset_rotation) {
3289+
copy_2d->set_rotation(0);
3290+
}
3291+
if (reset_scale) {
3292+
copy_2d->set_scale(Size2(1, 1));
3293+
}
3294+
}
3295+
Node3D *copy_3d = Object::cast_to<Node3D>(copy);
3296+
if (copy_3d != nullptr) {
3297+
if (reset_position) {
3298+
copy_3d->set_position(Vector3(0, 0, 0));
3299+
}
3300+
if (reset_rotation) {
3301+
copy_3d->set_rotation(Vector3(0, 0, 0));
3302+
}
3303+
if (reset_scale) {
3304+
copy_3d->set_scale(Vector3(0, 0, 0));
3305+
}
3306+
}
3307+
32623308
Ref<PackedScene> sdata = memnew(PackedScene);
32633309
Error err = sdata->pack(copy);
32643310
memdelete(copy);
@@ -4668,6 +4714,9 @@ SceneTreeDock::SceneTreeDock(Node *p_scene_root, EditorSelection *p_editor_selec
46684714

46694715
new_scene_from_dialog = memnew(EditorFileDialog);
46704716
new_scene_from_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
4717+
new_scene_from_dialog->add_option(TTR("Reset Position"), Vector<String>(), true);
4718+
new_scene_from_dialog->add_option(TTR("Reset Rotation"), Vector<String>(), false);
4719+
new_scene_from_dialog->add_option(TTR("Reset Scale"), Vector<String>(), false);
46714720
add_child(new_scene_from_dialog);
46724721
new_scene_from_dialog->connect("file_selected", callable_mp(this, &SceneTreeDock::_new_scene_from));
46734722

0 commit comments

Comments
 (0)