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