1/**************************************************************************/
2/* scene_import_settings.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "scene_import_settings.h"
32
33#include "core/config/project_settings.h"
34#include "editor/editor_file_system.h"
35#include "editor/editor_inspector.h"
36#include "editor/editor_node.h"
37#include "editor/editor_scale.h"
38#include "editor/editor_settings.h"
39#include "editor/editor_string_names.h"
40#include "editor/gui/editor_file_dialog.h"
41#include "scene/3d/importer_mesh_instance_3d.h"
42#include "scene/animation/animation_player.h"
43#include "scene/resources/importer_mesh.h"
44#include "scene/resources/surface_tool.h"
45
46class SceneImportSettingsData : public Object {
47 GDCLASS(SceneImportSettingsData, Object)
48 friend class SceneImportSettings;
49 HashMap<StringName, Variant> *settings = nullptr;
50 HashMap<StringName, Variant> current;
51 HashMap<StringName, Variant> defaults;
52 List<ResourceImporter::ImportOption> options;
53 bool hide_options = false;
54 String path;
55
56 ResourceImporterScene::InternalImportCategory category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX;
57
58 bool _set(const StringName &p_name, const Variant &p_value) {
59 if (settings) {
60 if (defaults.has(p_name) && defaults[p_name] == p_value) {
61 settings->erase(p_name);
62 } else {
63 (*settings)[p_name] = p_value;
64 }
65
66 current[p_name] = p_value;
67
68 // SceneImportSettings must decide if a new collider should be generated or not.
69 if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE) {
70 SceneImportSettings::get_singleton()->request_generate_collider();
71 }
72
73 if (SceneImportSettings::get_singleton()->is_editing_animation()) {
74 if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
75 if (ResourceImporterScene::get_animation_singleton()->get_option_visibility(path, p_name, current)) {
76 SceneImportSettings::get_singleton()->update_view();
77 }
78 } else {
79 if (ResourceImporterScene::get_animation_singleton()->get_internal_option_update_view_required(category, p_name, current)) {
80 SceneImportSettings::get_singleton()->update_view();
81 }
82 }
83 } else {
84 if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
85 if (ResourceImporterScene::get_scene_singleton()->get_option_visibility(path, p_name, current)) {
86 SceneImportSettings::get_singleton()->update_view();
87 }
88 } else {
89 if (ResourceImporterScene::get_scene_singleton()->get_internal_option_update_view_required(category, p_name, current)) {
90 SceneImportSettings::get_singleton()->update_view();
91 }
92 }
93 }
94
95 return true;
96 }
97 return false;
98 }
99 bool _get(const StringName &p_name, Variant &r_ret) const {
100 if (settings) {
101 if (settings->has(p_name)) {
102 r_ret = (*settings)[p_name];
103 return true;
104 }
105 }
106 if (defaults.has(p_name)) {
107 r_ret = defaults[p_name];
108 return true;
109 }
110 return false;
111 }
112 void _get_property_list(List<PropertyInfo> *p_list) const {
113 if (hide_options) {
114 return;
115 }
116 for (const ResourceImporter::ImportOption &E : options) {
117 if (SceneImportSettings::get_singleton()->is_editing_animation()) {
118 if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
119 if (ResourceImporterScene::get_animation_singleton()->get_option_visibility(path, E.option.name, current)) {
120 p_list->push_back(E.option);
121 }
122 } else {
123 if (ResourceImporterScene::get_animation_singleton()->get_internal_option_visibility(category, E.option.name, current)) {
124 p_list->push_back(E.option);
125 }
126 }
127 } else {
128 if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
129 if (ResourceImporterScene::get_scene_singleton()->get_option_visibility(path, E.option.name, current)) {
130 p_list->push_back(E.option);
131 }
132 } else {
133 if (ResourceImporterScene::get_scene_singleton()->get_internal_option_visibility(category, E.option.name, current)) {
134 p_list->push_back(E.option);
135 }
136 }
137 }
138 }
139 }
140};
141
142void SceneImportSettings::_fill_material(Tree *p_tree, const Ref<Material> &p_material, TreeItem *p_parent) {
143 String import_id;
144 bool has_import_id = false;
145
146 if (p_material->has_meta("import_id")) {
147 import_id = p_material->get_meta("import_id");
148 has_import_id = true;
149 } else if (!p_material->get_name().is_empty()) {
150 import_id = p_material->get_name();
151 has_import_id = true;
152 } else if (unnamed_material_name_map.has(p_material)) {
153 import_id = unnamed_material_name_map[p_material];
154 } else {
155 import_id = "@MATERIAL:" + itos(material_map.size());
156 unnamed_material_name_map[p_material] = import_id;
157 }
158
159 bool created = false;
160 if (!material_map.has(import_id)) {
161 MaterialData md;
162 created = true;
163 md.has_import_id = has_import_id;
164 md.material = p_material;
165
166 _load_default_subresource_settings(md.settings, "materials", import_id, ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MATERIAL);
167
168 material_map[import_id] = md;
169 }
170
171 MaterialData &material_data = material_map[import_id];
172 ERR_FAIL_COND(p_material != material_data.material);
173
174 Ref<Texture2D> icon = get_editor_theme_icon(SNAME("StandardMaterial3D"));
175
176 TreeItem *item = p_tree->create_item(p_parent);
177 if (p_material->get_name().is_empty()) {
178 item->set_text(0, TTR("<Unnamed Material>"));
179 } else {
180 item->set_text(0, p_material->get_name());
181 }
182 item->set_icon(0, icon);
183
184 item->set_meta("type", "Material");
185 item->set_meta("import_id", import_id);
186 item->set_tooltip_text(0, vformat(TTR("Import ID: %s"), import_id));
187 item->set_selectable(0, true);
188
189 if (p_tree == scene_tree) {
190 material_data.scene_node = item;
191 } else if (p_tree == mesh_tree) {
192 material_data.mesh_node = item;
193 } else {
194 material_data.material_node = item;
195 }
196
197 if (created) {
198 _fill_material(material_tree, p_material, material_tree->get_root());
199 }
200}
201
202void SceneImportSettings::_fill_mesh(Tree *p_tree, const Ref<Mesh> &p_mesh, TreeItem *p_parent) {
203 String import_id;
204
205 bool has_import_id = false;
206 if (p_mesh->has_meta("import_id")) {
207 import_id = p_mesh->get_meta("import_id");
208 has_import_id = true;
209 } else if (!p_mesh->get_name().is_empty()) {
210 import_id = p_mesh->get_name();
211 has_import_id = true;
212 } else {
213 import_id = "@MESH:" + itos(mesh_set.size());
214 }
215
216 if (!mesh_map.has(import_id)) {
217 MeshData md;
218 md.has_import_id = has_import_id;
219 md.mesh = p_mesh;
220
221 _load_default_subresource_settings(md.settings, "meshes", import_id, ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH);
222
223 mesh_map[import_id] = md;
224 }
225
226 MeshData &mesh_data = mesh_map[import_id];
227
228 Ref<Texture2D> icon = get_editor_theme_icon(SNAME("Mesh"));
229
230 TreeItem *item = p_tree->create_item(p_parent);
231 item->set_text(0, p_mesh->get_name());
232 item->set_icon(0, icon);
233
234 bool created = false;
235 if (!mesh_set.has(p_mesh)) {
236 mesh_set.insert(p_mesh);
237 created = true;
238 }
239
240 item->set_meta("type", "Mesh");
241 item->set_meta("import_id", import_id);
242 item->set_tooltip_text(0, vformat(TTR("Import ID: %s"), import_id));
243
244 item->set_selectable(0, true);
245
246 if (p_tree == scene_tree) {
247 mesh_data.scene_node = item;
248 } else {
249 mesh_data.mesh_node = item;
250 }
251
252 item->set_collapsed(true);
253
254 for (int i = 0; i < p_mesh->get_surface_count(); i++) {
255 Ref<Material> mat = p_mesh->surface_get_material(i);
256 if (mat.is_valid()) {
257 _fill_material(p_tree, mat, item);
258 }
259 }
260
261 if (created) {
262 _fill_mesh(mesh_tree, p_mesh, mesh_tree->get_root());
263 }
264}
265
266void SceneImportSettings::_fill_animation(Tree *p_tree, const Ref<Animation> &p_anim, const String &p_name, TreeItem *p_parent) {
267 if (!animation_map.has(p_name)) {
268 AnimationData ad;
269 ad.animation = p_anim;
270
271 _load_default_subresource_settings(ad.settings, "animations", p_name, ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION);
272
273 animation_map[p_name] = ad;
274 }
275
276 AnimationData &animation_data = animation_map[p_name];
277
278 Ref<Texture2D> icon = get_editor_theme_icon(SNAME("Animation"));
279
280 TreeItem *item = p_tree->create_item(p_parent);
281 item->set_text(0, p_name);
282 item->set_icon(0, icon);
283
284 item->set_meta("type", "Animation");
285 item->set_meta("import_id", p_name);
286
287 item->set_selectable(0, true);
288
289 animation_data.scene_node = item;
290}
291
292void SceneImportSettings::_fill_scene(Node *p_node, TreeItem *p_parent_item) {
293 String import_id;
294
295 if (p_node->has_meta("import_id")) {
296 import_id = p_node->get_meta("import_id");
297 } else {
298 import_id = "PATH:" + String(scene->get_path_to(p_node));
299 p_node->set_meta("import_id", import_id);
300 }
301
302 ImporterMeshInstance3D *src_mesh_node = Object::cast_to<ImporterMeshInstance3D>(p_node);
303
304 if (src_mesh_node) {
305 MeshInstance3D *mesh_node = memnew(MeshInstance3D);
306 mesh_node->set_name(src_mesh_node->get_name());
307 mesh_node->set_transform(src_mesh_node->get_transform());
308 mesh_node->set_skin(src_mesh_node->get_skin());
309 mesh_node->set_skeleton_path(src_mesh_node->get_skeleton_path());
310 if (src_mesh_node->get_mesh().is_valid()) {
311 Ref<ImporterMesh> editor_mesh = src_mesh_node->get_mesh();
312 mesh_node->set_mesh(editor_mesh->get_mesh());
313 }
314
315 p_node->replace_by(mesh_node);
316 memdelete(p_node);
317 p_node = mesh_node;
318 }
319
320 String type = p_node->get_class();
321
322 if (!has_theme_icon(type, EditorStringName(EditorIcons))) {
323 type = "Node3D";
324 }
325
326 Ref<Texture2D> icon = get_editor_theme_icon(type);
327
328 TreeItem *item = scene_tree->create_item(p_parent_item);
329 item->set_text(0, p_node->get_name());
330
331 if (p_node == scene) {
332 icon = get_editor_theme_icon(SNAME("PackedScene"));
333 item->set_text(0, TTR("Scene"));
334 }
335
336 item->set_icon(0, icon);
337
338 item->set_meta("type", "Node");
339 item->set_meta("class", type);
340 item->set_meta("import_id", import_id);
341 item->set_tooltip_text(0, vformat(TTR("Type: %s\nImport ID: %s"), type, import_id));
342
343 item->set_selectable(0, true);
344
345 if (!node_map.has(import_id)) {
346 NodeData nd;
347
348 if (p_node != scene) {
349 ResourceImporterScene::InternalImportCategory category;
350 if (src_mesh_node) {
351 category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE;
352 } else if (Object::cast_to<AnimationPlayer>(p_node)) {
353 category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE;
354
355 animation_player = Object::cast_to<AnimationPlayer>(p_node);
356 animation_player->connect(SNAME("animation_finished"), callable_mp(this, &SceneImportSettings::_animation_finished));
357 } else if (Object::cast_to<Skeleton3D>(p_node)) {
358 category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE;
359 skeletons.push_back(Object::cast_to<Skeleton3D>(p_node));
360 } else {
361 category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_NODE;
362 }
363
364 _load_default_subresource_settings(nd.settings, "nodes", import_id, category);
365 }
366
367 node_map[import_id] = nd;
368 }
369 NodeData &node_data = node_map[import_id];
370
371 node_data.node = p_node;
372 node_data.scene_node = item;
373
374 AnimationPlayer *anim_node = Object::cast_to<AnimationPlayer>(p_node);
375 if (anim_node) {
376 List<StringName> animations;
377 anim_node->get_animation_list(&animations);
378 for (const StringName &E : animations) {
379 _fill_animation(scene_tree, anim_node->get_animation(E), E, item);
380 }
381 }
382
383 for (int i = 0; i < p_node->get_child_count(); i++) {
384 _fill_scene(p_node->get_child(i), item);
385 }
386 MeshInstance3D *mesh_node = Object::cast_to<MeshInstance3D>(p_node);
387 if (mesh_node && mesh_node->get_mesh().is_valid()) {
388 if (!editing_animation) {
389 _fill_mesh(scene_tree, mesh_node->get_mesh(), item);
390 }
391
392 // Add the collider view.
393 MeshInstance3D *collider_view = memnew(MeshInstance3D);
394 collider_view->set_name("collider_view");
395 collider_view->set_visible(false);
396 mesh_node->add_child(collider_view, true);
397 collider_view->set_owner(mesh_node);
398
399 Transform3D accum_xform;
400 Node3D *base = mesh_node;
401 while (base) {
402 accum_xform = base->get_transform() * accum_xform;
403 base = Object::cast_to<Node3D>(base->get_parent());
404 }
405
406 AABB aabb = accum_xform.xform(mesh_node->get_mesh()->get_aabb());
407 if (first_aabb) {
408 contents_aabb = aabb;
409 first_aabb = false;
410 } else {
411 contents_aabb.merge_with(aabb);
412 }
413 }
414}
415
416void SceneImportSettings::_update_scene() {
417 scene_tree->clear();
418 material_tree->clear();
419 mesh_tree->clear();
420
421 // Hidden roots.
422 material_tree->create_item();
423 mesh_tree->create_item();
424
425 _fill_scene(scene, nullptr);
426}
427
428void SceneImportSettings::_update_view_gizmos() {
429 if (!is_visible()) {
430 return;
431 }
432 for (const KeyValue<String, NodeData> &e : node_map) {
433 bool show_collider_view = false;
434 if (e.value.settings.has(SNAME("generate/physics"))) {
435 show_collider_view = e.value.settings[SNAME("generate/physics")];
436 }
437
438 MeshInstance3D *mesh_node = Object::cast_to<MeshInstance3D>(e.value.node);
439 if (mesh_node == nullptr || mesh_node->get_mesh().is_null()) {
440 // Nothing to do.
441 continue;
442 }
443
444 TypedArray<Node> descendants = mesh_node->find_children("collider_view", "MeshInstance3D");
445
446 CRASH_COND_MSG(descendants.is_empty(), "This is unreachable, since the collider view is always created even when the collision is not used! If this is triggered there is a bug on the function `_fill_scene`.");
447
448 MeshInstance3D *collider_view = static_cast<MeshInstance3D *>(descendants[0].operator Object *());
449 collider_view->set_visible(show_collider_view);
450 if (generate_collider) {
451 // This collider_view doesn't have a mesh so we need to generate a new one.
452 Ref<ImporterMesh> mesh;
453 mesh.instantiate();
454 // ResourceImporterScene::get_collision_shapes() expects ImporterMesh, not Mesh.
455 // TODO: Duplicate code with EditorSceneFormatImporterESCN::import_scene()
456 // Consider making a utility function to convert from Mesh to ImporterMesh.
457 Ref<Mesh> mesh_3d_mesh = mesh_node->get_mesh();
458 Ref<ArrayMesh> array_mesh_3d_mesh = mesh_3d_mesh;
459 if (array_mesh_3d_mesh.is_valid()) {
460 // For the MeshInstance3D nodes, we need to convert the ArrayMesh to an ImporterMesh specially.
461 mesh->set_name(array_mesh_3d_mesh->get_name());
462 for (int32_t blend_i = 0; blend_i < array_mesh_3d_mesh->get_blend_shape_count(); blend_i++) {
463 mesh->add_blend_shape(array_mesh_3d_mesh->get_blend_shape_name(blend_i));
464 }
465 for (int32_t surface_i = 0; surface_i < array_mesh_3d_mesh->get_surface_count(); surface_i++) {
466 mesh->add_surface(array_mesh_3d_mesh->surface_get_primitive_type(surface_i),
467 array_mesh_3d_mesh->surface_get_arrays(surface_i),
468 array_mesh_3d_mesh->surface_get_blend_shape_arrays(surface_i),
469 array_mesh_3d_mesh->surface_get_lods(surface_i),
470 array_mesh_3d_mesh->surface_get_material(surface_i),
471 array_mesh_3d_mesh->surface_get_name(surface_i),
472 array_mesh_3d_mesh->surface_get_format(surface_i));
473 }
474 mesh->set_blend_shape_mode(array_mesh_3d_mesh->get_blend_shape_mode());
475 } else if (mesh_3d_mesh.is_valid()) {
476 // For the MeshInstance3D nodes, we need to convert the Mesh to an ImporterMesh specially.
477 mesh->set_name(mesh_3d_mesh->get_name());
478 for (int32_t surface_i = 0; surface_i < mesh_3d_mesh->get_surface_count(); surface_i++) {
479 mesh->add_surface(mesh_3d_mesh->surface_get_primitive_type(surface_i),
480 mesh_3d_mesh->surface_get_arrays(surface_i),
481 Array(),
482 mesh_3d_mesh->surface_get_lods(surface_i),
483 mesh_3d_mesh->surface_get_material(surface_i),
484 mesh_3d_mesh->surface_get_material(surface_i).is_valid() ? mesh_3d_mesh->surface_get_material(surface_i)->get_name() : String(),
485 mesh_3d_mesh->surface_get_format(surface_i));
486 }
487 }
488
489 // Generate the mesh collider.
490 Vector<Ref<Shape3D>> shapes = ResourceImporterScene::get_collision_shapes(mesh, e.value.settings, 1.0);
491 const Transform3D transform = ResourceImporterScene::get_collision_shapes_transform(e.value.settings);
492
493 Ref<ArrayMesh> collider_view_mesh;
494 collider_view_mesh.instantiate();
495 for (Ref<Shape3D> shape : shapes) {
496 Ref<ArrayMesh> debug_shape_mesh;
497 if (shape.is_valid()) {
498 debug_shape_mesh = shape->get_debug_mesh();
499 }
500 if (debug_shape_mesh.is_valid()) {
501 collider_view_mesh->add_surface_from_arrays(
502 debug_shape_mesh->surface_get_primitive_type(0),
503 debug_shape_mesh->surface_get_arrays(0));
504
505 collider_view_mesh->surface_set_material(
506 collider_view_mesh->get_surface_count() - 1,
507 collider_mat);
508 }
509 }
510
511 collider_view->set_mesh(collider_view_mesh);
512 collider_view->set_transform(transform);
513 }
514 }
515
516 generate_collider = false;
517}
518
519void SceneImportSettings::_update_camera() {
520 AABB camera_aabb;
521
522 float rot_x = cam_rot_x;
523 float rot_y = cam_rot_y;
524 float zoom = cam_zoom;
525
526 if (selected_type == "Node" || selected_type.is_empty()) {
527 camera_aabb = contents_aabb;
528 } else {
529 if (mesh_preview->get_mesh().is_valid()) {
530 camera_aabb = mesh_preview->get_transform().xform(mesh_preview->get_mesh()->get_aabb());
531 } else {
532 camera_aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
533 }
534 if (selected_type == "Mesh" && mesh_map.has(selected_id)) {
535 const MeshData &md = mesh_map[selected_id];
536 rot_x = md.cam_rot_x;
537 rot_y = md.cam_rot_y;
538 zoom = md.cam_zoom;
539 } else if (selected_type == "Material" && material_map.has(selected_id)) {
540 const MaterialData &md = material_map[selected_id];
541 rot_x = md.cam_rot_x;
542 rot_y = md.cam_rot_y;
543 zoom = md.cam_zoom;
544 }
545 }
546
547 Vector3 center = camera_aabb.get_center();
548 float camera_size = camera_aabb.get_longest_axis_size();
549
550 camera->set_orthogonal(camera_size * zoom, 0.0001, camera_size * 2);
551
552 Transform3D xf;
553 xf.basis = Basis(Vector3(1, 0, 0), rot_x) * Basis(Vector3(0, 1, 0), rot_y);
554 xf.origin = center;
555 xf.translate_local(0, 0, camera_size);
556
557 camera->set_transform(xf);
558}
559
560void SceneImportSettings::_load_default_subresource_settings(HashMap<StringName, Variant> &settings, const String &p_type, const String &p_import_id, ResourceImporterScene::InternalImportCategory p_category) {
561 if (base_subresource_settings.has(p_type)) {
562 Dictionary d = base_subresource_settings[p_type];
563 if (d.has(p_import_id)) {
564 d = d[p_import_id];
565 List<ResourceImporterScene::ImportOption> options;
566 if (editing_animation) {
567 ResourceImporterScene::get_animation_singleton()->get_internal_import_options(p_category, &options);
568 } else {
569 ResourceImporterScene::get_scene_singleton()->get_internal_import_options(p_category, &options);
570 }
571 for (const ResourceImporterScene::ImportOption &E : options) {
572 String key = E.option.name;
573 if (d.has(key)) {
574 settings[key] = d[key];
575 }
576 }
577 }
578 }
579}
580
581void SceneImportSettings::request_generate_collider() {
582 generate_collider = true;
583}
584
585void SceneImportSettings::update_view() {
586 update_view_timer->start();
587}
588
589void SceneImportSettings::open_settings(const String &p_path, bool p_for_animation) {
590 if (scene) {
591 memdelete(scene);
592 scene = nullptr;
593 }
594
595 editing_animation = p_for_animation;
596 scene_import_settings_data->settings = nullptr;
597 scene_import_settings_data->path = p_path;
598
599 // Visibility.
600 data_mode->set_tab_hidden(1, p_for_animation);
601 data_mode->set_tab_hidden(2, p_for_animation);
602 if (p_for_animation) {
603 data_mode->set_current_tab(0);
604 }
605
606 action_menu->get_popup()->set_item_disabled(action_menu->get_popup()->get_item_id(ACTION_EXTRACT_MATERIALS), p_for_animation);
607 action_menu->get_popup()->set_item_disabled(action_menu->get_popup()->get_item_id(ACTION_CHOOSE_MESH_SAVE_PATHS), p_for_animation);
608
609 base_path = p_path;
610
611 mesh_set.clear();
612 animation_map.clear();
613 material_map.clear();
614 unnamed_material_name_map.clear();
615 mesh_map.clear();
616 node_map.clear();
617 defaults.clear();
618
619 mesh_preview->hide();
620
621 selected_id = "";
622 selected_type = "";
623
624 cam_rot_x = -Math_PI / 4;
625 cam_rot_y = -Math_PI / 4;
626 cam_zoom = 1;
627
628 {
629 base_subresource_settings.clear();
630
631 Ref<ConfigFile> config;
632 config.instantiate();
633 Error err = config->load(p_path + ".import");
634 if (err == OK) {
635 List<String> keys;
636 config->get_section_keys("params", &keys);
637 for (const String &E : keys) {
638 Variant value = config->get_value("params", E);
639 if (E == "_subresources") {
640 base_subresource_settings = value;
641 } else {
642 defaults[E] = value;
643 }
644 }
645 }
646 }
647
648 scene = ResourceImporterScene::get_scene_singleton()->pre_import(p_path, defaults); // Use the scene singleton here because we want to see the full thing.
649 if (scene == nullptr) {
650 EditorNode::get_singleton()->show_warning(TTR("Error opening scene"));
651 return;
652 }
653
654 first_aabb = true;
655
656 _update_scene();
657
658 base_viewport->add_child(scene);
659
660 inspector->edit(nullptr);
661
662 if (first_aabb) {
663 contents_aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
664 first_aabb = false;
665 }
666
667 popup_centered_ratio();
668 _update_view_gizmos();
669 _update_camera();
670
671 // Start with the root item (Scene) selected.
672 scene_tree->get_root()->select(0);
673
674 if (p_for_animation) {
675 set_title(vformat(TTR("Advanced Import Settings for AnimationLibrary '%s'"), base_path.get_file()));
676 } else {
677 set_title(vformat(TTR("Advanced Import Settings for Scene '%s'"), base_path.get_file()));
678 }
679}
680
681SceneImportSettings *SceneImportSettings::singleton = nullptr;
682
683SceneImportSettings *SceneImportSettings::get_singleton() {
684 return singleton;
685}
686
687Node *SceneImportSettings::get_selected_node() {
688 if (selected_id == "") {
689 return nullptr;
690 }
691 return node_map[selected_id].node;
692}
693
694void SceneImportSettings::_select(Tree *p_from, String p_type, String p_id) {
695 selecting = true;
696 scene_import_settings_data->hide_options = false;
697
698 if (p_type == "Node") {
699 node_selected->hide(); // Always hide just in case.
700 mesh_preview->hide();
701 _reset_animation();
702
703 if (Object::cast_to<Node3D>(scene)) {
704 Object::cast_to<Node3D>(scene)->show();
705 }
706 material_tree->deselect_all();
707 mesh_tree->deselect_all();
708 NodeData &nd = node_map[p_id];
709
710 MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(nd.node);
711 if (mi) {
712 Ref<Mesh> base_mesh = mi->get_mesh();
713 if (base_mesh.is_valid()) {
714 AABB aabb = base_mesh->get_aabb();
715 Transform3D aabb_xf;
716 aabb_xf.basis.scale(aabb.size);
717 aabb_xf.origin = aabb.position;
718
719 aabb_xf = mi->get_global_transform() * aabb_xf;
720 node_selected->set_transform(aabb_xf);
721 node_selected->show();
722 }
723 }
724
725 if (nd.node == scene) {
726 scene_import_settings_data->settings = &defaults;
727 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX;
728 } else {
729 scene_import_settings_data->settings = &nd.settings;
730 if (mi) {
731 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE;
732 scene_import_settings_data->hide_options = editing_animation;
733 } else if (Object::cast_to<AnimationPlayer>(nd.node)) {
734 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE;
735 } else if (Object::cast_to<Skeleton3D>(nd.node)) {
736 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE;
737 } else {
738 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_NODE;
739 scene_import_settings_data->hide_options = editing_animation;
740 }
741 }
742 } else if (p_type == "Animation") {
743 node_selected->hide(); // Always hide just in case.
744 mesh_preview->hide();
745 _reset_animation(p_id);
746
747 if (Object::cast_to<Node3D>(scene)) {
748 Object::cast_to<Node3D>(scene)->show();
749 }
750 material_tree->deselect_all();
751 mesh_tree->deselect_all();
752 AnimationData &ad = animation_map[p_id];
753
754 scene_import_settings_data->settings = &ad.settings;
755 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION;
756 } else if (p_type == "Mesh") {
757 node_selected->hide();
758 if (Object::cast_to<Node3D>(scene)) {
759 Object::cast_to<Node3D>(scene)->hide();
760 }
761
762 MeshData &md = mesh_map[p_id];
763 if (md.mesh_node != nullptr) {
764 if (p_from != mesh_tree) {
765 md.mesh_node->uncollapse_tree();
766 md.mesh_node->select(0);
767 mesh_tree->ensure_cursor_is_visible();
768 }
769 if (p_from != scene_tree) {
770 md.scene_node->uncollapse_tree();
771 md.scene_node->select(0);
772 scene_tree->ensure_cursor_is_visible();
773 }
774 }
775
776 mesh_preview->set_mesh(md.mesh);
777 mesh_preview->show();
778 _reset_animation();
779
780 material_tree->deselect_all();
781
782 scene_import_settings_data->settings = &md.settings;
783 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH;
784 } else if (p_type == "Material") {
785 node_selected->hide();
786 if (Object::cast_to<Node3D>(scene)) {
787 Object::cast_to<Node3D>(scene)->hide();
788 }
789
790 mesh_preview->show();
791 _reset_animation();
792
793 MaterialData &md = material_map[p_id];
794
795 material_preview->set_material(md.material);
796 mesh_preview->set_mesh(material_preview);
797
798 if (p_from != mesh_tree) {
799 md.mesh_node->uncollapse_tree();
800 md.mesh_node->select(0);
801 mesh_tree->ensure_cursor_is_visible();
802 }
803 if (p_from != scene_tree) {
804 md.scene_node->uncollapse_tree();
805 md.scene_node->select(0);
806 scene_tree->ensure_cursor_is_visible();
807 }
808 if (p_from != material_tree) {
809 md.material_node->uncollapse_tree();
810 md.material_node->select(0);
811 material_tree->ensure_cursor_is_visible();
812 }
813
814 scene_import_settings_data->settings = &md.settings;
815 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MATERIAL;
816 }
817
818 selected_type = p_type;
819 selected_id = p_id;
820
821 selecting = false;
822
823 _update_camera();
824
825 List<ResourceImporter::ImportOption> options;
826
827 if (editing_animation) {
828 if (scene_import_settings_data->category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
829 ResourceImporterScene::get_animation_singleton()->get_import_options(base_path, &options);
830 } else {
831 ResourceImporterScene::get_animation_singleton()->get_internal_import_options(scene_import_settings_data->category, &options);
832 }
833
834 } else {
835 if (scene_import_settings_data->category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
836 ResourceImporterScene::get_scene_singleton()->get_import_options(base_path, &options);
837 } else {
838 ResourceImporterScene::get_scene_singleton()->get_internal_import_options(scene_import_settings_data->category, &options);
839 }
840 }
841
842 scene_import_settings_data->defaults.clear();
843 scene_import_settings_data->current.clear();
844
845 if (scene_import_settings_data->settings) {
846 for (const ResourceImporter::ImportOption &E : options) {
847 scene_import_settings_data->defaults[E.option.name] = E.default_value;
848 // Needed for visibility toggling (fails if something is missing).
849 if (scene_import_settings_data->settings->has(E.option.name)) {
850 scene_import_settings_data->current[E.option.name] = (*scene_import_settings_data->settings)[E.option.name];
851 } else {
852 scene_import_settings_data->current[E.option.name] = E.default_value;
853 }
854 }
855 }
856
857 scene_import_settings_data->options = options;
858 inspector->edit(scene_import_settings_data);
859 scene_import_settings_data->notify_property_list_changed();
860}
861
862void SceneImportSettings::_inspector_property_edited(const String &p_name) {
863 if (p_name == "settings/loop_mode") {
864 if (!animation_map.has(selected_id)) {
865 return;
866 }
867 HashMap<StringName, Variant> settings = animation_map[selected_id].settings;
868 if (settings.has(p_name)) {
869 animation_loop_mode = static_cast<Animation::LoopMode>((int)settings[p_name]);
870 } else {
871 animation_loop_mode = Animation::LoopMode::LOOP_NONE;
872 }
873 }
874}
875
876void SceneImportSettings::_reset_bone_transforms() {
877 for (Skeleton3D *skeleton : skeletons) {
878 skeleton->reset_bone_poses();
879 }
880}
881
882void SceneImportSettings::_play_animation() {
883 if (animation_player == nullptr) {
884 return;
885 }
886 StringName id = StringName(selected_id);
887 if (animation_player->has_animation(id)) {
888 if (animation_player->is_playing()) {
889 animation_player->pause();
890 animation_play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay")));
891 set_process(false);
892 } else {
893 animation_player->play(id);
894 animation_play_button->set_icon(get_editor_theme_icon(SNAME("Pause")));
895 set_process(true);
896 }
897 }
898}
899
900void SceneImportSettings::_stop_current_animation() {
901 animation_pingpong = false;
902 animation_player->stop();
903 animation_play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay")));
904 animation_slider->set_value_no_signal(0.0);
905 set_process(false);
906}
907
908void SceneImportSettings::_reset_animation(const String &p_animation_name) {
909 if (p_animation_name.is_empty()) {
910 animation_preview->hide();
911
912 if (animation_player != nullptr && animation_player->is_playing()) {
913 animation_player->stop();
914 }
915 animation_play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay")));
916
917 _reset_bone_transforms();
918 set_process(false);
919 } else {
920 _reset_bone_transforms();
921 animation_preview->show();
922
923 animation_loop_mode = Animation::LoopMode::LOOP_NONE;
924 animation_pingpong = false;
925
926 if (animation_map.has(p_animation_name)) {
927 HashMap<StringName, Variant> settings = animation_map[p_animation_name].settings;
928 if (settings.has("settings/loop_mode")) {
929 animation_loop_mode = static_cast<Animation::LoopMode>((int)settings["settings/loop_mode"]);
930 }
931 }
932
933 if (animation_player->is_playing() && animation_loop_mode != Animation::LoopMode::LOOP_NONE) {
934 animation_player->play(p_animation_name);
935 } else {
936 animation_player->stop(true);
937 animation_play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay")));
938 animation_player->set_assigned_animation(p_animation_name);
939 animation_player->seek(0.0, true);
940 animation_slider->set_value_no_signal(0.0);
941 set_process(false);
942 }
943 }
944}
945
946void SceneImportSettings::_animation_slider_value_changed(double p_value) {
947 if (animation_player == nullptr || !animation_map.has(selected_id) || animation_map[selected_id].animation.is_null()) {
948 return;
949 }
950 if (animation_player->is_playing()) {
951 animation_player->stop();
952 animation_play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay")));
953 set_process(false);
954 }
955 animation_player->seek(p_value * animation_map[selected_id].animation->get_length(), true);
956}
957
958void SceneImportSettings::_animation_finished(const StringName &p_name) {
959 Animation::LoopMode loop_mode = animation_loop_mode;
960
961 switch (loop_mode) {
962 case Animation::LOOP_NONE: {
963 animation_play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay")));
964 animation_slider->set_value_no_signal(1.0);
965 set_process(false);
966 } break;
967 case Animation::LOOP_LINEAR: {
968 animation_player->play(p_name);
969 } break;
970 case Animation::LOOP_PINGPONG: {
971 if (animation_pingpong) {
972 animation_player->play(p_name);
973 } else {
974 animation_player->play_backwards(p_name);
975 }
976 animation_pingpong = !animation_pingpong;
977 } break;
978 default: {
979 } break;
980 }
981}
982
983void SceneImportSettings::_material_tree_selected() {
984 if (selecting) {
985 return;
986 }
987 TreeItem *item = material_tree->get_selected();
988 String type = item->get_meta("type");
989 String import_id = item->get_meta("import_id");
990
991 _select(material_tree, type, import_id);
992}
993
994void SceneImportSettings::_mesh_tree_selected() {
995 if (selecting) {
996 return;
997 }
998
999 TreeItem *item = mesh_tree->get_selected();
1000 String type = item->get_meta("type");
1001 String import_id = item->get_meta("import_id");
1002
1003 _select(mesh_tree, type, import_id);
1004}
1005
1006void SceneImportSettings::_scene_tree_selected() {
1007 if (selecting) {
1008 return;
1009 }
1010 TreeItem *item = scene_tree->get_selected();
1011 String type = item->get_meta("type");
1012 String import_id = item->get_meta("import_id");
1013
1014 _select(scene_tree, type, import_id);
1015}
1016
1017void SceneImportSettings::_cleanup() {
1018 skeletons.clear();
1019 if (animation_player != nullptr) {
1020 animation_player->disconnect(SNAME("animation_finished"), callable_mp(this, &SceneImportSettings::_animation_finished));
1021 animation_player = nullptr;
1022 }
1023 set_process(false);
1024}
1025
1026void SceneImportSettings::_viewport_input(const Ref<InputEvent> &p_input) {
1027 float *rot_x = &cam_rot_x;
1028 float *rot_y = &cam_rot_y;
1029 float *zoom = &cam_zoom;
1030
1031 if (selected_type == "Mesh" && mesh_map.has(selected_id)) {
1032 MeshData &md = mesh_map[selected_id];
1033 rot_x = &md.cam_rot_x;
1034 rot_y = &md.cam_rot_y;
1035 zoom = &md.cam_zoom;
1036 } else if (selected_type == "Material" && material_map.has(selected_id)) {
1037 MaterialData &md = material_map[selected_id];
1038 rot_x = &md.cam_rot_x;
1039 rot_y = &md.cam_rot_y;
1040 zoom = &md.cam_zoom;
1041 }
1042 Ref<InputEventMouseMotion> mm = p_input;
1043 if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
1044 (*rot_x) -= mm->get_relative().y * 0.01 * EDSCALE;
1045 (*rot_y) -= mm->get_relative().x * 0.01 * EDSCALE;
1046 (*rot_x) = CLAMP((*rot_x), -Math_PI / 2, Math_PI / 2);
1047 _update_camera();
1048 }
1049 Ref<InputEventMouseButton> mb = p_input;
1050 if (mb.is_valid() && mb->get_button_index() == MouseButton::WHEEL_DOWN) {
1051 (*zoom) *= 1.1;
1052 if ((*zoom) > 10.0) {
1053 (*zoom) = 10.0;
1054 }
1055 _update_camera();
1056 }
1057 if (mb.is_valid() && mb->get_button_index() == MouseButton::WHEEL_UP) {
1058 (*zoom) /= 1.1;
1059 if ((*zoom) < 0.1) {
1060 (*zoom) = 0.1;
1061 }
1062 _update_camera();
1063 }
1064}
1065
1066void SceneImportSettings::_re_import() {
1067 HashMap<StringName, Variant> main_settings;
1068
1069 main_settings = scene_import_settings_data->current;
1070 main_settings.erase("_subresources");
1071 Dictionary nodes;
1072 Dictionary materials;
1073 Dictionary meshes;
1074 Dictionary animations;
1075
1076 Dictionary subresources;
1077
1078 for (KeyValue<String, NodeData> &E : node_map) {
1079 if (E.value.settings.size()) {
1080 Dictionary d;
1081 for (const KeyValue<StringName, Variant> &F : E.value.settings) {
1082 d[String(F.key)] = F.value;
1083 }
1084 nodes[E.key] = d;
1085 }
1086 }
1087 if (nodes.size()) {
1088 subresources["nodes"] = nodes;
1089 }
1090
1091 for (KeyValue<String, MaterialData> &E : material_map) {
1092 if (E.value.settings.size()) {
1093 Dictionary d;
1094 for (const KeyValue<StringName, Variant> &F : E.value.settings) {
1095 d[String(F.key)] = F.value;
1096 }
1097 materials[E.key] = d;
1098 }
1099 }
1100 if (materials.size()) {
1101 subresources["materials"] = materials;
1102 }
1103
1104 for (KeyValue<String, MeshData> &E : mesh_map) {
1105 if (E.value.settings.size()) {
1106 Dictionary d;
1107 for (const KeyValue<StringName, Variant> &F : E.value.settings) {
1108 d[String(F.key)] = F.value;
1109 }
1110 meshes[E.key] = d;
1111 }
1112 }
1113 if (meshes.size()) {
1114 subresources["meshes"] = meshes;
1115 }
1116
1117 for (KeyValue<String, AnimationData> &E : animation_map) {
1118 if (E.value.settings.size()) {
1119 Dictionary d;
1120 for (const KeyValue<StringName, Variant> &F : E.value.settings) {
1121 d[String(F.key)] = F.value;
1122 }
1123 animations[E.key] = d;
1124 }
1125 }
1126 if (animations.size()) {
1127 subresources["animations"] = animations;
1128 }
1129
1130 if (subresources.size()) {
1131 main_settings["_subresources"] = subresources;
1132 }
1133
1134 EditorFileSystem::get_singleton()->reimport_file_with_custom_parameters(base_path, editing_animation ? "animation_library" : "scene", main_settings);
1135}
1136
1137void SceneImportSettings::_notification(int p_what) {
1138 switch (p_what) {
1139 case NOTIFICATION_READY: {
1140 connect("confirmed", callable_mp(this, &SceneImportSettings::_re_import));
1141 } break;
1142
1143 case NOTIFICATION_THEME_CHANGED: {
1144 action_menu->add_theme_style_override("normal", get_theme_stylebox("normal", "Button"));
1145 action_menu->add_theme_style_override("hover", get_theme_stylebox("hover", "Button"));
1146 action_menu->add_theme_style_override("pressed", get_theme_stylebox("pressed", "Button"));
1147
1148 if (animation_player != nullptr && animation_player->is_playing()) {
1149 animation_play_button->set_icon(get_editor_theme_icon(SNAME("Pause")));
1150 } else {
1151 animation_play_button->set_icon(get_editor_theme_icon(SNAME("MainPlay")));
1152 }
1153 animation_stop_button->set_icon(get_editor_theme_icon(SNAME("Stop")));
1154 } break;
1155
1156 case NOTIFICATION_PROCESS: {
1157 if (animation_player != nullptr) {
1158 animation_slider->set_value_no_signal(animation_player->get_current_animation_position() / animation_player->get_current_animation_length());
1159 }
1160 } break;
1161
1162 case NOTIFICATION_VISIBILITY_CHANGED: {
1163 if (!is_visible()) {
1164 _cleanup();
1165 }
1166 } break;
1167 }
1168}
1169
1170void SceneImportSettings::_menu_callback(int p_id) {
1171 switch (p_id) {
1172 case ACTION_EXTRACT_MATERIALS: {
1173 save_path->set_title(TTR("Select folder to extract material resources"));
1174 external_extension_type->select(0);
1175 } break;
1176 case ACTION_CHOOSE_MESH_SAVE_PATHS: {
1177 save_path->set_title(TTR("Select folder where mesh resources will save on import"));
1178 external_extension_type->select(1);
1179 } break;
1180 case ACTION_CHOOSE_ANIMATION_SAVE_PATHS: {
1181 save_path->set_title(TTR("Select folder where animations will save on import"));
1182 external_extension_type->select(1);
1183 } break;
1184 }
1185
1186 save_path->set_current_dir(base_path.get_base_dir());
1187 current_action = p_id;
1188 save_path->popup_centered_ratio();
1189}
1190
1191void SceneImportSettings::_save_path_changed(const String &p_path) {
1192 save_path_item->set_text(1, p_path);
1193
1194 if (FileAccess::exists(p_path)) {
1195 save_path_item->set_text(2, TTR("Warning: File exists"));
1196 save_path_item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced."));
1197 save_path_item->set_icon(2, get_editor_theme_icon(SNAME("StatusWarning")));
1198
1199 } else {
1200 save_path_item->set_text(2, TTR("Will create new file"));
1201 save_path_item->set_icon(2, get_editor_theme_icon(SNAME("StatusSuccess")));
1202 }
1203}
1204
1205void SceneImportSettings::_browse_save_callback(Object *p_item, int p_column, int p_id, MouseButton p_button) {
1206 if (p_button != MouseButton::LEFT) {
1207 return;
1208 }
1209
1210 TreeItem *item = Object::cast_to<TreeItem>(p_item);
1211
1212 String path = item->get_text(1);
1213
1214 item_save_path->set_current_file(path);
1215 save_path_item = item;
1216
1217 item_save_path->popup_centered_ratio();
1218}
1219
1220void SceneImportSettings::_save_dir_callback(const String &p_path) {
1221 external_path_tree->clear();
1222 TreeItem *root = external_path_tree->create_item();
1223 save_path_items.clear();
1224
1225 switch (current_action) {
1226 case ACTION_EXTRACT_MATERIALS: {
1227 for (const KeyValue<String, MaterialData> &E : material_map) {
1228 MaterialData &md = material_map[E.key];
1229
1230 TreeItem *item = external_path_tree->create_item(root);
1231
1232 String name = md.material_node->get_text(0);
1233
1234 item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
1235 item->set_icon(0, get_editor_theme_icon(SNAME("StandardMaterial3D")));
1236 item->set_text(0, name);
1237
1238 if (md.has_import_id) {
1239 if (md.settings.has("use_external/enabled") && bool(md.settings["use_external/enabled"])) {
1240 item->set_text(2, TTR("Already External"));
1241 item->set_tooltip_text(2, TTR("This material already references an external file, no action will be taken.\nDisable the external property for it to be extracted again."));
1242 } else {
1243 item->set_metadata(0, E.key);
1244 item->set_editable(0, true);
1245 item->set_checked(0, true);
1246 String path = p_path.path_join(name);
1247 if (external_extension_type->get_selected() == 0) {
1248 path += ".tres";
1249 } else {
1250 path += ".res";
1251 }
1252
1253 item->set_text(1, path);
1254 if (FileAccess::exists(path)) {
1255 item->set_text(2, TTR("Warning: File exists"));
1256 item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced."));
1257 item->set_icon(2, get_editor_theme_icon(SNAME("StatusWarning")));
1258
1259 } else {
1260 item->set_text(2, TTR("Will create new file"));
1261 item->set_icon(2, get_editor_theme_icon(SNAME("StatusSuccess")));
1262 }
1263
1264 item->add_button(1, get_editor_theme_icon(SNAME("Folder")));
1265 }
1266
1267 } else {
1268 item->set_text(2, TTR("No import ID"));
1269 item->set_tooltip_text(2, TTR("Material has no name nor any other way to identify on re-import.\nPlease name it or ensure it is exported with an unique ID."));
1270 item->set_icon(2, get_editor_theme_icon(SNAME("StatusError")));
1271 }
1272
1273 save_path_items.push_back(item);
1274 }
1275
1276 external_paths->set_title(TTR("Extract Materials to Resource Files"));
1277 external_paths->set_ok_button_text(TTR("Extract"));
1278 } break;
1279 case ACTION_CHOOSE_MESH_SAVE_PATHS: {
1280 for (const KeyValue<String, MeshData> &E : mesh_map) {
1281 MeshData &md = mesh_map[E.key];
1282
1283 TreeItem *item = external_path_tree->create_item(root);
1284
1285 String name = md.mesh_node->get_text(0);
1286
1287 item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
1288 item->set_icon(0, get_editor_theme_icon(SNAME("Mesh")));
1289 item->set_text(0, name);
1290
1291 if (md.has_import_id) {
1292 if (md.settings.has("save_to_file/enabled") && bool(md.settings["save_to_file/enabled"])) {
1293 item->set_text(2, TTR("Already Saving"));
1294 item->set_tooltip_text(2, TTR("This mesh already saves to an external resource, no action will be taken."));
1295 } else {
1296 item->set_metadata(0, E.key);
1297 item->set_editable(0, true);
1298 item->set_checked(0, true);
1299 String path = p_path.path_join(name);
1300 if (external_extension_type->get_selected() == 0) {
1301 path += ".tres";
1302 } else {
1303 path += ".res";
1304 }
1305
1306 item->set_text(1, path);
1307 if (FileAccess::exists(path)) {
1308 item->set_text(2, TTR("Warning: File exists"));
1309 item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced on import."));
1310 item->set_icon(2, get_editor_theme_icon(SNAME("StatusWarning")));
1311
1312 } else {
1313 item->set_text(2, TTR("Will save to new file"));
1314 item->set_icon(2, get_editor_theme_icon(SNAME("StatusSuccess")));
1315 }
1316
1317 item->add_button(1, get_editor_theme_icon(SNAME("Folder")));
1318 }
1319
1320 } else {
1321 item->set_text(2, TTR("No import ID"));
1322 item->set_tooltip_text(2, TTR("Mesh has no name nor any other way to identify on re-import.\nPlease name it or ensure it is exported with an unique ID."));
1323 item->set_icon(2, get_editor_theme_icon(SNAME("StatusError")));
1324 }
1325
1326 save_path_items.push_back(item);
1327 }
1328
1329 external_paths->set_title(TTR("Set paths to save meshes as resource files on Reimport"));
1330 external_paths->set_ok_button_text(TTR("Set Paths"));
1331 } break;
1332 case ACTION_CHOOSE_ANIMATION_SAVE_PATHS: {
1333 for (const KeyValue<String, AnimationData> &E : animation_map) {
1334 AnimationData &ad = animation_map[E.key];
1335
1336 TreeItem *item = external_path_tree->create_item(root);
1337
1338 String name = ad.scene_node->get_text(0);
1339
1340 item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
1341 item->set_icon(0, get_editor_theme_icon(SNAME("Animation")));
1342 item->set_text(0, name);
1343
1344 if (ad.settings.has("save_to_file/enabled") && bool(ad.settings["save_to_file/enabled"])) {
1345 item->set_text(2, TTR("Already Saving"));
1346 item->set_tooltip_text(2, TTR("This animation already saves to an external resource, no action will be taken."));
1347 } else {
1348 item->set_metadata(0, E.key);
1349 item->set_editable(0, true);
1350 item->set_checked(0, true);
1351 String path = p_path.path_join(name);
1352 if (external_extension_type->get_selected() == 0) {
1353 path += ".tres";
1354 } else {
1355 path += ".res";
1356 }
1357
1358 item->set_text(1, path);
1359 if (FileAccess::exists(path)) {
1360 item->set_text(2, TTR("Warning: File exists"));
1361 item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced on import."));
1362 item->set_icon(2, get_editor_theme_icon(SNAME("StatusWarning")));
1363
1364 } else {
1365 item->set_text(2, TTR("Will save to new file"));
1366 item->set_icon(2, get_editor_theme_icon(SNAME("StatusSuccess")));
1367 }
1368
1369 item->add_button(1, get_editor_theme_icon(SNAME("Folder")));
1370 }
1371
1372 save_path_items.push_back(item);
1373 }
1374
1375 external_paths->set_title(TTR("Set paths to save animations as resource files on Reimport"));
1376 external_paths->set_ok_button_text(TTR("Set Paths"));
1377
1378 } break;
1379 }
1380
1381 external_paths->popup_centered_ratio();
1382}
1383
1384void SceneImportSettings::_save_dir_confirm() {
1385 for (int i = 0; i < save_path_items.size(); i++) {
1386 TreeItem *item = save_path_items[i];
1387 if (!item->is_checked(0)) {
1388 continue; //ignore
1389 }
1390 String path = item->get_text(1);
1391 if (!path.is_resource_file()) {
1392 continue;
1393 }
1394
1395 String id = item->get_metadata(0);
1396
1397 switch (current_action) {
1398 case ACTION_EXTRACT_MATERIALS: {
1399 ERR_CONTINUE(!material_map.has(id));
1400 MaterialData &md = material_map[id];
1401
1402 Error err = ResourceSaver::save(md.material, path);
1403 if (err != OK) {
1404 EditorNode::get_singleton()->add_io_error(TTR("Can't make material external to file, write error:") + "\n\t" + path);
1405 continue;
1406 }
1407
1408 md.settings["use_external/enabled"] = true;
1409 md.settings["use_external/path"] = path;
1410
1411 } break;
1412 case ACTION_CHOOSE_MESH_SAVE_PATHS: {
1413 ERR_CONTINUE(!mesh_map.has(id));
1414 MeshData &md = mesh_map[id];
1415
1416 md.settings["save_to_file/enabled"] = true;
1417 md.settings["save_to_file/path"] = path;
1418 } break;
1419 case ACTION_CHOOSE_ANIMATION_SAVE_PATHS: {
1420 ERR_CONTINUE(!animation_map.has(id));
1421 AnimationData &ad = animation_map[id];
1422
1423 ad.settings["save_to_file/enabled"] = true;
1424 ad.settings["save_to_file/path"] = path;
1425
1426 } break;
1427 }
1428 }
1429
1430 if (current_action == ACTION_EXTRACT_MATERIALS) {
1431 //as this happens right now, the scene needs to be saved and reimported.
1432 _re_import();
1433 open_settings(base_path);
1434 } else {
1435 scene_import_settings_data->notify_property_list_changed();
1436 }
1437}
1438
1439SceneImportSettings::SceneImportSettings() {
1440 singleton = this;
1441
1442 VBoxContainer *main_vb = memnew(VBoxContainer);
1443 add_child(main_vb);
1444 HBoxContainer *menu_hb = memnew(HBoxContainer);
1445 main_vb->add_child(menu_hb);
1446
1447 action_menu = memnew(MenuButton);
1448 action_menu->set_text(TTR("Actions..."));
1449 // Style the MenuButton like a regular Button to make it more noticeable.
1450 action_menu->set_flat(false);
1451 action_menu->set_focus_mode(Control::FOCUS_ALL);
1452 menu_hb->add_child(action_menu);
1453
1454 action_menu->get_popup()->add_item(TTR("Extract Materials"), ACTION_EXTRACT_MATERIALS);
1455 action_menu->get_popup()->add_separator();
1456 action_menu->get_popup()->add_item(TTR("Set Animation Save Paths"), ACTION_CHOOSE_ANIMATION_SAVE_PATHS);
1457 action_menu->get_popup()->add_item(TTR("Set Mesh Save Paths"), ACTION_CHOOSE_MESH_SAVE_PATHS);
1458
1459 action_menu->get_popup()->connect("id_pressed", callable_mp(this, &SceneImportSettings::_menu_callback));
1460
1461 tree_split = memnew(HSplitContainer);
1462 main_vb->add_child(tree_split);
1463 tree_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1464
1465 data_mode = memnew(TabContainer);
1466 tree_split->add_child(data_mode);
1467 data_mode->set_custom_minimum_size(Size2(300 * EDSCALE, 0));
1468 data_mode->set_theme_type_variation("TabContainerOdd");
1469
1470 property_split = memnew(HSplitContainer);
1471 tree_split->add_child(property_split);
1472 property_split->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1473
1474 scene_tree = memnew(Tree);
1475 scene_tree->set_name(TTR("Scene"));
1476 data_mode->add_child(scene_tree);
1477 scene_tree->connect("cell_selected", callable_mp(this, &SceneImportSettings::_scene_tree_selected));
1478
1479 mesh_tree = memnew(Tree);
1480 mesh_tree->set_name(TTR("Meshes"));
1481 data_mode->add_child(mesh_tree);
1482 mesh_tree->set_hide_root(true);
1483 mesh_tree->connect("cell_selected", callable_mp(this, &SceneImportSettings::_mesh_tree_selected));
1484
1485 material_tree = memnew(Tree);
1486 material_tree->set_name(TTR("Materials"));
1487 data_mode->add_child(material_tree);
1488 material_tree->connect("cell_selected", callable_mp(this, &SceneImportSettings::_material_tree_selected));
1489
1490 material_tree->set_hide_root(true);
1491
1492 VBoxContainer *vp_vb = memnew(VBoxContainer);
1493 vp_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1494 vp_vb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1495 vp_vb->set_anchors_and_offsets_preset(Control::LayoutPreset::PRESET_FULL_RECT);
1496 property_split->add_child(vp_vb);
1497
1498 SubViewportContainer *vp_container = memnew(SubViewportContainer);
1499 vp_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1500 vp_container->set_custom_minimum_size(Size2(10, 10));
1501 vp_container->set_stretch(true);
1502 vp_container->connect("gui_input", callable_mp(this, &SceneImportSettings::_viewport_input));
1503 vp_vb->add_child(vp_container);
1504
1505 base_viewport = memnew(SubViewport);
1506 vp_container->add_child(base_viewport);
1507
1508 animation_preview = memnew(PanelContainer);
1509 animation_preview->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1510 vp_vb->add_child(animation_preview);
1511 animation_preview->hide();
1512
1513 HBoxContainer *animation_hbox = memnew(HBoxContainer);
1514 animation_preview->add_child(animation_hbox);
1515
1516 animation_play_button = memnew(Button);
1517 animation_hbox->add_child(animation_play_button);
1518 animation_play_button->set_flat(true);
1519 animation_play_button->set_focus_mode(Control::FOCUS_NONE);
1520 animation_play_button->set_shortcut(ED_SHORTCUT("scene_import_settings/play_selected_animation", TTR("Selected Animation Play/Pause"), Key::SPACE));
1521 animation_play_button->connect(SNAME("pressed"), callable_mp(this, &SceneImportSettings::_play_animation));
1522
1523 animation_stop_button = memnew(Button);
1524 animation_hbox->add_child(animation_stop_button);
1525 animation_stop_button->set_flat(true);
1526 animation_stop_button->set_focus_mode(Control::FOCUS_NONE);
1527 animation_stop_button->connect(SNAME("pressed"), callable_mp(this, &SceneImportSettings::_stop_current_animation));
1528
1529 animation_slider = memnew(HSlider);
1530 animation_hbox->add_child(animation_slider);
1531 animation_slider->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1532 animation_slider->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1533 animation_slider->set_max(1.0);
1534 animation_slider->set_step(1.0 / 100.0);
1535 animation_slider->set_value_no_signal(0.0);
1536 animation_slider->set_focus_mode(Control::FOCUS_NONE);
1537 animation_slider->connect(SNAME("value_changed"), callable_mp(this, &SceneImportSettings::_animation_slider_value_changed));
1538
1539 base_viewport->set_use_own_world_3d(true);
1540
1541 camera = memnew(Camera3D);
1542 base_viewport->add_child(camera);
1543 camera->make_current();
1544
1545 if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
1546 camera_attributes.instantiate();
1547 camera->set_attributes(camera_attributes);
1548 }
1549
1550 light = memnew(DirectionalLight3D);
1551 light->set_transform(Transform3D().looking_at(Vector3(-1, -2, -0.6), Vector3(0, 1, 0)));
1552 base_viewport->add_child(light);
1553 light->set_shadow(true);
1554
1555 {
1556 Ref<StandardMaterial3D> selection_mat;
1557 selection_mat.instantiate();
1558 selection_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
1559 selection_mat->set_albedo(Color(1, 0.8, 1.0));
1560
1561 Ref<SurfaceTool> st;
1562 st.instantiate();
1563 st->begin(Mesh::PRIMITIVE_LINES);
1564
1565 AABB base_aabb;
1566 base_aabb.size = Vector3(1, 1, 1);
1567
1568 for (int i = 0; i < 12; i++) {
1569 Vector3 a, b;
1570 base_aabb.get_edge(i, a, b);
1571
1572 st->add_vertex(a);
1573 st->add_vertex(a.lerp(b, 0.2));
1574 st->add_vertex(b);
1575 st->add_vertex(b.lerp(a, 0.2));
1576 }
1577
1578 selection_mesh.instantiate();
1579 st->commit(selection_mesh);
1580 selection_mesh->surface_set_material(0, selection_mat);
1581
1582 node_selected = memnew(MeshInstance3D);
1583 node_selected->set_mesh(selection_mesh);
1584 base_viewport->add_child(node_selected);
1585 node_selected->hide();
1586 }
1587
1588 {
1589 mesh_preview = memnew(MeshInstance3D);
1590 base_viewport->add_child(mesh_preview);
1591 mesh_preview->hide();
1592
1593 material_preview.instantiate();
1594 }
1595
1596 {
1597 collider_mat.instantiate();
1598 collider_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
1599 collider_mat->set_albedo(Color(0.5, 0.5, 1.0));
1600 }
1601
1602 inspector = memnew(EditorInspector);
1603 inspector->set_custom_minimum_size(Size2(300 * EDSCALE, 0));
1604 inspector->connect(SNAME("property_edited"), callable_mp(this, &SceneImportSettings::_inspector_property_edited));
1605
1606 property_split->add_child(inspector);
1607
1608 scene_import_settings_data = memnew(SceneImportSettingsData);
1609
1610 set_ok_button_text(TTR("Reimport"));
1611 set_cancel_button_text(TTR("Close"));
1612
1613 external_paths = memnew(ConfirmationDialog);
1614 add_child(external_paths);
1615 external_path_tree = memnew(Tree);
1616 external_paths->add_child(external_path_tree);
1617 external_path_tree->connect("button_clicked", callable_mp(this, &SceneImportSettings::_browse_save_callback));
1618 external_paths->connect("confirmed", callable_mp(this, &SceneImportSettings::_save_dir_confirm));
1619 external_path_tree->set_columns(3);
1620 external_path_tree->set_column_titles_visible(true);
1621 external_path_tree->set_column_expand(0, true);
1622 external_path_tree->set_column_custom_minimum_width(0, 100 * EDSCALE);
1623 external_path_tree->set_column_title(0, TTR("Resource"));
1624 external_path_tree->set_column_expand(1, true);
1625 external_path_tree->set_column_custom_minimum_width(1, 100 * EDSCALE);
1626 external_path_tree->set_column_title(1, TTR("Path"));
1627 external_path_tree->set_column_expand(2, false);
1628 external_path_tree->set_column_custom_minimum_width(2, 200 * EDSCALE);
1629 external_path_tree->set_column_title(2, TTR("Status"));
1630 save_path = memnew(EditorFileDialog);
1631 save_path->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR);
1632 HBoxContainer *extension_hb = memnew(HBoxContainer);
1633 save_path->get_vbox()->add_child(extension_hb);
1634 extension_hb->add_spacer();
1635 extension_hb->add_child(memnew(Label(TTR("Save Extension:"))));
1636 external_extension_type = memnew(OptionButton);
1637 extension_hb->add_child(external_extension_type);
1638 external_extension_type->add_item(TTR("Text: *.tres"));
1639 external_extension_type->add_item(TTR("Binary: *.res"));
1640 external_path_tree->set_hide_root(true);
1641 add_child(save_path);
1642
1643 item_save_path = memnew(EditorFileDialog);
1644 item_save_path->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
1645 item_save_path->add_filter("*.tres", TTR("Text Resource"));
1646 item_save_path->add_filter("*.res", TTR("Binary Resource"));
1647 add_child(item_save_path);
1648 item_save_path->connect("file_selected", callable_mp(this, &SceneImportSettings::_save_path_changed));
1649
1650 save_path->connect("dir_selected", callable_mp(this, &SceneImportSettings::_save_dir_callback));
1651
1652 update_view_timer = memnew(Timer);
1653 update_view_timer->set_wait_time(0.2);
1654 update_view_timer->connect("timeout", callable_mp(this, &SceneImportSettings::_update_view_gizmos));
1655 add_child(update_view_timer);
1656}
1657
1658SceneImportSettings::~SceneImportSettings() {
1659 memdelete(scene_import_settings_data);
1660}
1661