diff --git a/editor/import/resource_importer_scene.cpp b/editor/import/resource_importer_scene.cpp index 3c0de61d24af..779fcae63225 100644 --- a/editor/import/resource_importer_scene.cpp +++ b/editor/import/resource_importer_scene.cpp @@ -1696,7 +1696,11 @@ void ResourceImporterScene::get_import_options(const String &p_path, List importer_elem : importers) { - importer_elem->get_import_options(p_path, r_options); + List allowed_extensions; + importer_elem->get_extensions(&allowed_extensions); + if (allowed_extensions.find(p_path.get_extension().to_lower()) != nullptr) { + importer_elem->get_import_options(p_path, r_options); + } } } diff --git a/modules/gltf/config.py b/modules/gltf/config.py index 189b5a831acc..52bd7673839d 100644 --- a/modules/gltf/config.py +++ b/modules/gltf/config.py @@ -11,6 +11,7 @@ def get_doc_classes(): "EditorSceneFormatImporterBlend", "EditorSceneFormatImporterFBX", "EditorSceneFormatImporterGLTF", + "EditorSceneFormatImporterGLTFBase", "GLTFAccessor", "GLTFAnimation", "GLTFBufferView", diff --git a/modules/gltf/doc_classes/EditorSceneFormatImporterBlend.xml b/modules/gltf/doc_classes/EditorSceneFormatImporterBlend.xml index ca8eb9854f25..e487bd0ceca4 100644 --- a/modules/gltf/doc_classes/EditorSceneFormatImporterBlend.xml +++ b/modules/gltf/doc_classes/EditorSceneFormatImporterBlend.xml @@ -1,5 +1,5 @@ - + Importer for Blender's [code].blend[/code] scene file format. diff --git a/modules/gltf/doc_classes/EditorSceneFormatImporterFBX.xml b/modules/gltf/doc_classes/EditorSceneFormatImporterFBX.xml index 6754d963ff34..4bcc11bd51f3 100644 --- a/modules/gltf/doc_classes/EditorSceneFormatImporterFBX.xml +++ b/modules/gltf/doc_classes/EditorSceneFormatImporterFBX.xml @@ -1,5 +1,5 @@ - + Importer for the [code].fbx[/code] scene file format. diff --git a/modules/gltf/doc_classes/EditorSceneFormatImporterGLTF.xml b/modules/gltf/doc_classes/EditorSceneFormatImporterGLTF.xml index 5a6a2f52d947..ccbbfd623411 100644 --- a/modules/gltf/doc_classes/EditorSceneFormatImporterGLTF.xml +++ b/modules/gltf/doc_classes/EditorSceneFormatImporterGLTF.xml @@ -1,5 +1,5 @@ - + diff --git a/modules/gltf/doc_classes/EditorSceneFormatImporterGLTFBase.xml b/modules/gltf/doc_classes/EditorSceneFormatImporterGLTFBase.xml new file mode 100644 index 000000000000..bee2d77c76a2 --- /dev/null +++ b/modules/gltf/doc_classes/EditorSceneFormatImporterGLTFBase.xml @@ -0,0 +1,23 @@ + + + + Base class for all scene format importers that use glTF as intermediate representation. + + + + + + + + + + + + + + This method can be overridden to modify the glTF document or state before any parsing has taken place. + For example, this can be used to handle the glTF metadata provided in [code]extras[/code] or [code]extensions[/code] by modifying the [member GLTFDocument.extensions] passed into this method. + + + + diff --git a/modules/gltf/editor/editor_scene_importer_blend.cpp b/modules/gltf/editor/editor_scene_importer_blend.cpp index 8002c185c79e..427e8dd3c1ce 100644 --- a/modules/gltf/editor/editor_scene_importer_blend.cpp +++ b/modules/gltf/editor/editor_scene_importer_blend.cpp @@ -32,9 +32,6 @@ #ifdef TOOLS_ENABLED -#include "../gltf_document.h" -#include "../gltf_state.h" - #include "core/config/project_settings.h" #include "editor/editor_file_dialog.h" #include "editor/editor_node.h" @@ -218,24 +215,11 @@ Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path, uint32_ } // Import the generated glTF. - - // Use GLTFDocument instead of glTF importer to keep image references. - Ref gltf; - gltf.instantiate(); - Ref state; - state.instantiate(); String base_dir; if (p_options.has(SNAME("blender/materials/unpack_enabled")) && p_options[SNAME("blender/materials/unpack_enabled")]) { base_dir = sink.get_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; - } - return nullptr; - } - return gltf->generate_scene(state, p_bake_fps); + return EditorSceneFormatImporterGLTFBase::generate_gltf(p_path, sink.get_basename() + ".gltf", p_flags, p_options, p_bake_fps, base_dir, r_err); } Variant EditorSceneFormatImporterBlend::get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option, @@ -253,9 +237,6 @@ Variant EditorSceneFormatImporterBlend::get_option_visibility(const String &p_pa } void EditorSceneFormatImporterBlend::get_import_options(const String &p_path, List *r_options) { - if (p_path.get_extension().to_lower() != "blend") { - return; - } #define ADD_OPTION_BOOL(PATH, VALUE) \ r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, SNAME(PATH)), VALUE)); #define ADD_OPTION_ENUM(PATH, ENUM_HINT, VALUE) \ diff --git a/modules/gltf/editor/editor_scene_importer_blend.h b/modules/gltf/editor/editor_scene_importer_blend.h index dd1c1b9889ef..180acae6d2f7 100644 --- a/modules/gltf/editor/editor_scene_importer_blend.h +++ b/modules/gltf/editor/editor_scene_importer_blend.h @@ -34,14 +34,14 @@ #ifdef TOOLS_ENABLED #include "editor/editor_file_system.h" -#include "editor/import/resource_importer_scene.h" +#include "editor_scene_importer_gltf_base.h" class Animation; class Node; class ConfirmationDialog; -class EditorSceneFormatImporterBlend : public EditorSceneFormatImporter { - GDCLASS(EditorSceneFormatImporterBlend, EditorSceneFormatImporter); +class EditorSceneFormatImporterBlend : public EditorSceneFormatImporterGLTFBase { + GDCLASS(EditorSceneFormatImporterBlend, EditorSceneFormatImporterGLTFBase); public: enum { diff --git a/modules/gltf/editor/editor_scene_importer_fbx.cpp b/modules/gltf/editor/editor_scene_importer_fbx.cpp index faad2d315d72..d53236f2c5a0 100644 --- a/modules/gltf/editor/editor_scene_importer_fbx.cpp +++ b/modules/gltf/editor/editor_scene_importer_fbx.cpp @@ -32,9 +32,6 @@ #ifdef 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" @@ -88,30 +85,7 @@ Node *EditorSceneFormatImporterFBX::import_scene(const String &p_path, uint32_t } // 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); -} - -Variant EditorSceneFormatImporterFBX::get_option_visibility(const String &p_path, bool p_for_animation, - const String &p_option, const HashMap &p_options) { - return true; -} - -void EditorSceneFormatImporterFBX::get_import_options(const String &p_path, - List *r_options) { + return EditorSceneFormatImporterGLTFBase::generate_gltf(p_path, sink, p_flags, p_options, p_bake_fps, String(), r_err); } #endif // TOOLS_ENABLED diff --git a/modules/gltf/editor/editor_scene_importer_fbx.h b/modules/gltf/editor/editor_scene_importer_fbx.h index b0039b1c8fbe..daaad3ba8239 100644 --- a/modules/gltf/editor/editor_scene_importer_fbx.h +++ b/modules/gltf/editor/editor_scene_importer_fbx.h @@ -33,13 +33,13 @@ #ifdef TOOLS_ENABLED -#include "editor/import/resource_importer_scene.h" +#include "editor_scene_importer_gltf_base.h" class Animation; class Node; -class EditorSceneFormatImporterFBX : public EditorSceneFormatImporter { - GDCLASS(EditorSceneFormatImporterFBX, EditorSceneFormatImporter); +class EditorSceneFormatImporterFBX : public EditorSceneFormatImporterGLTFBase { + GDCLASS(EditorSceneFormatImporterFBX, EditorSceneFormatImporterGLTFBase); public: virtual uint32_t get_import_flags() const override; @@ -47,10 +47,6 @@ class EditorSceneFormatImporterFBX : public EditorSceneFormatImporter { virtual Node *import_scene(const String &p_path, uint32_t p_flags, const HashMap &p_options, int p_bake_fps, List *r_missing_deps, Error *r_err = nullptr) override; - virtual void get_import_options(const String &p_path, - List *r_options) override; - virtual Variant get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option, - const HashMap &p_options) override; }; #endif // TOOLS_ENABLED diff --git a/modules/gltf/editor/editor_scene_importer_gltf.cpp b/modules/gltf/editor/editor_scene_importer_gltf.cpp index 161808aade43..73b5c83567e4 100644 --- a/modules/gltf/editor/editor_scene_importer_gltf.cpp +++ b/modules/gltf/editor/editor_scene_importer_gltf.cpp @@ -32,9 +32,6 @@ #include "editor_scene_importer_gltf.h" -#include "../gltf_document.h" -#include "../gltf_state.h" - #include "scene/resources/animation.h" uint32_t EditorSceneFormatImporterGLTF::get_import_flags() const { @@ -49,21 +46,7 @@ void EditorSceneFormatImporterGLTF::get_extensions(List *r_extensions) c Node *EditorSceneFormatImporterGLTF::import_scene(const String &p_path, uint32_t p_flags, const HashMap &p_options, int p_bake_fps, List *r_missing_deps, Error *r_err) { - Ref doc; - doc.instantiate(); - Ref state; - state.instantiate(); - Error err = doc->append_from_file(p_path, state, p_flags, p_bake_fps); - if (err != OK) { - if (r_err) { - *r_err = err; - } - return nullptr; - } - if (p_options.has("animation/import")) { - state->set_create_animations(bool(p_options["animation/import"])); - } - return doc->generate_scene(state, p_bake_fps); + return EditorSceneFormatImporterGLTFBase::generate_gltf(p_path, p_path, p_flags, p_options, p_bake_fps, String(), r_err); } #endif // TOOLS_ENABLED diff --git a/modules/gltf/editor/editor_scene_importer_gltf.h b/modules/gltf/editor/editor_scene_importer_gltf.h index b17a1e4eaa2b..6067347fc432 100644 --- a/modules/gltf/editor/editor_scene_importer_gltf.h +++ b/modules/gltf/editor/editor_scene_importer_gltf.h @@ -33,16 +33,13 @@ #ifdef TOOLS_ENABLED -#include "../gltf_document_extension.h" -#include "../gltf_state.h" - -#include "editor/import/resource_importer_scene.h" +#include "editor_scene_importer_gltf_base.h" class Animation; class Node; -class EditorSceneFormatImporterGLTF : public EditorSceneFormatImporter { - GDCLASS(EditorSceneFormatImporterGLTF, EditorSceneFormatImporter); +class EditorSceneFormatImporterGLTF : public EditorSceneFormatImporterGLTFBase { + GDCLASS(EditorSceneFormatImporterGLTF, EditorSceneFormatImporterGLTFBase); public: virtual uint32_t get_import_flags() const override; diff --git a/modules/gltf/editor/editor_scene_importer_gltf_base.cpp b/modules/gltf/editor/editor_scene_importer_gltf_base.cpp new file mode 100644 index 000000000000..10d6f306109b --- /dev/null +++ b/modules/gltf/editor/editor_scene_importer_gltf_base.cpp @@ -0,0 +1,78 @@ +/*************************************************************************/ +/* editor_scene_importer_gltf_base.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. */ +/*************************************************************************/ + +#ifdef TOOLS_ENABLED + +#include "editor_scene_importer_gltf_base.h" + +Variant EditorSceneFormatImporterGLTFBase::get_option_visibility(const String &p_path, bool p_for_animation, + const String &p_option, const HashMap &p_options) { + return true; +} + +void EditorSceneFormatImporterGLTFBase::get_import_options(const String &p_path, List *r_options) { +} + +Node *EditorSceneFormatImporterGLTFBase::generate_gltf(const String &p_path, const String &p_gltf_path, uint32_t p_flags, + const HashMap &p_options, int p_bake_fps, + const String &p_base_dir, Error *r_err) { + // Use GLTFDocument instead of glTF importer to keep image references. + Ref gltf; + gltf.instantiate(); + Ref state; + state.instantiate(); + + // Allow augmentation (e.g. adding document extensions). + initialize_gltf(p_path, p_gltf_path, gltf, state); + + print_verbose(vformat("glTF path: %s", p_gltf_path)); + Error err = gltf->append_from_file(p_gltf_path, state, p_flags, p_bake_fps, p_base_dir); + if (err != OK) { + if (r_err) { + *r_err = FAILED; + } + return nullptr; + } + if (p_options.has("animation/import")) { + state->set_create_animations(bool(p_options["animation/import"])); + } + return gltf->generate_scene(state, p_bake_fps); +} + +void EditorSceneFormatImporterGLTFBase::initialize_gltf(const String &p_src_path, const String &p_gltf_path, + Ref p_document, Ref p_state) { + GDVIRTUAL_CALL(_initialize_gltf, p_src_path, p_gltf_path, p_document, p_state); +} + +void EditorSceneFormatImporterGLTFBase::_bind_methods() { + GDVIRTUAL_BIND(_initialize_gltf, "original_path", "gltf_path", "document", "state"); +} + +#endif // TOOLS_ENABLED diff --git a/modules/gltf/editor/editor_scene_importer_gltf_base.h b/modules/gltf/editor/editor_scene_importer_gltf_base.h new file mode 100644 index 000000000000..81e889514657 --- /dev/null +++ b/modules/gltf/editor/editor_scene_importer_gltf_base.h @@ -0,0 +1,64 @@ +/*************************************************************************/ +/* editor_scene_importer_gltf_base.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_GLTF_BASE_H +#define EDITOR_SCENE_IMPORTER_GLTF_BASE_H + +#ifdef TOOLS_ENABLED + +#include "../gltf_document.h" +#include "../gltf_state.h" + +#include "editor/import/resource_importer_scene.h" + +class EditorSceneFormatImporterGLTFBase : public EditorSceneFormatImporter { + GDCLASS(EditorSceneFormatImporterGLTFBase, EditorSceneFormatImporter); + +protected: + static void _bind_methods(); + + Node *generate_gltf(const String &p_path, const String &p_gltf_path, uint32_t p_flags, + const HashMap &p_options, int p_bake_fps, + const String &p_base_dir, Error *r_err); + + GDVIRTUAL4(_initialize_gltf, String, String, Ref, Ref) + +public: + virtual void get_import_options(const String &p_path, + List *r_options) override; + virtual Variant get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option, + const HashMap &p_options) override; + virtual void initialize_gltf(const String &p_src_path, const String &p_gltf_path, + Ref p_document, Ref p_state); +}; + +#endif // TOOLS_ENABLED + +#endif // EDITOR_SCENE_IMPORTER_GLTF_BASE_H diff --git a/modules/gltf/register_types.cpp b/modules/gltf/register_types.cpp index 1e1204aa575f..056966e0a4aa 100644 --- a/modules/gltf/register_types.cpp +++ b/modules/gltf/register_types.cpp @@ -55,6 +55,7 @@ #include "editor/editor_scene_importer_blend.h" #include "editor/editor_scene_importer_fbx.h" #include "editor/editor_scene_importer_gltf.h" +#include "editor/editor_scene_importer_gltf_base.h" #include "editor/editor_settings.h" static void _editor_init() { @@ -127,6 +128,7 @@ void initialize_gltf_module(ModuleInitializationLevel p_level) { ClassDB::APIType prev_api = ClassDB::get_current_api(); ClassDB::set_current_api(ClassDB::API_EDITOR); + GDREGISTER_VIRTUAL_CLASS(EditorSceneFormatImporterGLTFBase); GDREGISTER_CLASS(EditorSceneFormatImporterGLTF); EditorPlugins::add_by_type();