Debian Codegraph @

editor/import/3d/scene_import_settings.cpp

raw symbols SCIP
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_node.h"
35 #include "editor/editor_string_names.h"
36 #include "editor/file_system/editor_file_system.h"
37 #include "editor/gui/editor_file_dialog.h"
38 #include "editor/inspector/editor_inspector.h"
39 #include "editor/scene/3d/skeleton_3d_editor_plugin.h"
40 #include "editor/settings/editor_settings.h"
41 #include "editor/themes/editor_scale.h"
42 #include "scene/3d/importer_mesh_instance_3d.h"
43 #include "scene/3d/multimesh_instance_3d.h"
44 #include "scene/animation/animation_player.h"
45 #include "scene/gui/subviewport_container.h"
46 #include "scene/main/timer.h"
47 #include "scene/resources/3d/importer_mesh.h"
48 #include "scene/resources/surface_tool.h"
49
50 class SceneImportSettingsData : public Object {
51 GDCLASS(SceneImportSettingsData, Object)
52 friend class SceneImportSettingsDialog;
53 HashMap<StringName, Variant> *settings = nullptr;
54 HashMap<StringName, Variant> current;
55 HashMap<StringName, Variant> defaults;
56 List<ResourceImporter::ImportOption> options;
57 Vector<String> animation_list;
58
59 bool hide_options = false;
60 String path;
61
62 ResourceImporterScene::InternalImportCategory category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX;
63
64 bool _set(const StringName &p_name, const Variant &p_value) {
65 if (settings) {
66 if (defaults.has(p_name) && defaults[p_name] == p_value) {
67 settings->erase(p_name);
68 } else {
69 (*settings)[p_name] = p_value;
70 }
71
72 current[p_name] = p_value;
73
74 // SceneImportSettings must decide if a new collider should be generated or not.
75 if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE) {
76 SceneImportSettingsDialog::get_singleton()->request_generate_collider();
77 }
78
79 ResourceImporterScene *resource_importer_scene = SceneImportSettingsDialog::get_singleton()->get_resource_importer_scene();
80 if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
81 if (resource_importer_scene->get_option_visibility(path, p_name, current)) {
82 SceneImportSettingsDialog::get_singleton()->update_view();
83 }
84 } else {
85 if (resource_importer_scene->get_internal_option_update_view_required(category, p_name, current)) {
86 SceneImportSettingsDialog::get_singleton()->update_view();
87 }
88 }
89
90 return true;
91 }
92 return false;
93 }
94
95 bool _get(const StringName &p_name, Variant &r_ret) const {
96 if (settings) {
97 if (settings->has(p_name)) {
98 r_ret = (*settings)[p_name];
99 return true;
100 }
101 }
102 if (defaults.has(p_name)) {
103 r_ret = defaults[p_name];
104 return true;
105 }
106 return false;
107 }
108
109 void handle_special_properties(PropertyInfo &r_option) const {
110 ERR_FAIL_NULL(settings);
111 if (r_option.name == "rest_pose/load_pose") {
112 if (!settings->has("rest_pose/load_pose") || int((*settings)["rest_pose/load_pose"]) != 2) {
113 if (settings->has("rest_pose/external_animation_library")) {
114 (*settings)["rest_pose/external_animation_library"] = Variant();
115 }
116 }
117 }
118 if (r_option.name == "rest_pose/selected_animation") {
119 if (!settings->has("rest_pose/load_pose")) {
120 return;
121 }
122 String hint_string;
123
124 switch (int((*settings)["rest_pose/load_pose"])) {
125 case 1: {
126 hint_string = String(",").join(animation_list);
127 if (animation_list.size() == 1) {
128 (*settings)["rest_pose/selected_animation"] = animation_list[0];
129 }
130 } break;
131 case 2: {
132 Object *res = nullptr;
133 if (settings->has("rest_pose/external_animation_library")) {
134 res = (*settings)["rest_pose/external_animation_library"];
135 }
136 Ref<Animation> anim(res);
137 Ref<AnimationLibrary> library(res);
138 if (anim.is_valid()) {
139 hint_string = anim->get_name();
140 }
141 if (library.is_valid()) {
142 List<StringName> anim_names;
143 library->get_animation_list(&anim_names);
144 if (anim_names.size() == 1) {
145 (*settings)["rest_pose/selected_animation"] = String(anim_names.front()->get());
146 }
147 for (StringName anim_name : anim_names) {
148 hint_string += "," + anim_name; // Include preceding, as a catch-all.
149 }
150 }
151 } break;
152 default:
153 break;
154 }
155 r_option.hint = PROPERTY_HINT_ENUM;
156 r_option.hint_string = hint_string;
157 }
158 }
159
160 void _get_property_list(List<PropertyInfo> *r_list) const {
161 if (hide_options) {
162 return;
163 }
164 ResourceImporterScene *resource_importer_scene = SceneImportSettingsDialog::get_singleton()->get_resource_importer_scene();
165 for (const ResourceImporter::ImportOption &E : options) {
166 PropertyInfo option = E.option;
167 if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
168 if (resource_importer_scene->get_option_visibility(path, E.option.name, current)) {
169 handle_special_properties(option);
170 r_list->push_back(option);
171 }
172 } else {
173 if (resource_importer_scene->get_internal_option_visibility(category, E.option.name, current)) {
174 handle_special_properties(option);
175 r_list->push_back(option);
176 }
177 }
178 }
179 }
180 };
181
182 bool SceneImportSettingsDialog::_get_current(const StringName &p_name, Variant &r_ret) const {
183 if (scene_import_settings_data->_get(p_name, r_ret)) {
184 return true;
185 }
186 if (defaults.has(p_name)) {
187 r_ret = defaults[p_name];
188 return true;
189 }
190 return false;
191 }
192
193 void SceneImportSettingsDialog::_set_default(const StringName &p_name, const Variant &p_value) {
194 defaults[p_name] = p_value;
195 scene_import_settings_data->defaults[p_name] = p_value;
196 scene_import_settings_data->_set(p_name, p_value);
197 }
198
199 void SceneImportSettingsDialog::_fill_material(Tree *p_tree, const Ref<Material> &p_material, TreeItem *p_parent) {
200 String import_id;
201 bool has_import_id = false;
202
203 if (p_material->has_meta("import_id")) {
204 import_id = p_material->get_meta("import_id");
205 has_import_id = true;
206 } else if (!p_material->get_name().is_empty()) {
207 import_id = p_material->get_name();
208 has_import_id = true;
209 } else if (unnamed_material_name_map.has(p_material)) {
210 import_id = unnamed_material_name_map[p_material];
211 } else {
212 import_id = "@MATERIAL:" + itos(material_map.size());
213 unnamed_material_name_map[p_material] = import_id;
214 }
215
216 bool created = false;
217 if (!material_map.has(import_id)) {
218 MaterialData md;
219 created = true;
220 md.has_import_id = has_import_id;
221 md.material = p_material;
222
223 _load_default_subresource_settings(md.settings, "materials", import_id, ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MATERIAL);
224
225 material_map[import_id] = md;
226 }
227
228 MaterialData &material_data = material_map[import_id];
229 ERR_FAIL_COND(p_material != material_data.material);
230
231 Variant value;
232 if (_get_current("materials/extract", value) && (int)value != 0) {
233 String spath = base_path.get_base_dir();
234 if (_get_current("materials/extract_path", value)) {
235 String extpath = value;
236 if (!extpath.is_empty()) {
237 spath = extpath;
238 }
239 }
240
241 String ext = ResourceImporterScene::material_extension[_get_current("materials/extract_format", value) ? (int)value : 0];
242 String path = spath.path_join(import_id.validate_filename() + ext);
243 String uid_path = ResourceUID::path_to_uid(path);
244 material_data.settings["use_external/enabled"] = true;
245 material_data.settings["use_external/path"] = uid_path;
246 material_data.settings["use_external/fallback_path"] = path;
247 }
248
249 Ref<Texture2D> icon = get_editor_theme_icon(SNAME("StandardMaterial3D"));
250
251 TreeItem *item = p_tree->create_item(p_parent);
252 if (p_material->get_name().is_empty()) {
253 item->set_text(0, TTR("<Unnamed Material>"));
254 } else {
255 item->set_text(0, p_material->get_name());
256 }
257 item->set_icon(0, icon);
258
259 item->set_meta("type", "Material");
260 item->set_meta("import_id", import_id);
261 item->set_tooltip_text(0, vformat(TTR("Import ID: %s"), import_id));
262 item->set_selectable(0, true);
263
264 if (p_tree == scene_tree) {
265 material_data.scene_node = item;
266 } else if (p_tree == mesh_tree) {
267 material_data.mesh_node = item;
268 } else {
269 material_data.material_node = item;
270 }
271
272 if (created) {
273 _fill_material(material_tree, p_material, material_tree->get_root());
274 }
275 }
276
277 void SceneImportSettingsDialog::_fill_mesh(Tree *p_tree, const Ref<Mesh> &p_mesh, TreeItem *p_parent) {
278 String import_id;
279
280 bool has_import_id = false;
281 if (p_mesh->has_meta("import_id")) {
282 import_id = p_mesh->get_meta("import_id");
283 has_import_id = true;
284 } else if (!p_mesh->get_name().is_empty()) {
285 import_id = p_mesh->get_name();
286 has_import_id = true;
287 } else {
288 import_id = "@MESH:" + itos(mesh_set.size());
289 }
290
291 if (!mesh_map.has(import_id)) {
292 MeshData md;
293 md.has_import_id = has_import_id;
294 md.mesh = p_mesh;
295
296 _load_default_subresource_settings(md.settings, "meshes", import_id, ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH);
297
298 mesh_map[import_id] = md;
299 }
300
301 MeshData &mesh_data = mesh_map[import_id];
302
303 Ref<Texture2D> icon = get_editor_theme_icon(SNAME("MeshItem"));
304
305 TreeItem *item = p_tree->create_item(p_parent);
306 item->set_text(0, p_mesh->get_name());
307 item->set_icon(0, icon);
308
309 bool created = false;
310 if (!mesh_set.has(p_mesh)) {
311 mesh_set.insert(p_mesh);
312 created = true;
313 }
314
315 item->set_meta("type", "Mesh");
316 item->set_meta("import_id", import_id);
317 item->set_tooltip_text(0, vformat(TTR("Import ID: %s"), import_id));
318
319 item->set_selectable(0, true);
320
321 if (p_tree == scene_tree) {
322 mesh_data.scene_node = item;
323 } else {
324 mesh_data.mesh_node = item;
325 }
326
327 item->set_collapsed(true);
328
329 for (int i = 0; i < p_mesh->get_surface_count(); i++) {
330 Ref<Material> mat = p_mesh->surface_get_material(i);
331 if (mat.is_valid()) {
332 _fill_material(p_tree, mat, item);
333 }
334 }
335
336 if (created) {
337 _fill_mesh(mesh_tree, p_mesh, mesh_tree->get_root());
338 }
339 }
340
341 void SceneImportSettingsDialog::_fill_animation(Tree *p_tree, const Ref<Animation> &p_anim, const String &p_name, TreeItem *p_parent) {
342 if (!animation_map.has(p_name)) {
343 AnimationData ad;
344 ad.animation = p_anim;
345
346 _load_default_subresource_settings(ad.settings, "animations", p_name, ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION);
347 Animation::LoopMode loop_mode = p_anim->get_loop_mode();
348 if (!ad.settings.has("settings/loop_mode") && loop_mode != Animation::LoopMode::LOOP_NONE) {
349 // Update the loop mode to match detected mode (from import hints).
350 // This is necessary on the first import of a scene, otherwise the
351 // default (0/NONE) is set when filling out defaults.
352 ad.settings["settings/loop_mode"] = loop_mode;
353 }
354
355 animation_map[p_name] = ad;
356 }
357
358 AnimationData &animation_data = animation_map[p_name];
359
360 Ref<Texture2D> icon = get_editor_theme_icon(SNAME("Animation"));
361
362 TreeItem *item = p_tree->create_item(p_parent);
363 item->set_text(0, p_name);
364 item->set_icon(0, icon);
365
366 item->set_meta("type", "Animation");
367 item->set_meta("import_id", p_name);
368
369 item->set_selectable(0, true);
370
371 animation_data.scene_node = item;
372 }
373
374 void SceneImportSettingsDialog::_fill_scene(Node *p_node, TreeItem *p_parent_item) {
375 String import_id;
376
377 if (p_node->has_meta("import_id")) {
378 import_id = p_node->get_meta("import_id");
379 } else {
380 import_id = "PATH:" + String(scene->get_path_to(p_node));
381 p_node->set_meta("import_id", import_id);
382 }
383
384 ImporterMeshInstance3D *src_mesh_node = Object::cast_to<ImporterMeshInstance3D>(p_node);
385
386 if (src_mesh_node) {
387 MeshInstance3D *mesh_node = memnew(MeshInstance3D);
388 mesh_node->set_name(src_mesh_node->get_name());
389 mesh_node->set_transform(src_mesh_node->get_transform());
390 mesh_node->set_skin(src_mesh_node->get_skin());
391 mesh_node->set_skeleton_path(src_mesh_node->get_skeleton_path());
392 mesh_node->set_visible(src_mesh_node->is_visible());
393 if (src_mesh_node->get_mesh().is_valid()) {
394 Ref<ImporterMesh> editor_mesh = src_mesh_node->get_mesh();
395 mesh_node->set_mesh(editor_mesh->get_mesh());
396 }
397 // Replace the original mesh node in the scene tree with the new one.
398 if (unlikely(p_node == scene)) {
399 scene = mesh_node;
400 }
401 p_node->replace_by(mesh_node);
402 memdelete(p_node);
403 p_node = mesh_node;
404 }
405
406 String type = p_node->get_class();
407
408 if (!has_theme_icon(type, EditorStringName(EditorIcons))) {
409 type = "Node3D";
410 }
411
412 Ref<Texture2D> icon = get_editor_theme_icon(type);
413
414 TreeItem *item = scene_tree->create_item(p_parent_item);
415 item->set_text(0, p_node->get_name());
416
417 if (p_node == scene) {
418 icon = get_editor_theme_icon(SNAME("PackedScene"));
419 item->set_text(0, TTR("Scene"));
420 }
421
422 item->set_icon(0, icon);
423
424 item->set_meta("type", "Node");
425 item->set_meta("class", type);
426 item->set_meta("import_id", import_id);
427 item->set_tooltip_text(0, vformat(TTR("Type: %s\nImport ID: %s"), type, import_id));
428
429 item->set_selectable(0, true);
430
431 if (!node_map.has(import_id)) {
432 NodeData nd;
433
434 if (p_node != scene) {
435 ResourceImporterScene::InternalImportCategory category;
436 if (src_mesh_node) {
437 category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE;
438 } else if (Object::cast_to<AnimationPlayer>(p_node)) {
439 category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE;
440
441 animation_player = Object::cast_to<AnimationPlayer>(p_node);
442 animation_player->connect(SceneStringName(animation_finished), callable_mp(this, &SceneImportSettingsDialog::_animation_finished));
443 } else if (Object::cast_to<Skeleton3D>(p_node)) {
444 category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE;
445 Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(p_node);
446 skeleton->connect(SceneStringName(tree_entered), callable_mp(this, &SceneImportSettingsDialog::_skeleton_tree_entered).bind(skeleton));
447 skeletons.push_back(skeleton);
448 } else {
449 category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_NODE;
450 }
451
452 _load_default_subresource_settings(nd.settings, "nodes", import_id, category);
453 }
454
455 node_map[import_id] = nd;
456 }
457 NodeData &node_data = node_map[import_id];
458
459 node_data.node = p_node;
460 node_data.scene_node = item;
461
462 AnimationPlayer *anim_node = Object::cast_to<AnimationPlayer>(p_node);
463 if (anim_node) {
464 Vector<String> animation_list;
465 List<StringName> animations;
466 anim_node->get_animation_list(&animations);
467 for (const StringName &E : animations) {
468 _fill_animation(scene_tree, anim_node->get_animation(E), E, item);
469 animation_list.append(E);
470 }
471 if (scene_import_settings_data != nullptr) {
472 scene_import_settings_data->animation_list = animation_list;
473 }
474 }
475
476 for (int i = 0; i < p_node->get_child_count(); i++) {
477 _fill_scene(p_node->get_child(i), item);
478 }
479 Transform3D accum_xform;
480 Node3D *base = Object::cast_to<Node3D>(p_node);
481 while (base) {
482 accum_xform = base->get_transform() * accum_xform;
483 base = Object::cast_to<Node3D>(base->get_parent());
484 }
485 MeshInstance3D *mesh_node = Object::cast_to<MeshInstance3D>(p_node);
486 if (mesh_node && mesh_node->get_mesh().is_valid()) {
487 // This controls the display of mesh resources in the import settings dialog tree (the white mesh icon).
488 // We want to show these icons for any import type that preserves meshes.
489 if (_resource_importer_scene->get_scene_import_type() != "AnimationLibrary") {
490 _fill_mesh(scene_tree, mesh_node->get_mesh(), item);
491 }
492
493 // Add the collider view.
494 MeshInstance3D *collider_view = memnew(MeshInstance3D);
495 collider_view->set_name("collider_view");
496 collider_view->set_visible(false);
497 mesh_node->add_child(collider_view, true);
498 collider_view->set_owner(mesh_node);
499
500 AABB aabb = accum_xform.xform(mesh_node->get_mesh()->get_aabb());
501
502 if (first_aabb) {
503 contents_aabb = aabb;
504 first_aabb = false;
505 } else {
506 contents_aabb.merge_with(aabb);
507 }
508 }
509 MultiMeshInstance3D *multi_mesh_node = Object::cast_to<MultiMeshInstance3D>(p_node);
510 if (multi_mesh_node && multi_mesh_node->get_multimesh().is_valid()) {
511 const Ref<MultiMesh> multi_mesh = multi_mesh_node->get_multimesh();
512 const Ref<Mesh> mm_mesh = multi_mesh->get_mesh();
513 if (mm_mesh.is_valid()) {
514 _fill_mesh(scene_tree, mm_mesh, item);
515 }
516 const AABB aabb = accum_xform.xform(multi_mesh->get_aabb());
517 if (first_aabb) {
518 contents_aabb = aabb;
519 first_aabb = false;
520 } else {
521 contents_aabb.merge_with(aabb);
522 }
523 }
524
525 Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(p_node);
526 if (skeleton) {
527 Ref<ArrayMesh> bones_mesh = Skeleton3DGizmoPlugin::get_bones_mesh(skeleton, -1, true);
528
529 bones_mesh_preview->set_mesh(bones_mesh);
530 bones_mesh_preview->set_transform(accum_xform * skeleton->get_transform());
531
532 AABB aabb = accum_xform.xform(bones_mesh->get_aabb());
533
534 if (first_aabb) {
535 contents_aabb = aabb;
536 first_aabb = false;
537 } else {
538 contents_aabb.merge_with(aabb);
539 }
540 }
541 }
542
543 void SceneImportSettingsDialog::_update_scene() {
544 scene_tree->clear();
545 material_tree->clear();
546 mesh_tree->clear();
547
548 // Hidden roots.
549 material_tree->create_item();
550 mesh_tree->create_item();
551
552 _fill_scene(scene, nullptr);
553 }
554
555 void SceneImportSettingsDialog::_update_view_gizmos() {
556 if (!is_visible()) {
557 return;
558 }
559 const HashMap<StringName, Variant> &main_settings = scene_import_settings_data->current;
560 bool reshow_settings = false;
561 if (main_settings.has("nodes/import_as_skeleton_bones")) {
562 bool new_import_as_skeleton = main_settings["nodes/import_as_skeleton_bones"];
563 reshow_settings = reshow_settings || (new_import_as_skeleton != previous_import_as_skeleton);
564 previous_import_as_skeleton = new_import_as_skeleton;
565 }
566 if (main_settings.has("animation/import_rest_as_RESET")) {
567 bool new_rest_as_reset = main_settings["animation/import_rest_as_RESET"];
568 reshow_settings = reshow_settings || (new_rest_as_reset != previous_rest_as_reset);
569 previous_rest_as_reset = new_rest_as_reset;
570 }
571 if (reshow_settings) {
572 _re_import();
573 open_settings(base_path);
574 return;
575 }
576 for (const KeyValue<String, NodeData> &e : node_map) {
577 // Skip import nodes that aren't MeshInstance3D.
578 const MeshInstance3D *mesh_node = Object::cast_to<MeshInstance3D>(e.value.node);
579 if (mesh_node == nullptr || mesh_node->get_mesh().is_null()) {
580 continue;
581 }
582
583 // Determine if the mesh collider should be visible.
584 bool show_collider_view = false;
585 if (e.value.settings.has(SNAME("generate/physics"))) {
586 show_collider_view = e.value.settings[SNAME("generate/physics")];
587 }
588
589 // Get the collider_view MeshInstance3D.
590 TypedArray<Node> descendants = mesh_node->find_children("collider_view", "MeshInstance3D");
591 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`.");
592 MeshInstance3D *collider_view = Object::cast_to<MeshInstance3D>(descendants[0].operator Object *());
593
594 // Regenerate the physics collider for this MeshInstance3D if either:
595 // - A regeneration is requested for the selected import node.
596 // - The collider is being made visible.
597 if ((generate_collider && e.key == selected_id) || (show_collider_view && !collider_view->is_visible())) {
598 // This collider_view doesn't have a mesh so we need to generate a new one.
599 Ref<ImporterMesh> mesh;
600 mesh.instantiate();
601 // ResourceImporterScene::get_collision_shapes() expects ImporterMesh, not Mesh.
602 // TODO: Duplicate code with EditorSceneFormatImporterESCN::import_scene()
603 // Consider making a utility function to convert from Mesh to ImporterMesh.
604 Ref<Mesh> mesh_3d_mesh = mesh_node->get_mesh();
605 Ref<ArrayMesh> array_mesh_3d_mesh = mesh_3d_mesh;
606 if (array_mesh_3d_mesh.is_valid()) {
607 // For the MeshInstance3D nodes, we need to convert the ArrayMesh to an ImporterMesh specially.
608 mesh->set_name(array_mesh_3d_mesh->get_name());
609 for (int32_t blend_i = 0; blend_i < array_mesh_3d_mesh->get_blend_shape_count(); blend_i++) {
610 mesh->add_blend_shape(array_mesh_3d_mesh->get_blend_shape_name(blend_i));
611 }
612 for (int32_t surface_i = 0; surface_i < array_mesh_3d_mesh->get_surface_count(); surface_i++) {
613 mesh->add_surface(array_mesh_3d_mesh->surface_get_primitive_type(surface_i),
614 array_mesh_3d_mesh->surface_get_arrays(surface_i),
615 array_mesh_3d_mesh->surface_get_blend_shape_arrays(surface_i),
616 array_mesh_3d_mesh->surface_get_lods(surface_i),
617 array_mesh_3d_mesh->surface_get_material(surface_i),
618 array_mesh_3d_mesh->surface_get_name(surface_i),
619 array_mesh_3d_mesh->surface_get_format(surface_i));
620 }
621 mesh->set_blend_shape_mode(array_mesh_3d_mesh->get_blend_shape_mode());
622 } else if (mesh_3d_mesh.is_valid()) {
623 // For the MeshInstance3D nodes, we need to convert the Mesh to an ImporterMesh specially.
624 mesh->set_name(mesh_3d_mesh->get_name());
625 for (int32_t surface_i = 0; surface_i < mesh_3d_mesh->get_surface_count(); surface_i++) {
626 mesh->add_surface(mesh_3d_mesh->surface_get_primitive_type(surface_i),
627 mesh_3d_mesh->surface_get_arrays(surface_i),
628 Array(),
629 mesh_3d_mesh->surface_get_lods(surface_i),
630 mesh_3d_mesh->surface_get_material(surface_i),
631 mesh_3d_mesh->surface_get_material(surface_i).is_valid() ? mesh_3d_mesh->surface_get_material(surface_i)->get_name() : String(),
632 mesh_3d_mesh->surface_get_format(surface_i));
633 }
634 }
635
636 // Generate the mesh collider.
637 Vector<Ref<Shape3D>> shapes = ResourceImporterScene::get_collision_shapes(mesh, e.value.settings, 1.0);
638 const Transform3D transform = ResourceImporterScene::get_collision_shapes_transform(e.value.settings);
639
640 Ref<ArrayMesh> collider_view_mesh;
641 collider_view_mesh.instantiate();
642 for (Ref<Shape3D> shape : shapes) {
643 Ref<ArrayMesh> debug_shape_mesh;
644 if (shape.is_valid()) {
645 debug_shape_mesh = shape->get_debug_mesh();
646 }
647 if (debug_shape_mesh.is_valid()) {
648 collider_view_mesh->add_surface_from_arrays(
649 debug_shape_mesh->surface_get_primitive_type(0),
650 debug_shape_mesh->surface_get_arrays(0));
651
652 collider_view_mesh->surface_set_material(
653 collider_view_mesh->get_surface_count() - 1,
654 collider_mat);
655 }
656 }
657
658 collider_view->set_mesh(collider_view_mesh);
659 collider_view->set_transform(transform);
660 }
661
662 // Set the collider visibility.
663 collider_view->set_visible(show_collider_view);
664 }
665
666 generate_collider = false;
667 }
668
669 void SceneImportSettingsDialog::_update_camera() {
670 AABB camera_aabb;
671
672 float rot_x = cam_rot_x;
673 float rot_y = cam_rot_y;
674 float zoom = cam_zoom;
675
676 if (selected_type == "Node" || selected_type == "Animation" || selected_type.is_empty()) {
677 camera_aabb = contents_aabb;
678 } else {
679 if (mesh_preview->get_mesh().is_valid()) {
680 camera_aabb = mesh_preview->get_transform().xform(mesh_preview->get_mesh()->get_aabb());
681 } else {
682 camera_aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
683 }
684 if (selected_type == "Mesh" && mesh_map.has(selected_id)) {
685 const MeshData &md = mesh_map[selected_id];
686 rot_x = md.cam_rot_x;
687 rot_y = md.cam_rot_y;
688 zoom = md.cam_zoom;
689 } else if (selected_type == "Material" && material_map.has(selected_id)) {
690 const MaterialData &md = material_map[selected_id];
691 rot_x = md.cam_rot_x;
692 rot_y = md.cam_rot_y;
693 zoom = md.cam_zoom;
694 }
695 }
696
697 Vector3 center = camera_aabb.get_center();
698 float camera_size = camera_aabb.get_longest_axis_size();
699
700 camera->set_orthogonal(camera_size * zoom, 0.0001, camera_size * 2);
701
702 Transform3D xf;
703 xf.basis = Basis(Vector3(0, 1, 0), rot_y) * Basis(Vector3(1, 0, 0), rot_x);
704 xf.origin = center;
705 xf.translate_local(0, 0, camera_size);
706
707 camera->set_transform(xf);
708 }
709
710 void SceneImportSettingsDialog::_load_default_subresource_settings(HashMap<StringName, Variant> &settings, const String &p_type, const String &p_import_id, ResourceImporterScene::InternalImportCategory p_category) {
711 if (base_subresource_settings.has(p_type)) {
712 Dictionary d = base_subresource_settings[p_type];
713 if (d.has(p_import_id)) {
714 d = d[p_import_id];
715 List<ResourceImporterScene::ImportOption> options;
716 _resource_importer_scene->get_internal_import_options(p_category, &options);
717 for (const ResourceImporterScene::ImportOption &E : options) {
718 String key = E.option.name;
719 if (d.has(key)) {
720 settings[key] = d[key];
721 }
722 }
723 }
724 }
725 }
726
727 void SceneImportSettingsDialog::request_generate_collider() {
728 generate_collider = true;
729 }
730
731 void SceneImportSettingsDialog::update_view() {
732 update_view_timer->start();
733 }
734
735 void SceneImportSettingsDialog::open_settings(const String &p_path, const String &p_scene_import_type) {
736 if (scene) {
737 _cleanup();
738 memdelete(scene);
739 scene = nullptr;
740 }
741
742 scene_import_settings_data->settings = nullptr;
743 scene_import_settings_data->path = p_path;
744
745 // AnimationLibrary cannot make use of the mesh and material tabs.
746 const bool disable_mesh_mat_tabs = p_scene_import_type == "AnimationLibrary";
747 data_mode->set_tab_hidden(1, disable_mesh_mat_tabs);
748 data_mode->set_tab_hidden(2, disable_mesh_mat_tabs);
749 if (disable_mesh_mat_tabs) {
750 data_mode->set_current_tab(0);
751 }
752
753 // Only show the save data options for PackedScene imports of scenes, not resource imports.
754 const bool disable_save_mesh_mat = p_scene_import_type != "PackedScene";
755 action_menu->get_popup()->set_item_disabled(action_menu->get_popup()->get_item_id(ACTION_EXTRACT_MATERIALS), disable_save_mesh_mat);
756 action_menu->get_popup()->set_item_disabled(action_menu->get_popup()->get_item_id(ACTION_CHOOSE_MESH_SAVE_PATHS), disable_save_mesh_mat);
757 const bool disable_save_anim = disable_save_mesh_mat && p_scene_import_type != "AnimationLibrary";
758 action_menu->get_popup()->set_item_disabled(action_menu->get_popup()->get_item_id(ACTION_CHOOSE_ANIMATION_SAVE_PATHS), disable_save_anim);
759
760 base_path = p_path;
761
762 mesh_set.clear();
763 animation_map.clear();
764 material_map.clear();
765 unnamed_material_name_map.clear();
766 mesh_map.clear();
767 node_map.clear();
768 defaults.clear();
769
770 mesh_preview->hide();
771
772 selected_id = "";
773 selected_type = "";
774
775 cam_rot_x = -Math::PI / 4;
776 cam_rot_y = -Math::PI / 4;
777 cam_zoom = 1;
778
779 {
780 base_subresource_settings.clear();
781
782 Ref<ConfigFile> config;
783 config.instantiate();
784 Error err = config->load(p_path + ".import");
785 if (err == OK) {
786 Vector<String> keys = config->get_section_keys("params");
787 for (const String &E : keys) {
788 Variant value = config->get_value("params", E);
789 if (E == "_subresources") {
790 base_subresource_settings = value;
791 } else {
792 defaults[E] = value;
793 }
794 }
795 }
796 }
797
798 // Regardless of p_scene_import_type, use PackedScene for pre_import because we want to see the full thing.
799 _resource_importer_scene->set_scene_import_type("PackedScene");
800 scene = _resource_importer_scene->pre_import(p_path, defaults);
801 _resource_importer_scene->set_scene_import_type(p_scene_import_type);
802 if (scene == nullptr) {
803 EditorNode::get_singleton()->show_warning(TTR("Error opening scene"));
804 return;
805 }
806
807 first_aabb = true;
808
809 _update_scene();
810
811 base_viewport->add_child(scene);
812
813 inspector->edit(nullptr);
814
815 if (first_aabb) {
816 contents_aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
817 first_aabb = false;
818 }
819
820 const HashMap<StringName, Variant> &main_settings = scene_import_settings_data->current;
821 if (main_settings.has("nodes/import_as_skeleton_bones")) {
822 previous_import_as_skeleton = main_settings["nodes/import_as_skeleton_bones"];
823 }
824 if (main_settings.has("animation/import_rest_as_RESET")) {
825 previous_rest_as_reset = main_settings["animation/import_rest_as_RESET"];
826 }
827 popup_centered_ratio();
828 _update_view_gizmos();
829 _update_camera();
830
831 // Start with the root item (Scene) selected.
832 scene_tree->get_root()->select(0);
833
834 set_title(vformat(TTR("Advanced Import Settings for %s '%s'"), _resource_importer_scene->get_visible_name(), base_path.get_file()));
835 }
836
837 SceneImportSettingsDialog *SceneImportSettingsDialog::singleton = nullptr;
838
839 SceneImportSettingsDialog *SceneImportSettingsDialog::get_singleton() {
840 return singleton;
841 }
842
843 Node *SceneImportSettingsDialog::get_selected_node() {
844 if (selected_id == "") {
845 return nullptr;
846 }
847 return node_map[selected_id].node;
848 }
849
850 void SceneImportSettingsDialog::_select(Tree *p_from, const String &p_type, const String &p_id) {
851 selecting = true;
852 scene_import_settings_data->hide_options = false;
853 // Only AnimationLibrary and actual scenes can make use of the animation and skeleton options.
854 const bool hide_anim_and_skel_options = _resource_importer_scene->get_scene_import_type() != "PackedScene" && _resource_importer_scene->get_scene_import_type() != "AnimationLibrary";
855 // Only actual scenes can make use of the node generation options such as generating physics colliders on meshes or setting a script.
856 const bool hide_node_gen_options = _resource_importer_scene->get_scene_import_type() != "PackedScene";
857
858 bones_mesh_preview->hide();
859 if (p_type == "Node") {
860 node_selected->hide(); // Always hide just in case.
861 mesh_preview->hide();
862 _reset_animation();
863
864 if (Object::cast_to<Node3D>(scene)) {
865 Object::cast_to<Node3D>(scene)->show();
866 }
867 material_tree->deselect_all();
868 mesh_tree->deselect_all();
869 NodeData &nd = node_map[p_id];
870
871 MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(nd.node);
872 if (mi) {
873 Ref<Mesh> base_mesh = mi->get_mesh();
874 if (base_mesh.is_valid()) {
875 AABB aabb = base_mesh->get_aabb();
876 Transform3D aabb_xf;
877 aabb_xf.basis.scale(aabb.size);
878 aabb_xf.origin = aabb.position;
879
880 aabb_xf = mi->get_global_transform() * aabb_xf;
881 node_selected->set_transform(aabb_xf);
882 node_selected->show();
883 }
884 }
885
886 if (nd.node == scene) {
887 scene_import_settings_data->settings = &defaults;
888 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX;
889 } else {
890 scene_import_settings_data->settings = &nd.settings;
891 if (mi) {
892 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE;
893 scene_import_settings_data->hide_options = hide_node_gen_options;
894 } else if (Object::cast_to<AnimationPlayer>(nd.node)) {
895 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE;
896 scene_import_settings_data->hide_options = hide_anim_and_skel_options;
897 } else if (Object::cast_to<Skeleton3D>(nd.node)) {
898 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE;
899 bones_mesh_preview->show();
900 scene_import_settings_data->hide_options = hide_anim_and_skel_options;
901 } else {
902 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_NODE;
903 scene_import_settings_data->hide_options = hide_node_gen_options;
904 }
905 }
906 } else if (p_type == "Animation") {
907 node_selected->hide(); // Always hide just in case.
908 mesh_preview->hide();
909 _reset_animation(p_id);
910
911 if (Object::cast_to<Node3D>(scene)) {
912 Object::cast_to<Node3D>(scene)->show();
913 }
914 material_tree->deselect_all();
915 mesh_tree->deselect_all();
916 AnimationData &ad = animation_map[p_id];
917
918 scene_import_settings_data->settings = &ad.settings;
919 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION;
920 scene_import_settings_data->hide_options = hide_anim_and_skel_options;
921
922 _animation_update_skeleton_visibility();
923 } else if (p_type == "Mesh") {
924 node_selected->hide();
925 if (Object::cast_to<Node3D>(scene)) {
926 Object::cast_to<Node3D>(scene)->hide();
927 }
928
929 MeshData &md = mesh_map[p_id];
930 if (md.mesh_node != nullptr) {
931 if (p_from != mesh_tree) {
932 md.mesh_node->uncollapse_tree();
933 md.mesh_node->select(0);
934 mesh_tree->ensure_cursor_is_visible();
935 }
936 if (p_from != scene_tree) {
937 md.scene_node->uncollapse_tree();
938 md.scene_node->select(0);
939 scene_tree->ensure_cursor_is_visible();
940 }
941 }
942
943 mesh_preview->set_mesh(md.mesh);
944 mesh_preview->show();
945 _reset_animation();
946
947 material_tree->deselect_all();
948
949 scene_import_settings_data->settings = &md.settings;
950 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH;
951 } else if (p_type == "Material") {
952 node_selected->hide();
953 if (Object::cast_to<Node3D>(scene)) {
954 Object::cast_to<Node3D>(scene)->hide();
955 }
956
957 mesh_preview->show();
958 _reset_animation();
959
960 MaterialData &md = material_map[p_id];
961
962 material_preview->set_material(md.material);
963 mesh_preview->set_mesh(material_preview);
964
965 if (p_from != mesh_tree) {
966 md.mesh_node->uncollapse_tree();
967 md.mesh_node->select(0);
968 mesh_tree->ensure_cursor_is_visible();
969 }
970 if (p_from != scene_tree) {
971 md.scene_node->uncollapse_tree();
972 md.scene_node->select(0);
973 scene_tree->ensure_cursor_is_visible();
974 }
975 if (p_from != material_tree) {
976 md.material_node->uncollapse_tree();
977 md.material_node->select(0);
978 material_tree->ensure_cursor_is_visible();
979 }
980
981 scene_import_settings_data->settings = &md.settings;
982 scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MATERIAL;
983 }
984
985 selected_type = p_type;
986 selected_id = p_id;
987
988 selecting = false;
989
990 _update_camera();
991
992 List<ResourceImporter::ImportOption> options;
993
994 if (scene_import_settings_data->category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
995 _resource_importer_scene->get_import_options(base_path, &options);
996 } else {
997 _resource_importer_scene->get_internal_import_options(scene_import_settings_data->category, &options);
998 }
999
1000 scene_import_settings_data->defaults.clear();
1001 scene_import_settings_data->current.clear();
1002
1003 if (scene_import_settings_data->settings) {
1004 for (const ResourceImporter::ImportOption &E : options) {
1005 scene_import_settings_data->defaults[E.option.name] = E.default_value;
1006 // Needed for visibility toggling (fails if something is missing).
1007 if (scene_import_settings_data->settings->has(E.option.name)) {
1008 scene_import_settings_data->current[E.option.name] = (*scene_import_settings_data->settings)[E.option.name];
1009 } else {
1010 scene_import_settings_data->current[E.option.name] = E.default_value;
1011 }
1012 }
1013 }
1014
1015 scene_import_settings_data->options = options;
1016 inspector->edit(scene_import_settings_data);
1017 scene_import_settings_data->notify_property_list_changed();
1018 }
1019
1020 void SceneImportSettingsDialog::_inspector_property_edited(const String &p_name) {
1021 if (p_name == "settings/loop_mode") {
1022 if (!animation_map.has(selected_id)) {
1023 return;
1024 }
1025 HashMap<StringName, Variant> settings = animation_map[selected_id].settings;
1026 if (settings.has(p_name)) {
1027 animation_loop_mode = static_cast<Animation::LoopMode>((int)settings[p_name]);
1028 } else {
1029 animation_loop_mode = Animation::LoopMode::LOOP_NONE;
1030 }
1031 }
1032 if ((p_name == "use_external/enabled") || (p_name == "use_external/path") || (p_name == "use_external/fallback_path")) {
1033 MaterialData &material_data = material_map[selected_id];
1034 String spath = base_path.get_base_dir();
1035 Variant value;
1036 if (_get_current("materials/extract_path", value)) {
1037 String extpath = value;
1038 if (!extpath.is_empty()) {
1039 spath = extpath;
1040 }
1041 }
1042 String opath = material_data.settings.has("use_external/path") ? (String)material_data.settings["use_external/path"] : String();
1043 if (opath.begins_with("uid://")) {
1044 opath = ResourceUID::uid_to_path(opath);
1045 }
1046 String ext = ResourceImporterScene::material_extension[_get_current("materials/extract_format", value) ? (int)value : 0];
1047 String npath = spath.path_join(selected_id.validate_filename() + ext);
1048
1049 if (!material_data.settings.has("use_external/enabled") || (bool)material_data.settings["use_external/enabled"] == false || opath != npath) {
1050 if (_get_current("materials/extract", value) && (int)value != 0) {
1051 print_line("Material settings changed, automatic material extraction disabled.");
1052 }
1053 _set_default("materials/extract", 0);
1054 }
1055 }
1056 }
1057
1058 void SceneImportSettingsDialog::_reset_bone_transforms() {
1059 for (Skeleton3D *skeleton : skeletons) {
1060 skeleton->reset_bone_poses();
1061 }
1062 }
1063
1064 void SceneImportSettingsDialog::_play_animation() {
1065 if (animation_player == nullptr) {
1066 return;
1067 }
1068 StringName id = StringName(selected_id);
1069 if (animation_player->has_animation(id)) {
1070 if (animation_player->is_playing()) {
1071 animation_player->pause();
1072 animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
1073 set_process(false);
1074 } else {
1075 animation_player->play(id);
1076 animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("Pause")));
1077 set_process(true);
1078 }
1079 }
1080 }
1081
1082 void SceneImportSettingsDialog::_stop_current_animation() {
1083 animation_pingpong = false;
1084 animation_player->stop();
1085 animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
1086 animation_slider->set_value_no_signal(0.0);
1087 set_process(false);
1088 }
1089
1090 void SceneImportSettingsDialog::_reset_animation(const String &p_animation_name) {
1091 if (p_animation_name.is_empty()) {
1092 animation_preview->hide();
1093
1094 if (animation_player != nullptr && animation_player->is_playing()) {
1095 animation_player->stop();
1096 }
1097 animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
1098
1099 _reset_bone_transforms();
1100 set_process(false);
1101 } else {
1102 _reset_bone_transforms();
1103 animation_preview->show();
1104
1105 animation_loop_mode = Animation::LoopMode::LOOP_NONE;
1106 animation_pingpong = false;
1107
1108 if (animation_map.has(p_animation_name)) {
1109 HashMap<StringName, Variant> settings = animation_map[p_animation_name].settings;
1110 if (settings.has("settings/loop_mode")) {
1111 animation_loop_mode = static_cast<Animation::LoopMode>((int)settings["settings/loop_mode"]);
1112 }
1113 }
1114
1115 if (animation_player->is_playing() && animation_loop_mode != Animation::LoopMode::LOOP_NONE) {
1116 animation_player->play(p_animation_name);
1117 } else {
1118 animation_player->stop(true);
1119 animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
1120 animation_player->set_assigned_animation(p_animation_name);
1121 animation_player->seek(0.0, true);
1122 animation_slider->set_value_no_signal(0.0);
1123 set_process(false);
1124 }
1125 }
1126 }
1127
1128 void SceneImportSettingsDialog::_animation_slider_value_changed(double p_value) {
1129 if (animation_player == nullptr || !animation_map.has(selected_id) || animation_map[selected_id].animation.is_null()) {
1130 return;
1131 }
1132 if (animation_player->is_playing()) {
1133 animation_player->stop();
1134 animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
1135 set_process(false);
1136 }
1137 animation_player->seek(p_value * animation_map[selected_id].animation->get_length(), true);
1138 }
1139
1140 void SceneImportSettingsDialog::_skeleton_tree_entered(Skeleton3D *p_skeleton) {
1141 bones_mesh_preview->set_skeleton_path(p_skeleton->get_path());
1142 Ref<Skin> skin = p_skeleton->create_skin_from_rest_transforms();
1143 p_skeleton->register_skin(skin);
1144 bones_mesh_preview->set_skin(skin);
1145 }
1146
1147 void SceneImportSettingsDialog::_animation_finished(const StringName &p_name) {
1148 Animation::LoopMode loop_mode = animation_loop_mode;
1149
1150 switch (loop_mode) {
1151 case Animation::LOOP_NONE: {
1152 animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
1153 animation_slider->set_value_no_signal(1.0);
1154 set_process(false);
1155 } break;
1156 case Animation::LOOP_LINEAR: {
1157 animation_player->play(p_name);
1158 } break;
1159 case Animation::LOOP_PINGPONG: {
1160 if (animation_pingpong) {
1161 animation_player->play(p_name);
1162 } else {
1163 animation_player->play_backwards(p_name);
1164 }
1165 animation_pingpong = !animation_pingpong;
1166 } break;
1167 default: {
1168 } break;
1169 }
1170 }
1171
1172 void SceneImportSettingsDialog::_animation_update_skeleton_visibility() {
1173 if (animation_toggle_skeleton_visibility->is_pressed()) {
1174 bones_mesh_preview->show();
1175 } else {
1176 bones_mesh_preview->hide();
1177 }
1178 }
1179
1180 void SceneImportSettingsDialog::_material_tree_selected() {
1181 if (selecting) {
1182 return;
1183 }
1184 TreeItem *item = material_tree->get_selected();
1185 String type = item->get_meta("type");
1186 String import_id = item->get_meta("import_id");
1187
1188 _select(material_tree, type, import_id);
1189 }
1190
1191 void SceneImportSettingsDialog::_mesh_tree_selected() {
1192 if (selecting) {
1193 return;
1194 }
1195
1196 TreeItem *item = mesh_tree->get_selected();
1197 String type = item->get_meta("type");
1198 String import_id = item->get_meta("import_id");
1199
1200 _select(mesh_tree, type, import_id);
1201 }
1202
1203 void SceneImportSettingsDialog::_scene_tree_selected() {
1204 if (selecting) {
1205 return;
1206 }
1207 TreeItem *item = scene_tree->get_selected();
1208 String type = item->get_meta("type");
1209 String import_id = item->get_meta("import_id");
1210
1211 _select(scene_tree, type, import_id);
1212 }
1213
1214 void SceneImportSettingsDialog::_cleanup() {
1215 skeletons.clear();
1216 if (animation_player != nullptr) {
1217 animation_player->disconnect(SceneStringName(animation_finished), callable_mp(this, &SceneImportSettingsDialog::_animation_finished));
1218 animation_player = nullptr;
1219 }
1220 set_process(false);
1221 }
1222
1223 void SceneImportSettingsDialog::_on_light_1_switch_pressed() {
1224 light1->set_visible(light_1_switch->is_pressed());
1225 }
1226
1227 void SceneImportSettingsDialog::_on_light_2_switch_pressed() {
1228 light2->set_visible(light_2_switch->is_pressed());
1229 }
1230
1231 void SceneImportSettingsDialog::_on_light_rotate_switch_pressed() {
1232 bool light_top_level = !light_rotate_switch->is_pressed();
1233 light1->set_as_top_level_keep_local(light_top_level);
1234 light2->set_as_top_level_keep_local(light_top_level);
1235 }
1236
1237 void SceneImportSettingsDialog::_viewport_input(const Ref<InputEvent> &p_input) {
1238 float *rot_x = &cam_rot_x;
1239 float *rot_y = &cam_rot_y;
1240 float *zoom = &cam_zoom;
1241
1242 if (selected_type == "Mesh" && mesh_map.has(selected_id)) {
1243 MeshData &md = mesh_map[selected_id];
1244 rot_x = &md.cam_rot_x;
1245 rot_y = &md.cam_rot_y;
1246 zoom = &md.cam_zoom;
1247 } else if (selected_type == "Material" && material_map.has(selected_id)) {
1248 MaterialData &md = material_map[selected_id];
1249 rot_x = &md.cam_rot_x;
1250 rot_y = &md.cam_rot_y;
1251 zoom = &md.cam_zoom;
1252 }
1253 Ref<InputEventMouseMotion> mm = p_input;
1254 if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
1255 (*rot_x) -= mm->get_relative().y * 0.01 * EDSCALE;
1256 (*rot_y) -= mm->get_relative().x * 0.01 * EDSCALE;
1257 (*rot_x) = CLAMP((*rot_x), -Math::PI / 2, Math::PI / 2);
1258 _update_camera();
1259 }
1260 if (mm.is_valid() && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CURSOR_SHAPE)) {
1261 DisplayServer::get_singleton()->cursor_set_shape(DisplayServer::CursorShape::CURSOR_ARROW);
1262 }
1263 Ref<InputEventMouseButton> mb = p_input;
1264 if (mb.is_valid() && mb->get_button_index() == MouseButton::WHEEL_DOWN) {
1265 (*zoom) *= 1.1;
1266 if ((*zoom) > 10.0) {
1267 (*zoom) = 10.0;
1268 }
1269 _update_camera();
1270 }
1271 if (mb.is_valid() && mb->get_button_index() == MouseButton::WHEEL_UP) {
1272 (*zoom) /= 1.1;
1273 if ((*zoom) < 0.1) {
1274 (*zoom) = 0.1;
1275 }
1276 _update_camera();
1277 }
1278 Ref<InputEventMagnifyGesture> mg = p_input;
1279 if (mg.is_valid()) {
1280 real_t mg_factor = mg->get_factor();
1281 if (mg_factor == 0.0) {
1282 mg_factor = 1.0;
1283 }
1284 (*zoom) /= mg_factor;
1285 if ((*zoom) < 0.1) {
1286 (*zoom) = 0.1;
1287 } else if ((*zoom) > 10.0) {
1288 (*zoom) = 10.0;
1289 }
1290 _update_camera();
1291 }
1292 }
1293
1294 void SceneImportSettingsDialog::_re_import() {
1295 HashMap<StringName, Variant> main_settings;
1296
1297 main_settings = scene_import_settings_data->current;
1298 main_settings.erase("_subresources");
1299 Dictionary nodes;
1300 Dictionary materials;
1301 Dictionary meshes;
1302 Dictionary animations;
1303
1304 Dictionary subresources;
1305
1306 for (KeyValue<String, NodeData> &E : node_map) {
1307 if (E.value.settings.size()) {
1308 Dictionary d;
1309 for (const KeyValue<StringName, Variant> &F : E.value.settings) {
1310 d[String(F.key)] = F.value;
1311 }
1312 nodes[E.key] = d;
1313 }
1314 }
1315 if (nodes.size()) {
1316 subresources["nodes"] = nodes;
1317 }
1318
1319 for (KeyValue<String, MaterialData> &E : material_map) {
1320 if (E.value.settings.size()) {
1321 Dictionary d;
1322 for (const KeyValue<StringName, Variant> &F : E.value.settings) {
1323 d[String(F.key)] = F.value;
1324 }
1325 materials[E.key] = d;
1326 }
1327 }
1328 if (materials.size()) {
1329 subresources["materials"] = materials;
1330 }
1331
1332 for (KeyValue<String, MeshData> &E : mesh_map) {
1333 if (E.value.settings.size()) {
1334 Dictionary d;
1335 for (const KeyValue<StringName, Variant> &F : E.value.settings) {
1336 d[String(F.key)] = F.value;
1337 }
1338 meshes[E.key] = d;
1339 }
1340 }
1341 if (meshes.size()) {
1342 subresources["meshes"] = meshes;
1343 }
1344
1345 for (KeyValue<String, AnimationData> &E : animation_map) {
1346 if (E.value.settings.size()) {
1347 Dictionary d;
1348 for (const KeyValue<StringName, Variant> &F : E.value.settings) {
1349 d[String(F.key)] = F.value;
1350 }
1351 animations[E.key] = d;
1352 }
1353 }
1354 if (animations.size()) {
1355 subresources["animations"] = animations;
1356 }
1357
1358 main_settings["_subresources"] = subresources;
1359
1360 _cleanup(); // Prevent skeletons and other pointers from pointing to dangling references.
1361 EditorFileSystem::get_singleton()->reimport_file_with_custom_parameters(base_path, _resource_importer_scene->get_importer_name(), main_settings);
1362 }
1363
1364 void SceneImportSettingsDialog::_update_theme_item_cache() {
1365 ConfirmationDialog::_update_theme_item_cache();
1366 theme_cache.light_1_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight1"));
1367 theme_cache.light_2_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight2"));
1368 theme_cache.rotate_icon = get_editor_theme_icon(SNAME("PreviewRotate"));
1369 }
1370
1371 void SceneImportSettingsDialog::_notification(int p_what) {
1372 switch (p_what) {
1373 case NOTIFICATION_READY: {
1374 connect(SceneStringName(confirmed), callable_mp(this, &SceneImportSettingsDialog::_re_import));
1375 } break;
1376
1377 case NOTIFICATION_THEME_CHANGED: {
1378 if (animation_player != nullptr && animation_player->is_playing()) {
1379 animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("Pause")));
1380 } else {
1381 animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
1382 }
1383 animation_stop_button->set_button_icon(get_editor_theme_icon(SNAME("Stop")));
1384
1385 light_1_switch->set_button_icon(theme_cache.light_1_icon);
1386 light_2_switch->set_button_icon(theme_cache.light_2_icon);
1387 light_rotate_switch->set_button_icon(theme_cache.rotate_icon);
1388
1389 animation_toggle_skeleton_visibility->set_button_icon(get_editor_theme_icon(SNAME("SkeletonPreview")));
1390 } break;
1391
1392 case NOTIFICATION_PROCESS: {
1393 if (animation_player != nullptr) {
1394 animation_slider->set_value_no_signal(animation_player->get_current_animation_position() / animation_player->get_current_animation_length());
1395 }
1396 } break;
1397
1398 case NOTIFICATION_VISIBILITY_CHANGED: {
1399 if (!is_visible()) {
1400 _cleanup();
1401 }
1402 } break;
1403 }
1404 }
1405
1406 void SceneImportSettingsDialog::_menu_callback(int p_id) {
1407 switch (p_id) {
1408 case ACTION_EXTRACT_MATERIALS: {
1409 save_path->set_title(TTR("Select folder to extract material resources"));
1410 external_extension_type->select(0);
1411 } break;
1412 case ACTION_CHOOSE_MESH_SAVE_PATHS: {
1413 save_path->set_title(TTR("Select folder where mesh resources will save on import"));
1414 external_extension_type->select(1);
1415 } break;
1416 case ACTION_CHOOSE_ANIMATION_SAVE_PATHS: {
1417 save_path->set_title(TTR("Select folder where animations will save on import"));
1418 external_extension_type->select(1);
1419 } break;
1420 }
1421
1422 save_path->set_current_dir(base_path.get_base_dir());
1423 current_action = p_id;
1424 save_path->popup_centered_ratio();
1425 }
1426
1427 void SceneImportSettingsDialog::_save_path_changed(const String &p_path) {
1428 save_path_item->set_text(1, p_path);
1429
1430 if (FileAccess::exists(p_path)) {
1431 save_path_item->set_text(2, TTR("Warning: File exists"));
1432 save_path_item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced."));
1433 save_path_item->set_icon(2, get_editor_theme_icon(SNAME("StatusWarning")));
1434
1435 } else {
1436 save_path_item->set_text(2, TTR("Will create new file"));
1437 save_path_item->set_icon(2, get_editor_theme_icon(SNAME("StatusSuccess")));
1438 }
1439 }
1440
1441 void SceneImportSettingsDialog::_browse_save_callback(Object *p_item, int p_column, int p_id, MouseButton p_button) {
1442 if (p_button != MouseButton::LEFT) {
1443 return;
1444 }
1445
1446 TreeItem *item = Object::cast_to<TreeItem>(p_item);
1447
1448 String path = item->get_text(1);
1449
1450 item_save_path->set_current_file(path);
1451 save_path_item = item;
1452
1453 item_save_path->popup_centered_ratio();
1454 }
1455
1456 void SceneImportSettingsDialog::_save_dir_callback(const String &p_path) {
1457 external_path_tree->clear();
1458 TreeItem *root = external_path_tree->create_item();
1459 save_path_items.clear();
1460
1461 switch (current_action) {
1462 case ACTION_EXTRACT_MATERIALS: {
1463 for (const KeyValue<String, MaterialData> &E : material_map) {
1464 MaterialData &md = material_map[E.key];
1465
1466 TreeItem *item = external_path_tree->create_item(root);
1467
1468 String name = md.material_node->get_text(0);
1469
1470 item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
1471 item->set_icon(0, get_editor_theme_icon(SNAME("StandardMaterial3D")));
1472 item->set_text(0, name);
1473
1474 if (md.has_import_id) {
1475 if (md.settings.has("use_external/enabled") && bool(md.settings["use_external/enabled"])) {
1476 item->set_text(2, TTR("Already External"));
1477 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."));
1478 } else {
1479 item->set_metadata(0, E.key);
1480 item->set_editable(0, true);
1481 item->set_checked(0, true);
1482 name = name.validate_filename();
1483 String path = p_path.path_join(name);
1484 if (external_extension_type->get_selected() == 0) {
1485 path += ".tres";
1486 } else {
1487 path += ".res";
1488 }
1489
1490 item->set_text(1, path);
1491 if (FileAccess::exists(path)) {
1492 item->set_text(2, TTR("Warning: File exists"));
1493 item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced."));
1494 item->set_icon(2, get_editor_theme_icon(SNAME("StatusWarning")));
1495
1496 } else {
1497 item->set_text(2, TTR("Will create new file"));
1498 item->set_icon(2, get_editor_theme_icon(SNAME("StatusSuccess")));
1499 }
1500
1501 item->add_button(1, get_editor_theme_icon(SNAME("Folder")));
1502 }
1503
1504 } else {
1505 item->set_text(2, TTR("No import ID"));
1506 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."));
1507 item->set_icon(2, get_editor_theme_icon(SNAME("StatusError")));
1508 }
1509
1510 save_path_items.push_back(item);
1511 }
1512
1513 external_paths->set_title(TTR("Extract Materials to Resource Files"));
1514 external_paths->set_ok_button_text(TTR("Extract"));
1515 } break;
1516 case ACTION_CHOOSE_MESH_SAVE_PATHS: {
1517 for (const KeyValue<String, MeshData> &E : mesh_map) {
1518 MeshData &md = mesh_map[E.key];
1519
1520 TreeItem *item = external_path_tree->create_item(root);
1521
1522 String name = md.mesh_node->get_text(0);
1523
1524 item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
1525 item->set_icon(0, get_editor_theme_icon(SNAME("MeshItem")));
1526 item->set_text(0, name);
1527
1528 if (md.has_import_id) {
1529 if (md.settings.has("save_to_file/enabled") && bool(md.settings["save_to_file/enabled"])) {
1530 item->set_text(2, TTR("Already Saving"));
1531 item->set_tooltip_text(2, TTR("This mesh already saves to an external resource, no action will be taken."));
1532 } else {
1533 item->set_metadata(0, E.key);
1534 item->set_editable(0, true);
1535 item->set_checked(0, true);
1536 name = name.validate_filename();
1537 String path = p_path.path_join(name);
1538 if (external_extension_type->get_selected() == 0) {
1539 path += ".tres";
1540 } else {
1541 path += ".res";
1542 }
1543
1544 item->set_text(1, path);
1545 if (FileAccess::exists(path)) {
1546 item->set_text(2, TTR("Warning: File exists"));
1547 item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced on import."));
1548 item->set_icon(2, get_editor_theme_icon(SNAME("StatusWarning")));
1549
1550 } else {
1551 item->set_text(2, TTR("Will save to new file"));
1552 item->set_icon(2, get_editor_theme_icon(SNAME("StatusSuccess")));
1553 }
1554
1555 item->add_button(1, get_editor_theme_icon(SNAME("Folder")));
1556 }
1557
1558 } else {
1559 item->set_text(2, TTR("No import ID"));
1560 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."));
1561 item->set_icon(2, get_editor_theme_icon(SNAME("StatusError")));
1562 }
1563
1564 save_path_items.push_back(item);
1565 }
1566
1567 external_paths->set_title(TTR("Set paths to save meshes as resource files on Reimport"));
1568 external_paths->set_ok_button_text(TTR("Set Paths"));
1569 } break;
1570 case ACTION_CHOOSE_ANIMATION_SAVE_PATHS: {
1571 for (const KeyValue<String, AnimationData> &E : animation_map) {
1572 AnimationData &ad = animation_map[E.key];
1573
1574 TreeItem *item = external_path_tree->create_item(root);
1575
1576 String name = ad.scene_node->get_text(0);
1577
1578 item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
1579 item->set_icon(0, get_editor_theme_icon(SNAME("Animation")));
1580 item->set_text(0, name);
1581
1582 if (ad.settings.has("save_to_file/enabled") && bool(ad.settings["save_to_file/enabled"])) {
1583 item->set_text(2, TTR("Already Saving"));
1584 item->set_tooltip_text(2, TTR("This animation already saves to an external resource, no action will be taken."));
1585 } else {
1586 item->set_metadata(0, E.key);
1587 item->set_editable(0, true);
1588 item->set_checked(0, true);
1589 name = name.validate_filename();
1590 String path = p_path.path_join(name);
1591 if (external_extension_type->get_selected() == 0) {
1592 path += ".tres";
1593 } else {
1594 path += ".res";
1595 }
1596
1597 item->set_text(1, path);
1598 if (FileAccess::exists(path)) {
1599 item->set_text(2, TTR("Warning: File exists"));
1600 item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced on import."));
1601 item->set_icon(2, get_editor_theme_icon(SNAME("StatusWarning")));
1602
1603 } else {
1604 item->set_text(2, TTR("Will save to new file"));
1605 item->set_icon(2, get_editor_theme_icon(SNAME("StatusSuccess")));
1606 }
1607
1608 item->add_button(1, get_editor_theme_icon(SNAME("Folder")));
1609 }
1610
1611 save_path_items.push_back(item);
1612 }
1613
1614 external_paths->set_title(TTR("Set paths to save animations as resource files on Reimport"));
1615 external_paths->set_ok_button_text(TTR("Set Paths"));
1616
1617 } break;
1618 }
1619
1620 external_paths->popup_centered_ratio();
1621 }
1622
1623 void SceneImportSettingsDialog::_save_dir_confirm() {
1624 for (int i = 0; i < save_path_items.size(); i++) {
1625 TreeItem *item = save_path_items[i];
1626 if (!item->is_checked(0)) {
1627 continue; //ignore
1628 }
1629 String path = item->get_text(1);
1630 String uid_path = path;
1631 if (path.begins_with("uid://")) {
1632 path = ResourceUID::uid_to_path(uid_path);
1633 }
1634 if (!path.is_resource_file()) {
1635 continue;
1636 }
1637
1638 String id = item->get_metadata(0);
1639
1640 switch (current_action) {
1641 case ACTION_EXTRACT_MATERIALS: {
1642 ERR_CONTINUE(!material_map.has(id));
1643 MaterialData &md = material_map[id];
1644
1645 Error err = ResourceSaver::save(md.material, path);
1646 if (err != OK) {
1647 EditorNode::get_singleton()->add_io_error(TTR("Can't make material external to file, write error:") + "\n\t" + path);
1648 continue;
1649 }
1650 uid_path = ResourceUID::path_to_uid(path);
1651
1652 md.settings["use_external/enabled"] = true;
1653 md.settings["use_external/path"] = uid_path;
1654 md.settings["use_external/fallback_path"] = path;
1655
1656 } break;
1657 case ACTION_CHOOSE_MESH_SAVE_PATHS: {
1658 ERR_CONTINUE(!mesh_map.has(id));
1659 MeshData &md = mesh_map[id];
1660
1661 md.settings["save_to_file/enabled"] = true;
1662 md.settings["save_to_file/path"] = uid_path;
1663 md.settings["save_to_file/fallback_path"] = path;
1664 } break;
1665 case ACTION_CHOOSE_ANIMATION_SAVE_PATHS: {
1666 ERR_CONTINUE(!animation_map.has(id));
1667 AnimationData &ad = animation_map[id];
1668
1669 ad.settings["save_to_file/enabled"] = true;
1670 ad.settings["save_to_file/path"] = uid_path;
1671 ad.settings["save_to_file/fallback_path"] = path;
1672
1673 } break;
1674 }
1675 }
1676
1677 if (current_action == ACTION_EXTRACT_MATERIALS) {
1678 //as this happens right now, the scene needs to be saved and reimported.
1679 _re_import();
1680 open_settings(base_path);
1681 } else {
1682 scene_import_settings_data->notify_property_list_changed();
1683 }
1684 }
1685
1686 SceneImportSettingsDialog::SceneImportSettingsDialog() {
1687 singleton = this;
1688 _resource_importer_scene = memnew(ResourceImporterScene("PackedScene"));
1689
1690 VBoxContainer *main_vb = memnew(VBoxContainer);
1691 add_child(main_vb);
1692 HBoxContainer *menu_hb = memnew(HBoxContainer);
1693 main_vb->add_child(menu_hb);
1694
1695 action_menu = memnew(MenuButton);
1696 action_menu->set_text(TTR("Actions..."));
1697 // Style the MenuButton like a regular Button to make it more noticeable.
1698 action_menu->set_flat(false);
1699 action_menu->set_focus_mode(Control::FOCUS_ALL);
1700 menu_hb->add_child(action_menu);
1701
1702 action_menu->get_popup()->add_item(TTR("Extract Materials"), ACTION_EXTRACT_MATERIALS);
1703 action_menu->get_popup()->add_separator();
1704 action_menu->get_popup()->add_item(TTR("Set Animation Save Paths"), ACTION_CHOOSE_ANIMATION_SAVE_PATHS);
1705 action_menu->get_popup()->add_item(TTR("Set Mesh Save Paths"), ACTION_CHOOSE_MESH_SAVE_PATHS);
1706
1707 action_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &SceneImportSettingsDialog::_menu_callback));
1708
1709 tree_split = memnew(HSplitContainer);
1710 main_vb->add_child(tree_split);
1711 tree_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1712
1713 data_mode = memnew(TabContainer);
1714 tree_split->add_child(data_mode);
1715 data_mode->set_custom_minimum_size(Size2(300 * EDSCALE, 0));
1716 data_mode->set_theme_type_variation("TabContainerOdd");
1717
1718 property_split = memnew(HSplitContainer);
1719 tree_split->add_child(property_split);
1720 property_split->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1721
1722 scene_tree = memnew(Tree);
1723 scene_tree->set_name(TTR("Scene"));
1724 scene_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
1725 data_mode->add_child(scene_tree);
1726 scene_tree->connect("cell_selected", callable_mp(this, &SceneImportSettingsDialog::_scene_tree_selected));
1727
1728 mesh_tree = memnew(Tree);
1729 mesh_tree->set_name(TTR("Meshes"));
1730 mesh_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
1731 data_mode->add_child(mesh_tree);
1732 mesh_tree->set_hide_root(true);
1733 mesh_tree->connect("cell_selected", callable_mp(this, &SceneImportSettingsDialog::_mesh_tree_selected));
1734
1735 material_tree = memnew(Tree);
1736 material_tree->set_name(TTR("Materials"));
1737 material_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
1738 data_mode->add_child(material_tree);
1739 material_tree->connect("cell_selected", callable_mp(this, &SceneImportSettingsDialog::_material_tree_selected));
1740
1741 material_tree->set_hide_root(true);
1742
1743 VBoxContainer *vp_vb = memnew(VBoxContainer);
1744 vp_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1745 vp_vb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1746 vp_vb->set_anchors_and_offsets_preset(Control::LayoutPreset::PRESET_FULL_RECT);
1747 property_split->add_child(vp_vb);
1748
1749 SubViewportContainer *vp_container = memnew(SubViewportContainer);
1750 vp_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1751 vp_container->set_custom_minimum_size(Size2(10, 10));
1752 vp_container->set_stretch(true);
1753 vp_container->connect(SceneStringName(gui_input), callable_mp(this, &SceneImportSettingsDialog::_viewport_input));
1754 vp_vb->add_child(vp_container);
1755
1756 base_viewport = memnew(SubViewport);
1757 vp_container->add_child(base_viewport);
1758
1759 animation_preview = memnew(PanelContainer);
1760 animation_preview->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1761 vp_vb->add_child(animation_preview);
1762 animation_preview->hide();
1763
1764 HBoxContainer *animation_hbox = memnew(HBoxContainer);
1765 animation_preview->add_child(animation_hbox);
1766
1767 animation_play_button = memnew(Button);
1768 animation_hbox->add_child(animation_play_button);
1769 animation_play_button->set_flat(true);
1770 animation_play_button->set_accessibility_name(TTRC("Selected Animation Play/Pause"));
1771 animation_play_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
1772 animation_play_button->set_shortcut(ED_SHORTCUT("scene_import_settings/play_selected_animation", TTRC("Selected Animation Play/Pause"), Key::SPACE));
1773 animation_play_button->connect(SceneStringName(pressed), callable_mp(this, &SceneImportSettingsDialog::_play_animation));
1774
1775 animation_stop_button = memnew(Button);
1776 animation_hbox->add_child(animation_stop_button);
1777 animation_stop_button->set_flat(true);
1778 animation_stop_button->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
1779 animation_stop_button->set_tooltip_text(TTR("Selected Animation Stop"));
1780 animation_stop_button->connect(SceneStringName(pressed), callable_mp(this, &SceneImportSettingsDialog::_stop_current_animation));
1781
1782 animation_slider = memnew(HSlider);
1783 animation_hbox->add_child(animation_slider);
1784 animation_slider->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1785 animation_slider->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1786 animation_slider->set_max(1.0);
1787 animation_slider->set_step(1.0 / 100.0);
1788 animation_slider->set_value_no_signal(0.0);
1789 animation_slider->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
1790 animation_slider->set_accessibility_name(TTRC("Animation"));
1791 animation_slider->connect(SceneStringName(value_changed), callable_mp(this, &SceneImportSettingsDialog::_animation_slider_value_changed));
1792
1793 animation_toggle_skeleton_visibility = memnew(Button);
1794 animation_hbox->add_child(animation_toggle_skeleton_visibility);
1795 animation_toggle_skeleton_visibility->set_toggle_mode(true);
1796 animation_toggle_skeleton_visibility->set_theme_type_variation("FlatButton");
1797 animation_toggle_skeleton_visibility->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
1798 animation_toggle_skeleton_visibility->set_tooltip_text(TTR("Toggle Animation Skeleton Visibility"));
1799
1800 animation_toggle_skeleton_visibility->connect(SceneStringName(pressed), callable_mp(this, &SceneImportSettingsDialog::_animation_update_skeleton_visibility));
1801
1802 base_viewport->set_use_own_world_3d(true);
1803
1804 HBoxContainer *viewport_hbox = memnew(HBoxContainer);
1805 vp_container->add_child(viewport_hbox);
1806 viewport_hbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 2);
1807
1808 viewport_hbox->add_spacer();
1809
1810 VBoxContainer *vb_light = memnew(VBoxContainer);
1811 vb_light->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1812 viewport_hbox->add_child(vb_light);
1813
1814 light_rotate_switch = memnew(Button);
1815 light_rotate_switch->set_theme_type_variation("PreviewLightButton");
1816 light_rotate_switch->set_toggle_mode(true);
1817 light_rotate_switch->set_pressed(true);
1818 light_rotate_switch->set_tooltip_text(TTR("Rotate Lights With Model"));
1819 light_rotate_switch->connect(SceneStringName(pressed), callable_mp(this, &SceneImportSettingsDialog::_on_light_rotate_switch_pressed));
1820 vb_light->add_child(light_rotate_switch);
1821
1822 light_1_switch = memnew(Button);
1823 light_1_switch->set_theme_type_variation("PreviewLightButton");
1824 light_1_switch->set_toggle_mode(true);
1825 light_1_switch->set_pressed(true);
1826 light_1_switch->set_tooltip_text(TTR("Primary Light"));
1827 light_1_switch->connect(SceneStringName(pressed), callable_mp(this, &SceneImportSettingsDialog::_on_light_1_switch_pressed));
1828 vb_light->add_child(light_1_switch);
1829
1830 light_2_switch = memnew(Button);
1831 light_2_switch->set_theme_type_variation("PreviewLightButton");
1832 light_2_switch->set_toggle_mode(true);
1833 light_2_switch->set_pressed(true);
1834 light_2_switch->set_tooltip_text(TTR("Secondary Light"));
1835 light_2_switch->connect(SceneStringName(pressed), callable_mp(this, &SceneImportSettingsDialog::_on_light_2_switch_pressed));
1836 vb_light->add_child(light_2_switch);
1837
1838 camera = memnew(Camera3D);
1839 base_viewport->add_child(camera);
1840 camera->make_current();
1841
1842 if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
1843 camera_attributes.instantiate();
1844 camera->set_attributes(camera_attributes);
1845 }
1846
1847 // Use a grayscale gradient sky to avoid skewing the preview towards a specific color,
1848 // but still allow shaded areas to be easily distinguished (using the ambient and reflected light).
1849 // This also helps the user orient themselves in the preview, since the bottom of the sky is black
1850 // and the top of the sky is white.
1851 procedural_sky_material.instantiate();
1852 procedural_sky_material->set_sky_top_color(Color(1, 1, 1));
1853 procedural_sky_material->set_sky_horizon_color(Color(0.5, 0.5, 0.5));
1854 procedural_sky_material->set_ground_horizon_color(Color(0.5, 0.5, 0.5));
1855 procedural_sky_material->set_ground_bottom_color(Color(0, 0, 0));
1856 procedural_sky_material->set_sky_curve(2.0);
1857 procedural_sky_material->set_ground_curve(0.5);
1858 // Hide the sun from the sky.
1859 procedural_sky_material->set_sun_angle_max(0.0);
1860 sky.instantiate();
1861 sky->set_material(procedural_sky_material);
1862 environment.instantiate();
1863 environment->set_background(Environment::BG_SKY);
1864 environment->set_sky(sky);
1865 // A custom FOV must be specified, as an orthogonal camera is used for the preview.
1866 environment->set_sky_custom_fov(50.0);
1867 camera->set_environment(environment);
1868
1869 light1 = memnew(DirectionalLight3D);
1870 light1->set_transform(Transform3D(Basis::looking_at(Vector3(-1, -1, -1))));
1871 light1->set_shadow(true);
1872 camera->add_child(light1);
1873
1874 light2 = memnew(DirectionalLight3D);
1875 light2->set_transform(Transform3D(Basis::looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1))));
1876 light2->set_color(Color(0.5f, 0.5f, 0.5f));
1877 camera->add_child(light2);
1878
1879 {
1880 Ref<StandardMaterial3D> selection_mat;
1881 selection_mat.instantiate();
1882 selection_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
1883 selection_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
1884 selection_mat->set_albedo(Color(1, 0.8, 1.0));
1885
1886 Ref<SurfaceTool> st;
1887 st.instantiate();
1888 st->begin(Mesh::PRIMITIVE_LINES);
1889
1890 AABB base_aabb;
1891 base_aabb.size = Vector3(1, 1, 1);
1892
1893 for (int i = 0; i < 12; i++) {
1894 Vector3 a, b;
1895 base_aabb.get_edge(i, a, b);
1896
1897 st->add_vertex(a);
1898 st->add_vertex(a.lerp(b, 0.2));
1899 st->add_vertex(b);
1900 st->add_vertex(b.lerp(a, 0.2));
1901 }
1902
1903 selection_mesh.instantiate();
1904 st->commit(selection_mesh);
1905 selection_mesh->surface_set_material(0, selection_mat);
1906
1907 node_selected = memnew(MeshInstance3D);
1908 node_selected->set_mesh(selection_mesh);
1909 node_selected->set_cast_shadows_setting(GeometryInstance3D::SHADOW_CASTING_SETTING_OFF);
1910 base_viewport->add_child(node_selected);
1911 node_selected->hide();
1912 }
1913
1914 {
1915 mesh_preview = memnew(MeshInstance3D);
1916 base_viewport->add_child(mesh_preview);
1917 mesh_preview->hide();
1918
1919 material_preview.instantiate();
1920 }
1921
1922 {
1923 collider_mat.instantiate();
1924 collider_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
1925 collider_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
1926 collider_mat->set_albedo(Color(0.5, 0.5, 1.0));
1927 }
1928
1929 {
1930 bones_mesh_preview = memnew(MeshInstance3D);
1931 bones_mesh_preview->set_cast_shadows_setting(GeometryInstance3D::SHADOW_CASTING_SETTING_OFF);
1932 bones_mesh_preview->set_skeleton_path(NodePath());
1933 base_viewport->add_child(bones_mesh_preview);
1934 }
1935
1936 inspector = memnew(EditorInspector);
1937 inspector->set_custom_minimum_size(Size2(300 * EDSCALE, 0));
1938 inspector->connect(SNAME("property_edited"), callable_mp(this, &SceneImportSettingsDialog::_inspector_property_edited));
1939 // Display the same tooltips as in the Import dock.
1940 inspector->set_object_class(ResourceImporterScene::get_class_static());
1941 inspector->set_use_doc_hints(true);
1942 inspector->set_theme_type_variation(SNAME("EditorInspectorForeground"));
1943
1944 property_split->add_child(inspector);
1945
1946 scene_import_settings_data = memnew(SceneImportSettingsData);
1947
1948 set_ok_button_text(TTR("Reimport"));
1949 set_cancel_button_text(TTR("Close"));
1950
1951 external_paths = memnew(ConfirmationDialog);
1952 add_child(external_paths);
1953 external_path_tree = memnew(Tree);
1954 external_paths->add_child(external_path_tree);
1955 external_path_tree->connect("button_clicked", callable_mp(this, &SceneImportSettingsDialog::_browse_save_callback));
1956 external_paths->connect(SceneStringName(confirmed), callable_mp(this, &SceneImportSettingsDialog::_save_dir_confirm));
1957 external_path_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
1958 external_path_tree->set_columns(3);
1959 external_path_tree->set_column_titles_visible(true);
1960 external_path_tree->set_column_expand(0, true);
1961 external_path_tree->set_column_custom_minimum_width(0, 100 * EDSCALE);
1962 external_path_tree->set_column_title(0, TTR("Resource"));
1963 external_path_tree->set_column_expand(1, true);
1964 external_path_tree->set_column_custom_minimum_width(1, 100 * EDSCALE);
1965 external_path_tree->set_column_title(1, TTR("Path"));
1966 external_path_tree->set_column_expand(2, false);
1967 external_path_tree->set_column_custom_minimum_width(2, 200 * EDSCALE);
1968 external_path_tree->set_column_title(2, TTR("Status"));
1969 save_path = memnew(EditorFileDialog);
1970 save_path->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR);
1971 HBoxContainer *extension_hb = memnew(HBoxContainer);
1972 save_path->get_vbox()->add_child(extension_hb);
1973 extension_hb->add_spacer();
1974 extension_hb->add_child(memnew(Label(TTR("Save Extension:"))));
1975 external_extension_type = memnew(OptionButton);
1976 extension_hb->add_child(external_extension_type);
1977 external_extension_type->add_item(TTR("Text: *.tres"));
1978 external_extension_type->add_item(TTR("Binary: *.res"));
1979 external_path_tree->set_hide_root(true);
1980 add_child(save_path);
1981
1982 item_save_path = memnew(EditorFileDialog);
1983 item_save_path->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
1984 item_save_path->add_filter("*.tres", TTR("Text Resource"));
1985 item_save_path->add_filter("*.res", TTR("Binary Resource"));
1986 add_child(item_save_path);
1987 item_save_path->connect("file_selected", callable_mp(this, &SceneImportSettingsDialog::_save_path_changed));
1988
1989 save_path->connect("dir_selected", callable_mp(this, &SceneImportSettingsDialog::_save_dir_callback));
1990
1991 update_view_timer = memnew(Timer);
1992 update_view_timer->set_wait_time(0.2);
1993 update_view_timer->set_one_shot(true);
1994 update_view_timer->connect("timeout", callable_mp(this, &SceneImportSettingsDialog::_update_view_gizmos));
1995 add_child(update_view_timer);
1996 }
1997
1998 SceneImportSettingsDialog::~SceneImportSettingsDialog() {
1999 memdelete(scene_import_settings_data);
2000 memdelete(_resource_importer_scene);
2001 }