editor_scene_importer_blend.cpp 23 KB

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