1/**************************************************************************/
2/* register_types.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 "register_types.h"
32
33#include "extensions/gltf_document_extension_convert_importer_mesh.h"
34#include "extensions/gltf_document_extension_texture_ktx.h"
35#include "extensions/gltf_document_extension_texture_webp.h"
36#include "extensions/gltf_spec_gloss.h"
37#include "extensions/physics/gltf_document_extension_physics.h"
38#include "gltf_document.h"
39
40#ifdef TOOLS_ENABLED
41#include "editor/editor_import_blend_runner.h"
42#include "editor/editor_scene_exporter_gltf_plugin.h"
43#include "editor/editor_scene_importer_blend.h"
44#include "editor/editor_scene_importer_fbx.h"
45#include "editor/editor_scene_importer_gltf.h"
46
47#include "core/config/project_settings.h"
48#include "editor/editor_node.h"
49#include "editor/editor_settings.h"
50
51static void _editor_init() {
52 Ref<EditorSceneFormatImporterGLTF> import_gltf;
53 import_gltf.instantiate();
54 ResourceImporterScene::add_importer(import_gltf);
55
56 // Blend to glTF importer.
57
58 bool blend_enabled = GLOBAL_GET("filesystem/import/blender/enabled");
59 String blender3_path = EDITOR_GET("filesystem/import/blender/blender3_path");
60 if (blend_enabled) {
61 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
62 if (blender3_path.is_empty()) {
63 WARN_PRINT(TTR("Blend file import is enabled in the project settings, but no Blender path is configured in the editor settings. Blend files will not be imported."));
64 } else if (!da->dir_exists(blender3_path)) {
65 WARN_PRINT(TTR("Blend file import is enabled, but the Blender path doesn't point to an accessible directory. Blend files will not be imported."));
66 } else {
67 Ref<EditorSceneFormatImporterBlend> importer;
68 importer.instantiate();
69 ResourceImporterScene::add_importer(importer);
70
71 Ref<EditorFileSystemImportFormatSupportQueryBlend> blend_import_query;
72 blend_import_query.instantiate();
73 EditorFileSystem::get_singleton()->add_import_format_support_query(blend_import_query);
74 }
75 }
76 memnew(EditorImportBlendRunner);
77 EditorNode::get_singleton()->add_child(EditorImportBlendRunner::get_singleton());
78
79 // FBX to glTF importer.
80
81 bool fbx_enabled = GLOBAL_GET("filesystem/import/fbx/enabled");
82 if (fbx_enabled) {
83 Ref<EditorSceneFormatImporterFBX> importer;
84 importer.instantiate();
85 ResourceImporterScene::get_scene_singleton()->add_importer(importer);
86
87 Ref<EditorFileSystemImportFormatSupportQueryFBX> fbx_import_query;
88 fbx_import_query.instantiate();
89 EditorFileSystem::get_singleton()->add_import_format_support_query(fbx_import_query);
90 }
91}
92#endif // TOOLS_ENABLED
93
94#define GLTF_REGISTER_DOCUMENT_EXTENSION(m_doc_ext_class) \
95 Ref<m_doc_ext_class> extension_##m_doc_ext_class; \
96 extension_##m_doc_ext_class.instantiate(); \
97 GLTFDocument::register_gltf_document_extension(extension_##m_doc_ext_class);
98
99void initialize_gltf_module(ModuleInitializationLevel p_level) {
100 if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) {
101 // glTF API available at runtime.
102 GDREGISTER_CLASS(GLTFAccessor);
103 GDREGISTER_CLASS(GLTFAnimation);
104 GDREGISTER_CLASS(GLTFBufferView);
105 GDREGISTER_CLASS(GLTFCamera);
106 GDREGISTER_CLASS(GLTFDocument);
107 GDREGISTER_CLASS(GLTFDocumentExtension);
108 GDREGISTER_CLASS(GLTFDocumentExtensionConvertImporterMesh);
109 GDREGISTER_CLASS(GLTFLight);
110 GDREGISTER_CLASS(GLTFMesh);
111 GDREGISTER_CLASS(GLTFNode);
112 GDREGISTER_CLASS(GLTFPhysicsBody);
113 GDREGISTER_CLASS(GLTFPhysicsShape);
114 GDREGISTER_CLASS(GLTFSkeleton);
115 GDREGISTER_CLASS(GLTFSkin);
116 GDREGISTER_CLASS(GLTFSpecGloss);
117 GDREGISTER_CLASS(GLTFState);
118 GDREGISTER_CLASS(GLTFTexture);
119 GDREGISTER_CLASS(GLTFTextureSampler);
120 // Register GLTFDocumentExtension classes with GLTFDocument.
121 GLTF_REGISTER_DOCUMENT_EXTENSION(GLTFDocumentExtensionPhysics);
122 GLTF_REGISTER_DOCUMENT_EXTENSION(GLTFDocumentExtensionTextureKTX);
123 GLTF_REGISTER_DOCUMENT_EXTENSION(GLTFDocumentExtensionTextureWebP);
124 bool is_editor = ::Engine::get_singleton()->is_editor_hint();
125 if (!is_editor) {
126 GLTF_REGISTER_DOCUMENT_EXTENSION(GLTFDocumentExtensionConvertImporterMesh);
127 }
128 }
129
130#ifdef TOOLS_ENABLED
131 if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
132 // Editor-specific API.
133 ClassDB::APIType prev_api = ClassDB::get_current_api();
134 ClassDB::set_current_api(ClassDB::API_EDITOR);
135
136 GDREGISTER_CLASS(EditorSceneFormatImporterGLTF);
137 EditorPlugins::add_by_type<SceneExporterGLTFPlugin>();
138
139 // Project settings defined here so doctool finds them.
140 GLOBAL_DEF_RST_BASIC("filesystem/import/blender/enabled", true);
141 GLOBAL_DEF_RST_BASIC("filesystem/import/fbx/enabled", true);
142 GDREGISTER_CLASS(EditorSceneFormatImporterBlend);
143 GDREGISTER_CLASS(EditorSceneFormatImporterFBX);
144 // Can't (a priori) run external app on these platforms.
145 GLOBAL_DEF_RST("filesystem/import/blender/enabled.android", false);
146 GLOBAL_DEF_RST("filesystem/import/blender/enabled.web", false);
147 GLOBAL_DEF_RST("filesystem/import/fbx/enabled.android", false);
148 GLOBAL_DEF_RST("filesystem/import/fbx/enabled.web", false);
149
150 ClassDB::set_current_api(prev_api);
151 EditorNode::add_init_callback(_editor_init);
152 }
153
154#endif // TOOLS_ENABLED
155}
156
157void uninitialize_gltf_module(ModuleInitializationLevel p_level) {
158 if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
159 return;
160 }
161 GLTFDocument::unregister_all_gltf_document_extensions();
162}
163