editor_scene_importer_ufbx.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**************************************************************************/
  2. /* editor_scene_importer_ufbx.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_ufbx.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "../fbx_document.h"
  33. #include "editor_scene_importer_fbx2gltf.h"
  34. #include "core/config/project_settings.h"
  35. void EditorSceneFormatImporterUFBX::get_extensions(List<String> *r_extensions) const {
  36. r_extensions->push_back("fbx");
  37. }
  38. Node *EditorSceneFormatImporterUFBX::import_scene(const String &p_path, uint32_t p_flags,
  39. const HashMap<StringName, Variant> &p_options,
  40. List<String> *r_missing_deps, Error *r_err) {
  41. // FIXME: Hack to work around GH-86309.
  42. if (p_options.has("fbx/importer") && int(p_options["fbx/importer"]) == FBX_IMPORTER_FBX2GLTF && GLOBAL_GET("filesystem/import/fbx2gltf/enabled")) {
  43. Ref<EditorSceneFormatImporterFBX2GLTF> fbx2gltf_importer;
  44. fbx2gltf_importer.instantiate();
  45. Node *scene = fbx2gltf_importer->import_scene(p_path, p_flags, p_options, r_missing_deps, r_err);
  46. if (r_err && *r_err == OK) {
  47. return scene;
  48. } else {
  49. return nullptr;
  50. }
  51. }
  52. Ref<FBXDocument> fbx;
  53. fbx.instantiate();
  54. Ref<FBXState> state;
  55. state.instantiate();
  56. print_verbose(vformat("FBX path: %s", p_path));
  57. String path = ProjectSettings::get_singleton()->globalize_path(p_path);
  58. bool allow_geometry_helper_nodes = p_options.has("fbx/allow_geometry_helper_nodes") ? (bool)p_options["fbx/allow_geometry_helper_nodes"] : false;
  59. if (allow_geometry_helper_nodes) {
  60. state->set_allow_geometry_helper_nodes(allow_geometry_helper_nodes);
  61. }
  62. if (p_options.has("fbx/embedded_image_handling")) {
  63. int32_t enum_option = p_options["fbx/embedded_image_handling"];
  64. state->set_handle_binary_image(enum_option);
  65. }
  66. if (p_options.has(SNAME("nodes/import_as_skeleton_bones")) ? (bool)p_options[SNAME("nodes/import_as_skeleton_bones")] : false) {
  67. state->set_import_as_skeleton_bones(true);
  68. }
  69. p_flags |= EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS;
  70. state->set_bake_fps(p_options["animation/fps"]);
  71. Error err = fbx->append_from_file(path, state, p_flags, p_path.get_base_dir());
  72. if (err != OK) {
  73. if (r_err) {
  74. *r_err = FAILED;
  75. }
  76. return nullptr;
  77. }
  78. return fbx->generate_scene(state, state->get_bake_fps(), (bool)p_options["animation/trimming"], false);
  79. }
  80. Variant EditorSceneFormatImporterUFBX::get_option_visibility(const String &p_path, const String &p_scene_import_type,
  81. const String &p_option, const HashMap<StringName, Variant> &p_options) {
  82. return true;
  83. }
  84. void EditorSceneFormatImporterUFBX::get_import_options(const String &p_path,
  85. List<ResourceImporter::ImportOption> *r_options) {
  86. // Returns all the options when path is empty because that means it's for the Project Settings.
  87. if (p_path.is_empty() || p_path.get_extension().to_lower() == "fbx") {
  88. r_options->push_back(ResourceImporterScene::ImportOption(PropertyInfo(Variant::INT, "fbx/importer", PROPERTY_HINT_ENUM, "ufbx,FBX2glTF"), FBX_IMPORTER_UFBX));
  89. r_options->push_back(ResourceImporterScene::ImportOption(PropertyInfo(Variant::BOOL, "fbx/allow_geometry_helper_nodes"), false));
  90. r_options->push_back(ResourceImporterScene::ImportOption(PropertyInfo(Variant::INT, "fbx/embedded_image_handling", PROPERTY_HINT_ENUM, "Discard All Textures,Extract Textures,Embed as Basis Universal,Embed as Uncompressed", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), FBXState::HANDLE_BINARY_EXTRACT_TEXTURES));
  91. }
  92. }
  93. void EditorSceneFormatImporterUFBX::handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const {
  94. if (!p_import_params.has("fbx/importer")) {
  95. p_import_params["fbx/importer"] = EditorSceneFormatImporterUFBX::FBX_IMPORTER_FBX2GLTF;
  96. }
  97. }
  98. #endif // TOOLS_ENABLED