Debian Codegraph @

modules/fbx/editor/editor_scene_importer_fbx2gltf.cpp

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