Debian Codegraph @

modules/gltf/editor/editor_scene_importer_blend.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_scene_importer_blend.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 "editor_scene_importer_blend.h"
32
33 #include "../gltf_defines.h"
34 #include "../gltf_document.h"
35 #include "editor_import_blend_runner.h"
36
37 #include "core/config/project_settings.h"
38 #include "editor/editor_node.h"
39 #include "editor/editor_string_names.h"
40 #include "editor/gui/editor_file_dialog.h"
41 #include "editor/settings/editor_settings.h"
42 #include "editor/themes/editor_scale.h"
43 #include "main/main.h"
44 #include "scene/gui/line_edit.h"
45
46 #ifdef WINDOWS_ENABLED
47 #include <shlwapi.h>
48 #endif
49
50 static bool _get_blender_version(const String &p_path, int &r_major, int &r_minor, String *r_err = nullptr) {
51 if (!FileAccess::exists(p_path)) {
52 if (r_err) {
53 *r_err = TTR("Path does not point to a valid executable.");
54 }
55 return false;
56 }
57 List<String> args;
58 args.push_back("--version");
59 String pipe;
60 Error err = OS::get_singleton()->execute(p_path, args, &pipe);
61 if (err != OK) {
62 if (r_err) {
63 *r_err = TTR("Couldn't run Blender executable.");
64 }
65 return false;
66 }
67 int bl = pipe.find("Blender ");
68 if (bl == -1) {
69 if (r_err) {
70 *r_err = vformat(TTR("Unexpected --version output from Blender executable at: %s."), p_path);
71 }
72 return false;
73 }
74 pipe = pipe.substr(bl);
75 pipe = pipe.replace_first("Blender ", "");
76 int pp = pipe.find_char('.');
77 if (pp == -1) {
78 if (r_err) {
79 *r_err = vformat(TTR("Couldn't extract version information from Blender executable at: %s."), p_path);
80 }
81 return false;
82 }
83 String v = pipe.substr(0, pp);
84 r_major = v.to_int();
85 if (r_major < 3) {
86 if (r_err) {
87 *r_err = vformat(TTR("Found Blender version %d.x, which is too old for this importer (3.0+ is required)."), r_major);
88 }
89 return false;
90 }
91
92 int pp2 = pipe.find_char('.', pp + 1);
93 r_minor = pp2 > pp ? pipe.substr(pp + 1, pp2 - pp - 1).to_int() : 0;
94
95 return true;
96 }
97
98 void EditorSceneFormatImporterBlend::get_extensions(List<String> *r_extensions) const {
99 r_extensions->push_back("blend");
100 }
101
102 Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path, uint32_t p_flags,
103 const HashMap<StringName, Variant> &p_options,
104 List<String> *r_missing_deps, Error *r_err) {
105 String blender_path = EDITOR_GET("filesystem/import/blender/blender_path");
106
107 ERR_FAIL_COND_V_MSG(blender_path.is_empty(), nullptr, "Blender path is empty, check your Editor Settings.");
108 ERR_FAIL_COND_V_MSG(!FileAccess::exists(blender_path), nullptr, vformat("Invalid Blender path: %s, check your Editor Settings.", blender_path));
109
110 if (blender_major_version == -1 || blender_minor_version == -1 || last_tested_blender_path != blender_path) {
111 String error;
112 if (!_get_blender_version(blender_path, blender_major_version, blender_minor_version, &error)) {
113 ERR_FAIL_V_MSG(nullptr, error);
114 }
115 last_tested_blender_path = blender_path;
116 }
117
118 // Get global paths for source and sink.
119 // Escape paths to be valid Python strings to embed in the script.
120 String source_global = ProjectSettings::get_singleton()->globalize_path(p_path);
121
122 #ifdef WINDOWS_ENABLED
123 // On Windows, when using a network share path, the above will return a path starting with "//"
124 // which once handed to Blender will be treated like a relative path. So we need to replace the
125 // first two characters with "\\" to make it absolute again.
126 if (source_global.is_network_share_path()) {
127 source_global = "\\\\" + source_global.substr(2);
128 }
129 #endif
130
131 const String blend_basename = p_path.get_file().get_basename();
132 const String sink = ProjectSettings::get_singleton()->get_imported_files_path().path_join(
133 vformat("%s-%s.gltf", blend_basename, p_path.md5_text()));
134 const String sink_global = ProjectSettings::get_singleton()->globalize_path(sink);
135 // If true, unpack the original images to the Godot file system and use them. Allows changing image import settings like VRAM compression.
136 // If false, allow Blender to convert the original images, such as re-packing roughness and metallic into one roughness+metallic texture.
137 // In most cases this is desired, but if the .blend file's images are not in the correct format, this must be disabled for correct behavior.
138 const bool unpack_original_images = p_options.has(SNAME("blender/materials/unpack_enabled")) && p_options[SNAME("blender/materials/unpack_enabled")];
139
140 // Handle configuration options.
141
142 Dictionary request_options;
143 Dictionary parameters_map;
144
145 parameters_map["filepath"] = sink_global;
146 parameters_map["export_keep_originals"] = unpack_original_images;
147 parameters_map["export_format"] = "GLTF_SEPARATE";
148 parameters_map["export_yup"] = true;
149 parameters_map["export_import_convert_lighting_mode"] = "COMPAT";
150
151 if (p_options.has(SNAME("blender/nodes/custom_properties")) && p_options[SNAME("blender/nodes/custom_properties")]) {
152 parameters_map["export_extras"] = true;
153 } else {
154 parameters_map["export_extras"] = false;
155 }
156 if (p_options.has(SNAME("blender/meshes/skins"))) {
157 int32_t skins = p_options["blender/meshes/skins"];
158 if (skins == BLEND_BONE_INFLUENCES_NONE) {
159 parameters_map["export_skins"] = false;
160 } else if (skins == BLEND_BONE_INFLUENCES_COMPATIBLE) {
161 parameters_map["export_skins"] = true;
162 parameters_map["export_all_influences"] = false;
163 } else if (skins == BLEND_BONE_INFLUENCES_ALL) {
164 parameters_map["export_skins"] = true;
165 parameters_map["export_all_influences"] = true;
166 }
167 } else {
168 parameters_map["export_skins"] = false;
169 }
170 if (p_options.has(SNAME("blender/materials/export_materials"))) {
171 int32_t exports = p_options["blender/materials/export_materials"];
172 switch (exports) {
173 case BLEND_MATERIAL_EXPORT_PLACEHOLDER: {
174 parameters_map["export_materials"] = "PLACEHOLDER";
175 } break;
176 case BLEND_MATERIAL_EXPORT_EXPORT: {
177 parameters_map["export_materials"] = "EXPORT";
178 } break;
179 case BLEND_MATERIAL_EXPORT_NAMED_PLACEHOLDER: {
180 parameters_map["export_materials"] = "EXPORT";
181 parameters_map["export_image_format"] = "NONE";
182 } break;
183 }
184 } else {
185 parameters_map["export_materials"] = "PLACEHOLDER";
186 }
187 if (p_options.has(SNAME("blender/nodes/cameras")) && p_options[SNAME("blender/nodes/cameras")]) {
188 parameters_map["export_cameras"] = true;
189 } else {
190 parameters_map["export_cameras"] = false;
191 }
192 if (p_options.has(SNAME("blender/nodes/punctual_lights")) && p_options[SNAME("blender/nodes/punctual_lights")]) {
193 parameters_map["export_lights"] = true;
194 } else {
195 parameters_map["export_lights"] = false;
196 }
197 if (blender_major_version > 4 || (blender_major_version == 4 && blender_minor_version >= 2)) {
198 if (p_options.has(SNAME("blender/meshes/colors")) && p_options[SNAME("blender/meshes/colors")]) {
199 parameters_map["export_vertex_color"] = "MATERIAL";
200 } else {
201 parameters_map["export_vertex_color"] = "NONE";
202 }
203 } else {
204 if (p_options.has(SNAME("blender/meshes/colors")) && p_options[SNAME("blender/meshes/colors")]) {
205 parameters_map["export_colors"] = true;
206 } else {
207 parameters_map["export_colors"] = false;
208 }
209 }
210 if (p_options.has(SNAME("blender/nodes/visible"))) {
211 int32_t visible = p_options["blender/nodes/visible"];
212 if (visible == BLEND_VISIBLE_VISIBLE_ONLY) {
213 parameters_map["use_visible"] = true;
214 } else if (visible == BLEND_VISIBLE_RENDERABLE) {
215 parameters_map["use_renderable"] = true;
216 } else if (visible == BLEND_VISIBLE_ALL) {
217 parameters_map["use_renderable"] = false;
218 parameters_map["use_visible"] = false;
219 }
220 } else {
221 parameters_map["use_renderable"] = false;
222 parameters_map["use_visible"] = false;
223 }
224 if (p_options.has(SNAME("blender/nodes/active_collection_only")) && p_options[SNAME("blender/nodes/active_collection_only")]) {
225 parameters_map["use_active_collection"] = true;
226 }
227
228 if (p_options.has(SNAME("blender/meshes/uvs")) && p_options[SNAME("blender/meshes/uvs")]) {
229 parameters_map["export_texcoords"] = true;
230 } else {
231 parameters_map["export_texcoords"] = false;
232 }
233 if (p_options.has(SNAME("blender/meshes/normals")) && p_options[SNAME("blender/meshes/normals")]) {
234 parameters_map["export_normals"] = true;
235 } else {
236 parameters_map["export_normals"] = false;
237 }
238
239 if (blender_major_version > 4 || (blender_major_version == 4 && blender_minor_version >= 1)) {
240 if (p_options.has(SNAME("blender/meshes/export_geometry_nodes_instances")) && p_options[SNAME("blender/meshes/export_geometry_nodes_instances")]) {
241 parameters_map["export_gn_mesh"] = true;
242 if (blender_major_version == 4 && blender_minor_version == 1) {
243 // There is a bug in Blender 4.1 where it can't export lights and geometry nodes at the same time, one must be disabled.
244 parameters_map["export_lights"] = false;
245 }
246 } else {
247 parameters_map["export_gn_mesh"] = false;
248 }
249 }
250 if (blender_major_version >= 4) {
251 if (p_options.has(SNAME("blender/meshes/gpu_instances")) && p_options[SNAME("blender/meshes/gpu_instances")]) {
252 parameters_map["export_gpu_instances"] = true;
253 } else {
254 parameters_map["export_gpu_instances"] = false;
255 }
256 }
257
258 if (p_options.has(SNAME("blender/meshes/tangents")) && p_options[SNAME("blender/meshes/tangents")]) {
259 parameters_map["export_tangents"] = true;
260 } else {
261 parameters_map["export_tangents"] = false;
262 }
263 if (p_options.has(SNAME("blender/animation/group_tracks")) && p_options[SNAME("blender/animation/group_tracks")]) {
264 if (blender_major_version > 3 || (blender_major_version == 3 && blender_minor_version >= 6)) {
265 parameters_map["export_animation_mode"] = "ACTIONS";
266 } else {
267 parameters_map["export_nla_strips"] = true;
268 }
269 } else {
270 if (blender_major_version > 3 || (blender_major_version == 3 && blender_minor_version >= 6)) {
271 parameters_map["export_animation_mode"] = "ACTIVE_ACTIONS";
272 } else {
273 parameters_map["export_nla_strips"] = false;
274 }
275 }
276 if (p_options.has(SNAME("blender/animation/limit_playback")) && p_options[SNAME("blender/animation/limit_playback")]) {
277 parameters_map["export_frame_range"] = true;
278 } else {
279 parameters_map["export_frame_range"] = false;
280 }
281 if (p_options.has(SNAME("blender/animation/always_sample")) && p_options[SNAME("blender/animation/always_sample")]) {
282 parameters_map["export_force_sampling"] = true;
283 } else {
284 parameters_map["export_force_sampling"] = false;
285 }
286 if (p_options.has(SNAME("blender/meshes/export_bones_deforming_mesh_only")) && p_options[SNAME("blender/meshes/export_bones_deforming_mesh_only")]) {
287 parameters_map["export_def_bones"] = true;
288 } else {
289 parameters_map["export_def_bones"] = false;
290 }
291 if (p_options.has(SNAME("blender/nodes/modifiers")) && p_options[SNAME("blender/nodes/modifiers")]) {
292 parameters_map["export_apply"] = true;
293 } else {
294 parameters_map["export_apply"] = false;
295 }
296
297 request_options["unpack_all"] = unpack_original_images;
298 request_options["path"] = source_global;
299 request_options["gltf_options"] = parameters_map;
300
301 // Run Blender and export glTF.
302 Error err = EditorImportBlendRunner::get_singleton()->do_import(request_options);
303 if (err != OK) {
304 if (r_err) {
305 *r_err = ERR_SCRIPT_FAILED;
306 }
307 return nullptr;
308 }
309
310 // Import the generated glTF.
311
312 // Use GLTFDocument instead of glTF importer to keep image references.
313 Ref<GLTFDocument> gltf;
314 gltf.instantiate();
315 Ref<GLTFState> state;
316 state.instantiate();
317
318 if (p_options.has("gltf/naming_version")) {
319 int naming_version = p_options["gltf/naming_version"];
320 gltf->set_naming_version(naming_version);
321 }
322 if (p_options.has(SNAME("nodes/import_as_skeleton_bones")) ? (bool)p_options[SNAME("nodes/import_as_skeleton_bones")] : false) {
323 state->set_import_as_skeleton_bones(true);
324 }
325 state->set_scene_name(blend_basename);
326 state->set_extract_path(p_path.get_base_dir());
327 state->set_extract_prefix(blend_basename);
328 err = gltf->append_from_file(sink.get_basename() + ".gltf", state, p_flags, sink.get_base_dir());
329 if (err != OK) {
330 if (r_err) {
331 *r_err = FAILED;
332 }
333 return nullptr;
334 }
335 ERR_FAIL_COND_V(!p_options.has("animation/fps"), nullptr);
336
337 #ifndef DISABLE_DEPRECATED
338 bool trimming = p_options.has("animation/trimming") ? (bool)p_options["animation/trimming"] : false;
339 return gltf->generate_scene(state, (float)p_options["animation/fps"], trimming, false);
340 #else
341 return gltf->generate_scene(state, (float)p_options["animation/fps"], (bool)p_options["animation/trimming"], false);
342 #endif
343 }
344
345 Variant EditorSceneFormatImporterBlend::get_option_visibility(const String &p_path, const String &p_scene_import_type, const String &p_option,
346 const HashMap<StringName, Variant> &p_options) {
347 if (!p_path.has_extension("blend")) {
348 return true;
349 }
350
351 if (p_option.begins_with("animation/")) {
352 if (p_option != "animation/import" && !bool(p_options["animation/import"])) {
353 return false;
354 }
355 }
356 return true;
357 }
358
359 void EditorSceneFormatImporterBlend::get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options) {
360 // Returns all the options when path is empty because that means it's for the Project Settings.
361 if (!p_path.is_empty() && !p_path.has_extension("blend")) {
362 return;
363 }
364 #define ADD_OPTION_BOOL(PATH, VALUE) \
365 r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, SNAME(PATH)), VALUE));
366 #define ADD_OPTION_ENUM(PATH, ENUM_HINT, VALUE) \
367 r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::INT, SNAME(PATH), PROPERTY_HINT_ENUM, ENUM_HINT), VALUE));
368
369 ADD_OPTION_ENUM("blender/nodes/visible", "All,Visible Only,Renderable", BLEND_VISIBLE_ALL);
370 ADD_OPTION_BOOL("blender/nodes/active_collection_only", false);
371 ADD_OPTION_BOOL("blender/nodes/punctual_lights", true);
372 ADD_OPTION_BOOL("blender/nodes/cameras", true);
373 ADD_OPTION_BOOL("blender/nodes/custom_properties", true);
374 ADD_OPTION_ENUM("blender/nodes/modifiers", "No Modifiers,All Modifiers", BLEND_MODIFIERS_ALL);
375 ADD_OPTION_BOOL("blender/meshes/colors", false);
376 ADD_OPTION_BOOL("blender/meshes/uvs", true);
377 ADD_OPTION_BOOL("blender/meshes/normals", true);
378 ADD_OPTION_BOOL("blender/meshes/export_geometry_nodes_instances", false);
379 ADD_OPTION_BOOL("blender/meshes/gpu_instances", false);
380 ADD_OPTION_BOOL("blender/meshes/tangents", true);
381 ADD_OPTION_ENUM("blender/meshes/skins", "None,4 Influences (Compatible),All Influences", BLEND_BONE_INFLUENCES_ALL);
382 ADD_OPTION_BOOL("blender/meshes/export_bones_deforming_mesh_only", false);
383 ADD_OPTION_BOOL("blender/materials/unpack_enabled", true);
384 ADD_OPTION_ENUM("blender/materials/export_materials", "Placeholder,Export,Named Placeholder", BLEND_MATERIAL_EXPORT_EXPORT);
385 ADD_OPTION_BOOL("blender/animation/limit_playback", true);
386 ADD_OPTION_BOOL("blender/animation/always_sample", true);
387 ADD_OPTION_BOOL("blender/animation/group_tracks", true);
388
389 r_options->push_back(ResourceImporterScene::ImportOption(PropertyInfo(Variant::INT, "gltf/naming_version", PROPERTY_HINT_ENUM, "Godot 4.0 or 4.1,Godot 4.2 to 4.4,Godot 4.5 or later"), 2));
390 }
391
392 ///////////////////////////
393
394 static bool _test_blender_path(const String &p_path, String *r_err = nullptr) {
395 int major, minor;
396 return _get_blender_version(p_path, major, minor, r_err);
397 }
398
399 bool EditorFileSystemImportFormatSupportQueryBlend::is_active() const {
400 bool blend_enabled = GLOBAL_GET_CACHED(bool, "filesystem/import/blender/enabled");
401
402 if (blend_enabled && !_test_blender_path(EDITOR_GET("filesystem/import/blender/blender_path").operator String())) {
403 // Intending to import Blender, but blend not configured.
404 return true;
405 }
406
407 return false;
408 }
409 Vector<String> EditorFileSystemImportFormatSupportQueryBlend::get_file_extensions() const {
410 Vector<String> ret;
411 ret.push_back("blend");
412 return ret;
413 }
414
415 void EditorFileSystemImportFormatSupportQueryBlend::_validate_path(String p_path) {
416 String error;
417 bool success = false;
418 if (p_path == "") {
419 error = TTR("Path is empty.");
420 } else {
421 if (_test_blender_path(p_path, &error)) {
422 success = true;
423 if (auto_detected_path == p_path) {
424 error = TTR("Path to Blender executable is valid (Autodetected).");
425 } else {
426 error = TTR("Path to Blender executable is valid.");
427 }
428 }
429 }
430
431 path_status->set_text(error);
432
433 if (success) {
434 path_status->add_theme_color_override(SceneStringName(font_color), path_status->get_theme_color(SNAME("success_color"), EditorStringName(Editor)));
435 configure_blender_dialog->get_ok_button()->set_disabled(false);
436 } else {
437 path_status->add_theme_color_override(SceneStringName(font_color), path_status->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
438 configure_blender_dialog->get_ok_button()->set_disabled(true);
439 }
440 }
441
442 bool EditorFileSystemImportFormatSupportQueryBlend::_autodetect_path() {
443 // Autodetect
444 auto_detected_path = "";
445
446 #if defined(MACOS_ENABLED)
447 Vector<String> find_paths = {
448 "/opt/homebrew/bin/blender",
449 "/opt/local/bin/blender",
450 "/usr/local/bin/blender",
451 "/usr/local/opt/blender",
452 "/Applications/Blender.app/Contents/MacOS/Blender",
453 };
454 {
455 List<String> mdfind_args;
456 mdfind_args.push_back("kMDItemCFBundleIdentifier=org.blenderfoundation.blender");
457
458 String output;
459 Error err = OS::get_singleton()->execute("mdfind", mdfind_args, &output);
460 if (err == OK) {
461 for (const String &find_path : output.split("\n")) {
462 find_paths.push_back(find_path.path_join("Contents/MacOS/Blender"));
463 }
464 }
465 }
466 #elif defined(WINDOWS_ENABLED)
467 Vector<String> find_paths = {
468 "C:\\Program Files\\Blender Foundation\\blender.exe",
469 "C:\\Program Files (x86)\\Blender Foundation\\blender.exe",
470 };
471 {
472 char blender_opener_path[MAX_PATH];
473 DWORD path_len = MAX_PATH;
474 HRESULT res = AssocQueryString(0, ASSOCSTR_EXECUTABLE, ".blend", "open", blender_opener_path, &path_len);
475 if (res == S_OK) {
476 find_paths.push_back(String(blender_opener_path).get_base_dir().path_join("blender.exe"));
477 }
478 }
479
480 #elif defined(UNIX_ENABLED)
481 Vector<String> find_paths = {
482 "/usr/bin/blender",
483 "/usr/local/bin/blender",
484 "/opt/blender/bin/blender",
485 };
486 #endif
487
488 for (const String &find_path : find_paths) {
489 if (_test_blender_path(find_path)) {
490 auto_detected_path = find_path;
491 return true;
492 }
493 }
494
495 return false;
496 }
497
498 void EditorFileSystemImportFormatSupportQueryBlend::_path_confirmed() {
499 confirmed = true;
500 }
501
502 void EditorFileSystemImportFormatSupportQueryBlend::_select_install(String p_path) {
503 blender_path->set_text(p_path);
504 _validate_path(p_path);
505 }
506 void EditorFileSystemImportFormatSupportQueryBlend::_browse_install() {
507 if (blender_path->get_text() != String()) {
508 browse_dialog->set_current_file(blender_path->get_text());
509 }
510
511 browse_dialog->popup_centered_ratio();
512 }
513
514 void EditorFileSystemImportFormatSupportQueryBlend::_update_icons() {
515 blender_path_browse->set_button_icon(blender_path_browse->get_editor_theme_icon(SNAME("FolderBrowse")));
516 }
517
518 bool EditorFileSystemImportFormatSupportQueryBlend::query() {
519 ERR_FAIL_COND_V_MSG(DisplayServer::get_singleton()->get_name() == "headless", true, "Blender path is invalid or not set, check your Editor Settings. Cannot configure blender path in headless mode.");
520
521 if (!configure_blender_dialog) {
522 configure_blender_dialog = memnew(ConfirmationDialog);
523 configure_blender_dialog->set_title(TTR("Configure Blender Importer"));
524 configure_blender_dialog->set_flag(Window::FLAG_BORDERLESS, true); // Avoid closing accidentally.
525 configure_blender_dialog->set_close_on_escape(false);
526
527 String select_exec_label = TTR("Blender 3.0+ is required to import '.blend' files.\nPlease provide a valid path to a Blender executable.");
528 #ifdef MACOS_ENABLED
529 select_exec_label += "\n" + TTR("On macOS, this should be the `Contents/MacOS/blender` file within the Blender `.app` folder.");
530 #endif
531 VBoxContainer *vb = memnew(VBoxContainer);
532 vb->add_child(memnew(Label(select_exec_label)));
533
534 HBoxContainer *hb = memnew(HBoxContainer);
535
536 blender_path = memnew(LineEdit);
537 blender_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
538 blender_path->set_accessibility_name(TTRC("Path"));
539 hb->add_child(blender_path);
540
541 blender_path_browse = memnew(Button);
542 blender_path_browse->set_text(TTR("Browse"));
543 blender_path_browse->connect(SceneStringName(pressed), callable_mp(this, &EditorFileSystemImportFormatSupportQueryBlend::_browse_install));
544 hb->add_child(blender_path_browse);
545
546 hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
547 hb->set_custom_minimum_size(Size2(400 * EDSCALE, 0));
548
549 vb->add_child(hb);
550
551 path_status = memnew(Label);
552 path_status->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
553 vb->add_child(path_status);
554
555 configure_blender_dialog->add_child(vb);
556
557 blender_path->connect(SceneStringName(text_changed), callable_mp(this, &EditorFileSystemImportFormatSupportQueryBlend::_validate_path));
558
559 EditorNode::get_singleton()->get_gui_base()->add_child(configure_blender_dialog);
560
561 configure_blender_dialog->set_ok_button_text(TTR("Confirm Path"));
562 configure_blender_dialog->set_cancel_button_text(TTR("Disable '.blend' Import"));
563 configure_blender_dialog->get_cancel_button()->set_tooltip_text(TTR("Disables Blender '.blend' files import for this project. Can be re-enabled in Project Settings."));
564 configure_blender_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorFileSystemImportFormatSupportQueryBlend::_path_confirmed));
565
566 browse_dialog = memnew(EditorFileDialog);
567 browse_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
568 browse_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
569 browse_dialog->connect("file_selected", callable_mp(this, &EditorFileSystemImportFormatSupportQueryBlend::_select_install));
570
571 EditorNode::get_singleton()->get_gui_base()->add_child(browse_dialog);
572
573 // Update icons.
574 // This is a hack because we can't rely on notifications here as we don't receive them.
575 // Usually, we only have to wait for `NOTIFICATION_THEME_CHANGED` to update the icons.
576 callable_mp(this, &EditorFileSystemImportFormatSupportQueryBlend::_update_icons).call_deferred();
577 }
578
579 String path = EDITOR_GET("filesystem/import/blender/blender_path");
580
581 if (path.is_empty() && _autodetect_path()) {
582 path = auto_detected_path;
583 }
584
585 blender_path->set_text(path);
586
587 _validate_path(path);
588
589 configure_blender_dialog->popup_centered();
590 confirmed = false;
591
592 while (true) {
593 DisplayServer::get_singleton()->process_events();
594 Main::iteration();
595 if (!configure_blender_dialog->is_visible() || confirmed) {
596 break;
597 }
598 }
599
600 if (confirmed) {
601 // Can only confirm a valid path.
602 EditorSettings::get_singleton()->set("filesystem/import/blender/blender_path", blender_path->get_text());
603 EditorSettings::get_singleton()->save();
604 } else {
605 // Disable Blender import
606 ProjectSettings::get_singleton()->set("filesystem/import/blender/enabled", false);
607 ProjectSettings::get_singleton()->save();
608
609 if (EditorNode::immediate_confirmation_dialog(TTR("Disabling '.blend' file import requires restarting the editor."), TTR("Save & Restart"), TTR("Restart"))) {
610 EditorNode::get_singleton()->save_all_scenes();
611 }
612 EditorNode::get_singleton()->restart_editor();
613 return true;
614 }
615
616 return false;
617 }