Skip to content

Commit

Permalink
Allow intercepting GLTFDocument on scene import
Browse files Browse the repository at this point in the history
- Create EditorSceneImporterGLTFBase, which is how all scene importers
  that use glTF as backing format can share behavior.
- Allow user to intercept creation of GLTFDocument with virtual method
  _initialize_gltf, for example to conditionally add GLTFDocumentExtensions
- De-duplicate file extension check by moving it into the
  ResourceImporterScene class.
  • Loading branch information
RedMser committed Aug 10, 2022
1 parent 106b680 commit 3cd52ad
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 85 deletions.
6 changes: 5 additions & 1 deletion editor/import/resource_importer_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,11 @@ void ResourceImporterScene::get_import_options(const String &p_path, List<Import
}

for (Ref<EditorSceneFormatImporter> importer_elem : importers) {
importer_elem->get_import_options(p_path, r_options);
List<String> 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);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions modules/gltf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def get_doc_classes():
"EditorSceneFormatImporterBlend",
"EditorSceneFormatImporterFBX",
"EditorSceneFormatImporterGLTF",
"EditorSceneFormatImporterGLTFBase",
"GLTFAccessor",
"GLTFAnimation",
"GLTFBufferView",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="EditorSceneFormatImporterBlend" inherits="EditorSceneFormatImporter" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<class name="EditorSceneFormatImporterBlend" inherits="EditorSceneFormatImporterGLTFBase" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Importer for Blender's [code].blend[/code] scene file format.
</brief_description>
Expand Down
2 changes: 1 addition & 1 deletion modules/gltf/doc_classes/EditorSceneFormatImporterFBX.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?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">
<class name="EditorSceneFormatImporterFBX" inherits="EditorSceneFormatImporterGLTFBase" 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>
Expand Down
2 changes: 1 addition & 1 deletion modules/gltf/doc_classes/EditorSceneFormatImporterGLTF.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="EditorSceneFormatImporterGLTF" inherits="EditorSceneFormatImporter" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<class name="EditorSceneFormatImporterGLTF" inherits="EditorSceneFormatImporterGLTFBase" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
</brief_description>
<description>
Expand Down
23 changes: 23 additions & 0 deletions modules/gltf/doc_classes/EditorSceneFormatImporterGLTFBase.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="EditorSceneFormatImporterGLTFBase" inherits="EditorSceneFormatImporter" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Base class for all scene format importers that use glTF as intermediate representation.
</brief_description>
<description>
</description>
<tutorials>
</tutorials>
<methods>
<method name="_initialize_gltf" qualifiers="virtual">
<return type="void" />
<param index="0" name="original_path" type="String" />
<param index="1" name="gltf_path" type="String" />
<param index="2" name="document" type="GLTFDocument" />
<param index="3" name="state" type="GLTFState" />
<description>
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.
</description>
</method>
</methods>
</class>
21 changes: 1 addition & 20 deletions modules/gltf/editor/editor_scene_importer_blend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<GLTFDocument> gltf;
gltf.instantiate();
Ref<GLTFState> 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,
Expand All @@ -253,9 +237,6 @@ Variant EditorSceneFormatImporterBlend::get_option_visibility(const String &p_pa
}

void EditorSceneFormatImporterBlend::get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *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) \
Expand Down
6 changes: 3 additions & 3 deletions modules/gltf/editor/editor_scene_importer_blend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 1 addition & 27 deletions modules/gltf/editor/editor_scene_importer_fbx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<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);
}

Variant EditorSceneFormatImporterFBX::get_option_visibility(const String &p_path, bool p_for_animation,
const String &p_option, const HashMap<StringName, Variant> &p_options) {
return true;
}

void EditorSceneFormatImporterFBX::get_import_options(const String &p_path,
List<ResourceImporter::ImportOption> *r_options) {
return EditorSceneFormatImporterGLTFBase::generate_gltf(p_path, sink, p_flags, p_options, p_bake_fps, String(), r_err);
}

#endif // TOOLS_ENABLED
10 changes: 3 additions & 7 deletions modules/gltf/editor/editor_scene_importer_fbx.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,20 @@

#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;
virtual void get_extensions(List<String> *r_extensions) const override;
virtual Node *import_scene(const String &p_path, uint32_t p_flags,
const HashMap<StringName, Variant> &p_options, int p_bake_fps,
List<String> *r_missing_deps, Error *r_err = nullptr) 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, bool p_for_animation, const String &p_option,
const HashMap<StringName, Variant> &p_options) override;
};

#endif // TOOLS_ENABLED
Expand Down
19 changes: 1 addition & 18 deletions modules/gltf/editor/editor_scene_importer_gltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -49,21 +46,7 @@ void EditorSceneFormatImporterGLTF::get_extensions(List<String> *r_extensions) c
Node *EditorSceneFormatImporterGLTF::import_scene(const String &p_path, uint32_t p_flags,
const HashMap<StringName, Variant> &p_options, int p_bake_fps,
List<String> *r_missing_deps, Error *r_err) {
Ref<GLTFDocument> doc;
doc.instantiate();
Ref<GLTFState> 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
9 changes: 3 additions & 6 deletions modules/gltf/editor/editor_scene_importer_gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
78 changes: 78 additions & 0 deletions modules/gltf/editor/editor_scene_importer_gltf_base.cpp
Original file line number Diff line number Diff line change
@@ -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<StringName, Variant> &p_options) {
return true;
}

void EditorSceneFormatImporterGLTFBase::get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options) {
}

Node *EditorSceneFormatImporterGLTFBase::generate_gltf(const String &p_path, const String &p_gltf_path, uint32_t p_flags,
const HashMap<StringName, Variant> &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<GLTFDocument> gltf;
gltf.instantiate();
Ref<GLTFState> 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<GLTFDocument> p_document, Ref<GLTFState> 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
64 changes: 64 additions & 0 deletions modules/gltf/editor/editor_scene_importer_gltf_base.h
Original file line number Diff line number Diff line change
@@ -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<StringName, Variant> &p_options, int p_bake_fps,
const String &p_base_dir, Error *r_err);

GDVIRTUAL4(_initialize_gltf, String, String, Ref<GLTFDocument>, Ref<GLTFState>)

public:
virtual void get_import_options(const String &p_path,
List<ResourceImporter::ImportOption> *r_options) override;
virtual Variant get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option,
const HashMap<StringName, Variant> &p_options) override;
virtual void initialize_gltf(const String &p_src_path, const String &p_gltf_path,
Ref<GLTFDocument> p_document, Ref<GLTFState> p_state);
};

#endif // TOOLS_ENABLED

#endif // EDITOR_SCENE_IMPORTER_GLTF_BASE_H
Loading

0 comments on commit 3cd52ad

Please sign in to comment.