fbx_importer_manager.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**************************************************************************/
  2. /* fbx_importer_manager.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 "fbx_importer_manager.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/themes/editor_scale.h"
  36. #include "scene/gui/link_button.h"
  37. void FBXImporterManager::_notification(int p_what) {
  38. switch (p_what) {
  39. case NOTIFICATION_THEME_CHANGED: {
  40. fbx_path_browse->set_button_icon(get_editor_theme_icon(SNAME("FileBrowse")));
  41. } break;
  42. case NOTIFICATION_READY: {
  43. connect(SceneStringName(confirmed), callable_mp(this, &FBXImporterManager::_path_confirmed));
  44. } break;
  45. }
  46. }
  47. void FBXImporterManager::show_dialog(bool p_exclusive) {
  48. String fbx2gltf_path = EDITOR_GET("filesystem/import/fbx/fbx2gltf_path");
  49. fbx_path->set_text(fbx2gltf_path);
  50. _validate_path(fbx2gltf_path);
  51. // If exclusive, we're importing a FBX file, there's no exit.
  52. is_importing = p_exclusive;
  53. set_flag(Window::FLAG_BORDERLESS, p_exclusive); // Avoid closing accidentally.
  54. set_close_on_escape(!p_exclusive);
  55. if (is_importing) {
  56. get_cancel_button()->set_text(TTR("Disable FBX2glTF & Restart"));
  57. get_cancel_button()->set_tooltip_text(TTR("Canceling this dialog will disable the FBX2glTF importer and use the ufbx importer.\nYou can re-enable FBX2glTF in the Project Settings under Filesystem > Import > FBX > Enabled.\n\nThe editor will restart as importers are registered when the editor starts."));
  58. } else {
  59. get_cancel_button()->set_text(TTR("Cancel"));
  60. get_cancel_button()->set_tooltip_text("");
  61. }
  62. popup_centered();
  63. }
  64. void FBXImporterManager::_validate_path(const String &p_path) {
  65. String error;
  66. bool success = false;
  67. if (p_path == "") {
  68. error = TTR("Path to FBX2glTF executable is empty.");
  69. } else if (!FileAccess::exists(p_path)) {
  70. error = TTR("Path to FBX2glTF executable is invalid.");
  71. } else {
  72. List<String> args;
  73. args.push_back("--version");
  74. int exitcode;
  75. Error err = OS::get_singleton()->execute(p_path, args, nullptr, &exitcode);
  76. if (err == OK && exitcode == 0) {
  77. success = true;
  78. } else {
  79. error = TTR("Error executing this file (wrong version or architecture).");
  80. }
  81. }
  82. if (success) {
  83. path_status->set_text(TTR("FBX2glTF executable is valid."));
  84. path_status->add_theme_color_override(SceneStringName(font_color), path_status->get_theme_color(SNAME("success_color"), EditorStringName(Editor)));
  85. get_ok_button()->set_disabled(false);
  86. } else {
  87. path_status->set_text(error);
  88. path_status->add_theme_color_override(SceneStringName(font_color), path_status->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  89. get_ok_button()->set_disabled(true);
  90. }
  91. }
  92. void FBXImporterManager::_select_file(const String &p_path) {
  93. fbx_path->set_text(p_path);
  94. _validate_path(p_path);
  95. }
  96. void FBXImporterManager::_path_confirmed() {
  97. String path = fbx_path->get_text();
  98. EditorSettings::get_singleton()->set("filesystem/import/fbx/fbx2gltf_path", path);
  99. EditorSettings::get_singleton()->save();
  100. }
  101. void FBXImporterManager::_cancel_setup() {
  102. if (!is_importing) {
  103. return; // No worry.
  104. }
  105. // No escape.
  106. ProjectSettings::get_singleton()->set("filesystem/import/fbx2gltf/enabled", false);
  107. ProjectSettings::get_singleton()->save();
  108. EditorNode::get_singleton()->save_all_scenes();
  109. EditorNode::get_singleton()->restart_editor();
  110. }
  111. void FBXImporterManager::_browse_install() {
  112. if (fbx_path->get_text() != String()) {
  113. browse_dialog->set_current_file(fbx_path->get_text());
  114. }
  115. browse_dialog->popup_centered_ratio();
  116. }
  117. FBXImporterManager *FBXImporterManager::singleton = nullptr;
  118. FBXImporterManager::FBXImporterManager() {
  119. singleton = this;
  120. set_title(TTR("Configure FBX Importer"));
  121. VBoxContainer *vb = memnew(VBoxContainer);
  122. vb->add_child(memnew(Label(TTR("FBX2glTF is required for importing FBX files if using FBX2glTF.\nAlternatively, you can use ufbx by disabling FBX2glTF.\nPlease download the necessary tool and provide a valid path to the binary:"))));
  123. LinkButton *lb = memnew(LinkButton);
  124. lb->set_text(TTR("Click this link to download FBX2glTF"));
  125. lb->set_uri("https://godotengine.org/fbx-import");
  126. vb->add_child(lb);
  127. HBoxContainer *hb = memnew(HBoxContainer);
  128. fbx_path = memnew(LineEdit);
  129. fbx_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  130. hb->add_child(fbx_path);
  131. fbx_path_browse = memnew(Button);
  132. fbx_path_browse->set_text(TTR("Browse"));
  133. fbx_path_browse->connect(SceneStringName(pressed), callable_mp(this, &FBXImporterManager::_browse_install));
  134. hb->add_child(fbx_path_browse);
  135. hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  136. hb->set_custom_minimum_size(Size2(400 * EDSCALE, 0));
  137. vb->add_child(hb);
  138. path_status = memnew(Label);
  139. vb->add_child(path_status);
  140. add_child(vb);
  141. fbx_path->connect(SceneStringName(text_changed), callable_mp(this, &FBXImporterManager::_validate_path));
  142. get_ok_button()->set_text(TTR("Confirm Path"));
  143. get_cancel_button()->connect(SceneStringName(pressed), callable_mp(this, &FBXImporterManager::_cancel_setup));
  144. browse_dialog = memnew(EditorFileDialog);
  145. browse_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  146. browse_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  147. #ifdef WINDOWS_ENABLED
  148. browse_dialog->add_filter("*.exe");
  149. #endif
  150. browse_dialog->connect("file_selected", callable_mp(this, &FBXImporterManager::_select_file));
  151. add_child(browse_dialog);
  152. }