editor_scene_importer_fbx.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**************************************************************************/
  2. /* editor_scene_importer_fbx.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_fbx.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "../gltf_document.h"
  33. #include "core/config/project_settings.h"
  34. #include "editor/editor_settings.h"
  35. #include "main/main.h"
  36. uint32_t EditorSceneFormatImporterFBX::get_import_flags() const {
  37. return ImportFlags::IMPORT_SCENE | ImportFlags::IMPORT_ANIMATION;
  38. }
  39. void EditorSceneFormatImporterFBX::get_extensions(List<String> *r_extensions) const {
  40. r_extensions->push_back("fbx");
  41. }
  42. Node *EditorSceneFormatImporterFBX::import_scene(const String &p_path, uint32_t p_flags,
  43. const HashMap<StringName, Variant> &p_options,
  44. List<String> *r_missing_deps, Error *r_err) {
  45. // Get global paths for source and sink.
  46. // Don't use `c_escape()` as it can generate broken paths. These paths will be
  47. // enclosed in double quotes by OS::execute(), so we only need to escape those.
  48. // `c_escape_multiline()` seems to do this (escapes `\` and `"` only).
  49. const String source_global = ProjectSettings::get_singleton()->globalize_path(p_path).c_escape_multiline();
  50. const String sink = ProjectSettings::get_singleton()->get_imported_files_path().path_join(
  51. vformat("%s-%s.glb", p_path.get_file().get_basename(), p_path.md5_text()));
  52. const String sink_global = ProjectSettings::get_singleton()->globalize_path(sink).c_escape_multiline();
  53. // Run fbx2gltf.
  54. String fbx2gltf_path = EDITOR_GET("filesystem/import/fbx/fbx2gltf_path");
  55. List<String> args;
  56. args.push_back("--pbr-metallic-roughness");
  57. args.push_back("--input");
  58. args.push_back(source_global);
  59. args.push_back("--output");
  60. args.push_back(sink_global);
  61. args.push_back("--binary");
  62. String standard_out;
  63. int ret;
  64. OS::get_singleton()->execute(fbx2gltf_path, args, &standard_out, &ret, true);
  65. print_verbose(fbx2gltf_path);
  66. print_verbose(standard_out);
  67. if (ret != 0) {
  68. if (r_err) {
  69. *r_err = ERR_SCRIPT_FAILED;
  70. }
  71. ERR_PRINT(vformat("FBX conversion to glTF failed with error: %d.", ret));
  72. return nullptr;
  73. }
  74. // Import the generated glTF.
  75. // Use GLTFDocument instead of glTF importer to keep image references.
  76. Ref<GLTFDocument> gltf;
  77. gltf.instantiate();
  78. Ref<GLTFState> state;
  79. state.instantiate();
  80. print_verbose(vformat("glTF path: %s", sink));
  81. Error err = gltf->append_from_file(sink, state, p_flags, p_path.get_base_dir());
  82. if (err != OK) {
  83. if (r_err) {
  84. *r_err = FAILED;
  85. }
  86. return nullptr;
  87. }
  88. #ifndef DISABLE_DEPRECATED
  89. bool trimming = p_options.has("animation/trimming") ? (bool)p_options["animation/trimming"] : false;
  90. bool remove_immutable = p_options.has("animation/remove_immutable_tracks") ? (bool)p_options["animation/remove_immutable_tracks"] : true;
  91. return gltf->generate_scene(state, (float)p_options["animation/fps"], trimming, remove_immutable);
  92. #else
  93. return gltf->generate_scene(state, (float)p_options["animation/fps"], (bool)p_options["animation/trimming"], (bool)p_options["animation/remove_immutable_tracks"]);
  94. #endif
  95. }
  96. Variant EditorSceneFormatImporterFBX::get_option_visibility(const String &p_path, bool p_for_animation,
  97. const String &p_option, const HashMap<StringName, Variant> &p_options) {
  98. return true;
  99. }
  100. void EditorSceneFormatImporterFBX::get_import_options(const String &p_path,
  101. List<ResourceImporter::ImportOption> *r_options) {
  102. }
  103. bool EditorFileSystemImportFormatSupportQueryFBX::is_active() const {
  104. String fbx2gltf_path = EDITOR_GET("filesystem/import/fbx/fbx2gltf_path");
  105. return !FileAccess::exists(fbx2gltf_path);
  106. }
  107. Vector<String> EditorFileSystemImportFormatSupportQueryFBX::get_file_extensions() const {
  108. Vector<String> ret;
  109. ret.push_back("fbx");
  110. return ret;
  111. }
  112. bool EditorFileSystemImportFormatSupportQueryFBX::query() {
  113. FBXImporterManager::get_singleton()->show_dialog(true);
  114. while (true) {
  115. OS::get_singleton()->delay_usec(1);
  116. DisplayServer::get_singleton()->process_events();
  117. Main::iteration();
  118. if (!FBXImporterManager::get_singleton()->is_visible()) {
  119. break;
  120. }
  121. }
  122. return false;
  123. }
  124. #endif // TOOLS_ENABLED