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