diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-03-30 16:50:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-30 16:50:29 +0200 |
commit | bfd26b33479931598ad18cc2a218b5c49edfeb73 (patch) | |
tree | 5419b4c76884899de4dc5d2f7711142778642320 | |
parent | 20649b383bcd14991163a13933641e644e3efdb4 (diff) | |
parent | 0a6889d834f44b8875a2a7969812824399764abb (diff) |
Merge pull request #59653 from V-Sekai/fbx-import
-rw-r--r-- | doc/classes/ProjectSettings.xml | 8 | ||||
-rw-r--r-- | modules/gltf/config.py | 1 | ||||
-rw-r--r-- | modules/gltf/doc_classes/EditorSceneFormatImporterBlend.xml | 2 | ||||
-rw-r--r-- | modules/gltf/doc_classes/EditorSceneFormatImporterFBX.xml | 13 | ||||
-rw-r--r-- | modules/gltf/editor/editor_scene_importer_blend.cpp | 37 | ||||
-rw-r--r-- | modules/gltf/editor/editor_scene_importer_fbx.cpp | 119 | ||||
-rw-r--r-- | modules/gltf/editor/editor_scene_importer_fbx.h | 60 | ||||
-rw-r--r-- | modules/gltf/gltf_mesh.cpp | 1 | ||||
-rw-r--r-- | modules/gltf/gltf_mesh.h | 1 | ||||
-rw-r--r-- | modules/gltf/register_types.cpp | 30 |
10 files changed, 241 insertions, 31 deletions
diff --git a/doc/classes/ProjectSettings.xml b/doc/classes/ProjectSettings.xml index 6f85ed5b38..ab36c9473c 100644 --- a/doc/classes/ProjectSettings.xml +++ b/doc/classes/ProjectSettings.xml @@ -553,10 +553,14 @@ <member name="editor/script/templates_search_path" type="String" setter="" getter="" default=""res://script_templates""> Search path for project-specific script templates. Godot will search for script templates both in the editor-specific path and in this project-specific path. </member> - <member name="filesystem/import/blend/enabled" type="bool" setter="" getter="" default="false"> - If [code]true[/code], 3D scene files with the [code].blend[/code] extension will be imported by converting them to glTF 2.0. + <member name="filesystem/import/blend/enabled" type="bool" setter="" getter="" default="true"> + If [code]true[/code], Blender 3D scene files with the [code].blend[/code] extension will be imported by converting them to glTF 2.0. This requires configuring a path to a Blender executable in the editor settings at [code]filesystem/import/blend/blender_path[/code]. Blender 3.0 or later is required. </member> + <member name="filesystem/import/fbx/enabled" type="bool" setter="" getter="" default="true"> + If [code]true[/code], Autodesk FBX 3D scene files with the [code].fbx[/code] extension will be imported by converting them to glTF 2.0. + This requires configuring a path to a FBX2glTF executable in the editor settings at [code]filesystem/import/fbx/fbx2gltf_path[/code]. + </member> <member name="gui/common/default_scroll_deadzone" type="int" setter="" getter="" default="0"> Default value for [member ScrollContainer.scroll_deadzone], which will be used for all [ScrollContainer]s unless overridden. </member> diff --git a/modules/gltf/config.py b/modules/gltf/config.py index 4ff2a6db38..189b5a831a 100644 --- a/modules/gltf/config.py +++ b/modules/gltf/config.py @@ -9,6 +9,7 @@ def configure(env): def get_doc_classes(): return [ "EditorSceneFormatImporterBlend", + "EditorSceneFormatImporterFBX", "EditorSceneFormatImporterGLTF", "GLTFAccessor", "GLTFAnimation", diff --git a/modules/gltf/doc_classes/EditorSceneFormatImporterBlend.xml b/modules/gltf/doc_classes/EditorSceneFormatImporterBlend.xml index b3a0381efc..9c6c8e03c4 100644 --- a/modules/gltf/doc_classes/EditorSceneFormatImporterBlend.xml +++ b/modules/gltf/doc_classes/EditorSceneFormatImporterBlend.xml @@ -6,7 +6,7 @@ <description> Imports Blender scenes in the [code].blend[/code] file format through the glTF 2.0 3D import pipeline. This importer requires Blender to be installed by the user, so that it can be used to export the scene as glTF 2.0. The location of the Blender binary is set via the [code]filesystem/import/blend/blender_path[/code] editor setting. - This importer is only used if [member ProjectSettings.filesystem/import/blend/enabled] is enabled, otherwise [code].blend[/code] present in the project folder are not imported. + This importer is only used if [member ProjectSettings.filesystem/import/blend/enabled] is enabled, otherwise [code].blend[/code] files present in the project folder are not imported. Blend import requires Blender 3.0. Internally, the EditorSceneFormatImporterBlend uses the Blender glTF "Use Original" mode to reference external textures. </description> diff --git a/modules/gltf/doc_classes/EditorSceneFormatImporterFBX.xml b/modules/gltf/doc_classes/EditorSceneFormatImporterFBX.xml new file mode 100644 index 0000000000..6754d963ff --- /dev/null +++ b/modules/gltf/doc_classes/EditorSceneFormatImporterFBX.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<class name="EditorSceneFormatImporterFBX" inherits="EditorSceneFormatImporter" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd"> + <brief_description> + Importer for the [code].fbx[/code] scene file format. + </brief_description> + <description> + Imports Autodesk FBX 3D scenes by way of converting them to glTF 2.0 using the FBX2glTF command line tool. + The location of the FBX2glTF binary is set via the [code]filesystem/import/fbx/fbx2gltf_path[/code] editor setting. + This importer is only used if [member ProjectSettings.filesystem/import/fbx/enabled] is enabled, otherwise [code].fbx[/code] files present in the project folder are not imported. + </description> + <tutorials> + </tutorials> +</class> 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<String> *r_extensions) Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path, uint32_t p_flags, const Map<StringName, Variant> &p_options, int p_bake_fps, List<String> *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; - 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<GLTFDocument> gltf; gltf.instantiate(); Ref<GLTFState> 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<String> *r_extensions) const { + r_extensions->push_back("fbx"); +} + +Node *EditorSceneFormatImporterFBX::import_scene(const String &p_path, uint32_t p_flags, + const Map<StringName, Variant> &p_options, int p_bake_fps, + List<String> *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<String> 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<GLTFDocument> gltf; + gltf.instantiate(); + Ref<GLTFState> 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<Animation> EditorSceneFormatImporterFBX::import_animation(const String &p_path, + uint32_t p_flags, const Map<StringName, Variant> &p_options, int p_bake_fps) { + return Ref<Animation>(); +} + +Variant EditorSceneFormatImporterFBX::get_option_visibility(const String &p_path, + const String &p_option, const Map<StringName, Variant> &p_options) { + return true; +} + +void EditorSceneFormatImporterFBX::get_import_options(const String &p_path, + List<ResourceImporter::ImportOption> *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<String> *r_extensions) const override; + virtual Node *import_scene(const String &p_path, uint32_t p_flags, + const Map<StringName, Variant> &p_options, int p_bake_fps, + List<String> *r_missing_deps, Error *r_err = nullptr) override; + virtual Ref<Animation> import_animation(const String &p_path, uint32_t p_flags, + const Map<StringName, Variant> &p_options, int p_bake_fps) override; + virtual void get_import_options(const String &p_path, + List<ResourceImporter::ImportOption> *r_options) override; + virtual Variant get_option_visibility(const String &p_path, const String &p_option, + const Map<StringName, Variant> &p_options) override; +}; + +#endif // TOOLS_ENABLED + +#endif // EDITOR_SCENE_IMPORTER_FBX_H diff --git a/modules/gltf/gltf_mesh.cpp b/modules/gltf/gltf_mesh.cpp index 1251622642..3add8304b1 100644 --- a/modules/gltf/gltf_mesh.cpp +++ b/modules/gltf/gltf_mesh.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "gltf_mesh.h" + #include "scene/resources/importer_mesh.h" void GLTFMesh::_bind_methods() { diff --git a/modules/gltf/gltf_mesh.h b/modules/gltf/gltf_mesh.h index 4a0533933a..dc26120b48 100644 --- a/modules/gltf/gltf_mesh.h +++ b/modules/gltf/gltf_mesh.h @@ -55,4 +55,5 @@ public: Array get_instance_materials(); void set_instance_materials(Array p_instance_materials); }; + #endif // GLTF_MESH_H diff --git a/modules/gltf/register_types.cpp b/modules/gltf/register_types.cpp index 1669d40269..79b918dbc0 100644 --- a/modules/gltf/register_types.cpp +++ b/modules/gltf/register_types.cpp @@ -53,6 +53,7 @@ #include "editor/editor_node.h" #include "editor/editor_scene_exporter_gltf_plugin.h" #include "editor/editor_scene_importer_blend.h" +#include "editor/editor_scene_importer_fbx.h" #include "editor/editor_scene_importer_gltf.h" #include "editor/editor_settings.h" @@ -62,6 +63,7 @@ static void _editor_init() { ResourceImporterScene::get_singleton()->add_importer(import_gltf); // Blend to glTF importer. + bool blend_enabled = GLOBAL_GET("filesystem/import/blend/enabled"); // Defined here because EditorSettings doesn't exist in `register_gltf_types` yet. String blender_path = EDITOR_DEF_RST("filesystem/import/blend/blender_path", ""); @@ -79,6 +81,26 @@ static void _editor_init() { ResourceImporterScene::get_singleton()->add_importer(importer); } } + + // FBX to glTF importer. + + bool fbx_enabled = GLOBAL_GET("filesystem/import/fbx/enabled"); + // Defined here because EditorSettings doesn't exist in `register_gltf_types` yet. + String fbx2gltf_path = EDITOR_DEF_RST("filesystem/import/fbx/fbx2gltf_path", ""); + EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, + "filesystem/import/fbx/fbx2gltf_path", PROPERTY_HINT_GLOBAL_FILE)); + if (fbx_enabled) { + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + if (fbx2gltf_path.is_empty()) { + WARN_PRINT("FBX file import is enabled, but no FBX2glTF path is configured. FBX files will not be imported."); + } else if (!da->file_exists(fbx2gltf_path)) { + WARN_PRINT("FBX file import is enabled, but the FBX2glTF path doesn't point to a valid FBX2glTF executable. FBX files will not be imported."); + } else { + Ref<EditorSceneFormatImporterFBX> importer; + importer.instantiate(); + ResourceImporterScene::get_singleton()->add_importer(importer); + } + } } #endif // TOOLS_ENABLED @@ -92,6 +114,7 @@ void register_gltf_types() { GDREGISTER_CLASS(GLTFDocumentExtension); GDREGISTER_CLASS(GLTFDocumentExtensionConvertImporterMesh); GDREGISTER_CLASS(GLTFLight); + GDREGISTER_CLASS(GLTFMesh); GDREGISTER_CLASS(GLTFNode); GDREGISTER_CLASS(GLTFSkeleton); GDREGISTER_CLASS(GLTFSkin); @@ -104,13 +127,14 @@ void register_gltf_types() { ClassDB::APIType prev_api = ClassDB::get_current_api(); ClassDB::set_current_api(ClassDB::API_EDITOR); - GDREGISTER_CLASS(GLTFMesh); GDREGISTER_CLASS(EditorSceneFormatImporterGLTF); EditorPlugins::add_by_type<SceneExporterGLTFPlugin>(); - // Blend to glTF importer. - GLOBAL_DEF_RST("filesystem/import/blend/enabled", false); // Defined here to catch in docs. + // Project settings defined here so doctool finds them. + GLOBAL_DEF_RST("filesystem/import/blend/enabled", true); + GLOBAL_DEF_RST("filesystem/import/fbx/enabled", true); GDREGISTER_CLASS(EditorSceneFormatImporterBlend); + GDREGISTER_CLASS(EditorSceneFormatImporterFBX); ClassDB::set_current_api(prev_api); EditorNode::add_init_callback(_editor_init); |