editor_scene_importer_fbx.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "../gltf_state.h"
  34. #include "core/config/project_settings.h"
  35. #include "editor/editor_settings.h"
  36. #include "scene/main/node.h"
  37. #include "scene/resources/animation.h"
  38. uint32_t EditorSceneFormatImporterFBX::get_import_flags() const {
  39. return ImportFlags::IMPORT_SCENE | ImportFlags::IMPORT_ANIMATION;
  40. }
  41. void EditorSceneFormatImporterFBX::get_extensions(List<String> *r_extensions) const {
  42. r_extensions->push_back("fbx");
  43. }
  44. Node *EditorSceneFormatImporterFBX::import_scene(const String &p_path, uint32_t p_flags,
  45. const HashMap<StringName, Variant> &p_options, int p_bake_fps,
  46. List<String> *r_missing_deps, Error *r_err) {
  47. // Get global paths for source and sink.
  48. // Don't use `c_escape()` as it can generate broken paths. These paths will be
  49. // enclosed in double quotes by OS::execute(), so we only need to escape those.
  50. // `c_escape_multiline()` seems to do this (escapes `\` and `"` only).
  51. const String source_global = ProjectSettings::get_singleton()->globalize_path(p_path).c_escape_multiline();
  52. const String sink = ProjectSettings::get_singleton()->get_imported_files_path().path_join(
  53. vformat("%s-%s.glb", p_path.get_file().get_basename(), p_path.md5_text()));
  54. const String sink_global = ProjectSettings::get_singleton()->globalize_path(sink).c_escape_multiline();
  55. // Run fbx2gltf.
  56. String fbx2gltf_path = EDITOR_GET("filesystem/import/fbx/fbx2gltf_path");
  57. List<String> args;
  58. args.push_back("--pbr-metallic-roughness");
  59. args.push_back("--input");
  60. args.push_back(source_global);
  61. args.push_back("--output");
  62. args.push_back(sink_global);
  63. args.push_back("--binary");
  64. String standard_out;
  65. int ret;
  66. OS::get_singleton()->execute(fbx2gltf_path, args, &standard_out, &ret, true);
  67. print_verbose(fbx2gltf_path);
  68. print_verbose(standard_out);
  69. if (ret != 0) {
  70. if (r_err) {
  71. *r_err = ERR_SCRIPT_FAILED;
  72. }
  73. ERR_PRINT(vformat("FBX conversion to glTF failed with error: %d.", ret));
  74. return nullptr;
  75. }
  76. // Import the generated glTF.
  77. // Use GLTFDocument instead of glTF importer to keep image references.
  78. Ref<GLTFDocument> gltf;
  79. gltf.instantiate();
  80. Ref<GLTFState> state;
  81. state.instantiate();
  82. print_verbose(vformat("glTF path: %s", sink));
  83. Error err = gltf->append_from_file(sink, state, p_flags, p_bake_fps);
  84. if (err != OK) {
  85. if (r_err) {
  86. *r_err = FAILED;
  87. }
  88. return nullptr;
  89. }
  90. return gltf->generate_scene(state, p_bake_fps);
  91. }
  92. Variant EditorSceneFormatImporterFBX::get_option_visibility(const String &p_path, bool p_for_animation,
  93. const String &p_option, const HashMap<StringName, Variant> &p_options) {
  94. return true;
  95. }
  96. void EditorSceneFormatImporterFBX::get_import_options(const String &p_path,
  97. List<ResourceImporter::ImportOption> *r_options) {
  98. }
  99. #endif // TOOLS_ENABLED