editor_scene_importer_fbx2gltf.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**************************************************************************/
  2. /* editor_scene_importer_fbx2gltf.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_fbx2gltf.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "core/config/project_settings.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor_scene_importer_ufbx.h"
  35. #include "modules/gltf/gltf_document.h"
  36. void EditorSceneFormatImporterFBX2GLTF::get_extensions(List<String> *r_extensions) const {
  37. r_extensions->push_back("fbx");
  38. }
  39. Node *EditorSceneFormatImporterFBX2GLTF::import_scene(const String &p_path, uint32_t p_flags,
  40. const HashMap<StringName, Variant> &p_options,
  41. List<String> *r_missing_deps, Error *r_err) {
  42. // FIXME: Hack to work around GH-86309.
  43. if (p_options.has("fbx/importer") && int(p_options["fbx/importer"]) == EditorSceneFormatImporterUFBX::FBX_IMPORTER_UFBX) {
  44. Ref<EditorSceneFormatImporterUFBX> fbx2gltf_importer;
  45. fbx2gltf_importer.instantiate();
  46. Node *scene = fbx2gltf_importer->import_scene(p_path, p_flags, p_options, r_missing_deps, r_err);
  47. if (r_err && *r_err == OK) {
  48. return scene;
  49. } else {
  50. return nullptr;
  51. }
  52. }
  53. // Get global paths for source and sink.
  54. // Don't use `c_escape()` as it can generate broken paths. These paths will be
  55. // enclosed in double quotes by OS::execute(), so we only need to escape those.
  56. // `c_escape_multiline()` seems to do this (escapes `\` and `"` only).
  57. const String source_global = ProjectSettings::get_singleton()->globalize_path(p_path).c_escape_multiline();
  58. const String sink = ProjectSettings::get_singleton()->get_imported_files_path().path_join(
  59. vformat("%s-%s.glb", p_path.get_file().get_basename(), p_path.md5_text()));
  60. const String sink_global = ProjectSettings::get_singleton()->globalize_path(sink).c_escape_multiline();
  61. // Run fbx2gltf.
  62. String fbx2gltf_path = EDITOR_GET("filesystem/import/fbx/fbx2gltf_path");
  63. List<String> args;
  64. args.push_back("--pbr-metallic-roughness");
  65. args.push_back("--input");
  66. args.push_back(source_global);
  67. args.push_back("--output");
  68. args.push_back(sink_global);
  69. args.push_back("--binary");
  70. String standard_out;
  71. int ret;
  72. OS::get_singleton()->execute(fbx2gltf_path, args, &standard_out, &ret, true);
  73. print_verbose(fbx2gltf_path);
  74. print_verbose(standard_out);
  75. if (ret != 0) {
  76. if (r_err) {
  77. *r_err = ERR_SCRIPT_FAILED;
  78. }
  79. ERR_PRINT(vformat("FBX conversion to glTF failed with error: %d.", ret));
  80. return nullptr;
  81. }
  82. // Import the generated glTF.
  83. // Use GLTFDocument instead of glTF importer to keep image references.
  84. Ref<GLTFDocument> gltf;
  85. gltf.instantiate();
  86. Ref<GLTFState> state;
  87. state.instantiate();
  88. if (p_options.has(SNAME("nodes/import_as_skeleton_bones")) ? (bool)p_options[SNAME("nodes/import_as_skeleton_bones")] : false) {
  89. state->set_import_as_skeleton_bones(true);
  90. }
  91. print_verbose(vformat("glTF path: %s", sink));
  92. Error err = gltf->append_from_file(sink, state, p_flags, p_path.get_base_dir());
  93. if (err != OK) {
  94. if (r_err) {
  95. *r_err = FAILED;
  96. }
  97. return nullptr;
  98. }
  99. #ifndef DISABLE_DEPRECATED
  100. bool trimming = p_options.has("animation/trimming") ? (bool)p_options["animation/trimming"] : false;
  101. return gltf->generate_scene(state, (float)p_options["animation/fps"], trimming, false);
  102. #else
  103. return gltf->generate_scene(state, (float)p_options["animation/fps"], (bool)p_options["animation/trimming"], false);
  104. #endif
  105. }
  106. Variant EditorSceneFormatImporterFBX2GLTF::get_option_visibility(const String &p_path, const String &p_scene_import_type,
  107. const String &p_option, const HashMap<StringName, Variant> &p_options) {
  108. // Remove all the FBX options except for 'fbx/importer' if the importer is fbx2gltf.
  109. // These options are available only for ufbx.
  110. if (p_option.begins_with("fbx/") && p_option != "fbx/importer" && p_options.has("fbx/importer") && int(p_options["fbx/importer"]) == EditorSceneFormatImporterUFBX::FBX_IMPORTER_FBX2GLTF) {
  111. return false;
  112. }
  113. return true;
  114. }
  115. #define ADD_OPTION_ENUM(PATH, ENUM_HINT, VALUE) \
  116. r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::INT, SNAME(PATH), PROPERTY_HINT_ENUM, ENUM_HINT), VALUE));
  117. void EditorSceneFormatImporterFBX2GLTF::get_import_options(const String &p_path,
  118. List<ResourceImporter::ImportOption> *r_options) {
  119. }
  120. void EditorSceneFormatImporterFBX2GLTF::handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const {
  121. if (!p_import_params.has("fbx/importer")) {
  122. p_import_params["fbx/importer"] = EditorSceneFormatImporterUFBX::FBX_IMPORTER_UFBX;
  123. }
  124. }
  125. #endif // TOOLS_ENABLED