From 0a6889d834f44b8875a2a7969812824399764abb Mon Sep 17 00:00:00 2001 From: "K. S. Ernest (iFire) Lee" Date: Mon, 13 Dec 2021 21:34:18 -0800 Subject: Add fbx2gltf support for importing .fbx files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lets you drag or place .fbx files in the project folder and it will import the files. An editor setting sets the location of the fbx2gltf binary. Enables .fbx and .blend by default. Co-authored-by: RĂ©mi Verschelde --- .../gltf/editor/editor_scene_importer_blend.cpp | 37 +++---- modules/gltf/editor/editor_scene_importer_fbx.cpp | 119 +++++++++++++++++++++ modules/gltf/editor/editor_scene_importer_fbx.h | 60 +++++++++++ 3 files changed, 191 insertions(+), 25 deletions(-) create mode 100644 modules/gltf/editor/editor_scene_importer_fbx.cpp create mode 100644 modules/gltf/editor/editor_scene_importer_fbx.h (limited to 'modules/gltf/editor') diff --git a/modules/gltf/editor/editor_scene_importer_blend.cpp b/modules/gltf/editor/editor_scene_importer_blend.cpp index 931b2bc73b..2587c095e1 100644 --- a/modules/gltf/editor/editor_scene_importer_blend.cpp +++ b/modules/gltf/editor/editor_scene_importer_blend.cpp @@ -36,7 +36,6 @@ #include "../gltf_state.h" #include "core/config/project_settings.h" -#include "core/io/json.h" #include "editor/editor_settings.h" #include "scene/main/node.h" #include "scene/resources/animation.h" @@ -52,26 +51,12 @@ void EditorSceneFormatImporterBlend::get_extensions(List *r_extensions) Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path, uint32_t p_flags, const Map &p_options, int p_bake_fps, List *r_missing_deps, Error *r_err) { - // Parse JSON config. + // Get global paths for source and sink. - const String source_global = ProjectSettings::get_singleton()->globalize_path(p_path).c_escape(); + const String source_global = ProjectSettings::get_singleton()->globalize_path(p_path); const String sink = ProjectSettings::get_singleton()->get_imported_files_path().plus_file( vformat("%s-%s.gltf", p_path.get_file().get_basename(), p_path.md5_text())); - const String sink_global = ProjectSettings::get_singleton()->globalize_path(sink).c_escape(); - const String json_parameters = vformat("{\"source\": \"%s\", \"sink\": \"%s\"}", source_global, sink_global); - - Ref json; - json.instantiate(); - Error err = json->parse(json_parameters); - if (err != OK) { - if (r_err) { - *r_err = err; - } - ERR_PRINT(vformat("Blend config can't be read at line %s with error: %s", - json->get_error_line(), json->get_error_message())); - return nullptr; - } - Dictionary parameters = json->get_data(); + const String sink_global = ProjectSettings::get_singleton()->globalize_path(sink); // Handle configuration options. @@ -170,14 +155,14 @@ Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path, uint32_ // Prepare Blender export script. - String common_args = vformat("filepath='%s',", parameters["sink"]) + + String common_args = vformat("filepath='%s',", sink_global) + "export_format='GLTF_SEPARATE'," "export_yup=True," + parameters_arg; String script = String("import bpy, sys;") + "print('Blender 3.0 or higher is required.', file=sys.stderr) if bpy.app.version < (3, 0, 0) else None;" + - vformat("bpy.ops.wm.open_mainfile(filepath='%s');", parameters["source"]) + + vformat("bpy.ops.wm.open_mainfile(filepath='%s');", source_global) + unpack_all + vformat("bpy.ops.export_scene.gltf(export_keep_originals=True,%s);", common_args); print_verbose(script); @@ -192,20 +177,22 @@ Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path, uint32_ args.push_back(script); String standard_out; - int32_t ret = OS::get_singleton()->execute(blender_path, args, &standard_out, &ret, true); + int ret; + OS::get_singleton()->execute(blender_path, args, &standard_out, &ret, true); + print_verbose(blender_path); print_verbose(standard_out); - if (ret != OK) { + if (ret != 0) { if (r_err) { *r_err = ERR_SCRIPT_FAILED; } - ERR_PRINT(vformat("Blend import failed with error: %d.", ret)); + ERR_PRINT(vformat("Blend export to glTF failed with error: %d.", ret)); return nullptr; } // Import the generated glTF. - // Use GLTFDocument instead of gltf importer to keep image references. + // Use GLTFDocument instead of glTF importer to keep image references. Ref gltf; gltf.instantiate(); Ref state; @@ -214,7 +201,7 @@ Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path, uint32_ if (p_options.has(SNAME("blender/materials/unpack_enabled")) && p_options[SNAME("blender/materials/unpack_enabled")]) { base_dir = sink.get_base_dir(); } - err = gltf->append_from_file(sink.get_basename() + ".gltf", state, p_flags, p_bake_fps, base_dir); + Error err = gltf->append_from_file(sink.get_basename() + ".gltf", state, p_flags, p_bake_fps, base_dir); if (err != OK) { if (r_err) { *r_err = FAILED; diff --git a/modules/gltf/editor/editor_scene_importer_fbx.cpp b/modules/gltf/editor/editor_scene_importer_fbx.cpp new file mode 100644 index 0000000000..7cfd85c73a --- /dev/null +++ b/modules/gltf/editor/editor_scene_importer_fbx.cpp @@ -0,0 +1,119 @@ +/*************************************************************************/ +/* editor_scene_importer_fbx.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "editor_scene_importer_fbx.h" + +#if TOOLS_ENABLED + +#include "../gltf_document.h" +#include "../gltf_state.h" + +#include "core/config/project_settings.h" +#include "editor/editor_settings.h" +#include "scene/main/node.h" +#include "scene/resources/animation.h" + +uint32_t EditorSceneFormatImporterFBX::get_import_flags() const { + return ImportFlags::IMPORT_SCENE | ImportFlags::IMPORT_ANIMATION; +} + +void EditorSceneFormatImporterFBX::get_extensions(List *r_extensions) const { + r_extensions->push_back("fbx"); +} + +Node *EditorSceneFormatImporterFBX::import_scene(const String &p_path, uint32_t p_flags, + const Map &p_options, int p_bake_fps, + List *r_missing_deps, Error *r_err) { + // Get global paths for source and sink. + + const String source_global = ProjectSettings::get_singleton()->globalize_path(p_path); + const String sink = ProjectSettings::get_singleton()->get_imported_files_path().plus_file( + vformat("%s-%s.glb", p_path.get_file().get_basename(), p_path.md5_text())); + const String sink_global = ProjectSettings::get_singleton()->globalize_path(sink); + + // Run fbx2gltf. + + String fbx2gltf_path = EDITOR_GET("filesystem/import/fbx/fbx2gltf_path"); + + List args; + args.push_back("--pbr-metallic-roughness"); + args.push_back("--input"); + args.push_back(vformat("\"%s\"", source_global)); + args.push_back("--output"); + args.push_back(vformat("\"%s\"", sink_global)); + args.push_back("--binary"); + + String standard_out; + int ret; + OS::get_singleton()->execute(fbx2gltf_path, args, &standard_out, &ret, true); + print_verbose(fbx2gltf_path); + print_verbose(standard_out); + + if (ret != 0) { + if (r_err) { + *r_err = ERR_SCRIPT_FAILED; + } + ERR_PRINT(vformat("FBX conversion to glTF failed with error: %d.", ret)); + return nullptr; + } + + // Import the generated glTF. + + // Use GLTFDocument instead of glTF importer to keep image references. + Ref gltf; + gltf.instantiate(); + Ref state; + state.instantiate(); + print_verbose(vformat("glTF path: %s", sink)); + Error err = gltf->append_from_file(sink, state, p_flags, p_bake_fps); + if (err != OK) { + if (r_err) { + *r_err = FAILED; + } + return nullptr; + } + return gltf->generate_scene(state, p_bake_fps); +} + +Ref EditorSceneFormatImporterFBX::import_animation(const String &p_path, + uint32_t p_flags, const Map &p_options, int p_bake_fps) { + return Ref(); +} + +Variant EditorSceneFormatImporterFBX::get_option_visibility(const String &p_path, + const String &p_option, const Map &p_options) { + return true; +} + +void EditorSceneFormatImporterFBX::get_import_options(const String &p_path, + List *r_options) { +} + +#endif // TOOLS_ENABLED diff --git a/modules/gltf/editor/editor_scene_importer_fbx.h b/modules/gltf/editor/editor_scene_importer_fbx.h new file mode 100644 index 0000000000..bb69cfaa30 --- /dev/null +++ b/modules/gltf/editor/editor_scene_importer_fbx.h @@ -0,0 +1,60 @@ +/*************************************************************************/ +/* editor_scene_importer_fbx.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef EDITOR_SCENE_IMPORTER_FBX_H +#define EDITOR_SCENE_IMPORTER_FBX_H + +#ifdef TOOLS_ENABLED + +#include "editor/import/resource_importer_scene.h" + +class Animation; +class Node; + +class EditorSceneFormatImporterFBX : public EditorSceneFormatImporter { + GDCLASS(EditorSceneFormatImporterFBX, EditorSceneFormatImporter); + +public: + virtual uint32_t get_import_flags() const override; + virtual void get_extensions(List *r_extensions) const override; + virtual Node *import_scene(const String &p_path, uint32_t p_flags, + const Map &p_options, int p_bake_fps, + List *r_missing_deps, Error *r_err = nullptr) override; + virtual Ref import_animation(const String &p_path, uint32_t p_flags, + const Map &p_options, int p_bake_fps) override; + virtual void get_import_options(const String &p_path, + List *r_options) override; + virtual Variant get_option_visibility(const String &p_path, const String &p_option, + const Map &p_options) override; +}; + +#endif // TOOLS_ENABLED + +#endif // EDITOR_SCENE_IMPORTER_FBX_H -- cgit v1.2.3