2020-12-21 15:39:32 +00:00
/*************************************************************************/
/* register_types.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
2022-01-03 20:27:34 +00:00
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2020-12-21 15:39:32 +00:00
/* */
/* 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 "register_types.h"
2022-03-28 12:10:28 +00:00
# ifndef _3D_DISABLED
2022-09-19 01:35:13 +00:00
# include "extensions/gltf_document_extension_convert_importer_mesh.h"
2022-07-24 19:20:50 +00:00
# include "extensions/gltf_light.h"
# include "extensions/gltf_spec_gloss.h"
2020-12-21 15:39:32 +00:00
# include "gltf_document.h"
# include "gltf_state.h"
2022-07-24 19:20:50 +00:00
# include "structures/gltf_accessor.h"
# include "structures/gltf_animation.h"
# include "structures/gltf_buffer_view.h"
# include "structures/gltf_camera.h"
# include "structures/gltf_mesh.h"
# include "structures/gltf_node.h"
# include "structures/gltf_skeleton.h"
# include "structures/gltf_skin.h"
# include "structures/gltf_texture.h"
2021-10-04 15:49:42 +00:00
# include "structures/gltf_texture_sampler.h"
2020-12-21 15:39:32 +00:00
# ifdef TOOLS_ENABLED
2021-12-14 05:34:18 +00:00
# include "core/config/project_settings.h"
2022-03-28 12:10:28 +00:00
# include "editor/editor_node.h"
# include "editor/editor_scene_exporter_gltf_plugin.h"
2021-12-14 05:34:18 +00:00
# include "editor/editor_scene_importer_blend.h"
2021-12-14 05:34:18 +00:00
# include "editor/editor_scene_importer_fbx.h"
2022-03-28 12:10:28 +00:00
# include "editor/editor_scene_importer_gltf.h"
2021-12-14 05:34:18 +00:00
# include "editor/editor_settings.h"
2022-03-28 12:10:28 +00:00
2020-12-21 15:39:32 +00:00
static void _editor_init ( ) {
2021-10-14 17:34:27 +00:00
Ref < EditorSceneFormatImporterGLTF > import_gltf ;
2021-06-17 22:03:09 +00:00
import_gltf . instantiate ( ) ;
2022-04-12 14:07:09 +00:00
ResourceImporterScene : : add_importer ( import_gltf ) ;
2021-12-14 05:34:18 +00:00
// Blend to glTF importer.
2021-12-14 05:34:18 +00:00
2022-03-31 20:00:17 +00:00
bool blend_enabled = GLOBAL_GET ( " filesystem/import/blender/enabled " ) ;
2021-12-14 05:34:18 +00:00
// Defined here because EditorSettings doesn't exist in `register_gltf_types` yet.
2022-08-22 17:29:07 +00:00
String blender3_path = EDITOR_DEF_RST ( " filesystem/import/blender/blender3_path " , " " ) ;
2021-12-14 05:34:18 +00:00
EditorSettings : : get_singleton ( ) - > add_property_hint ( PropertyInfo ( Variant : : STRING ,
2022-03-31 20:00:17 +00:00
" filesystem/import/blender/blender3_path " , PROPERTY_HINT_GLOBAL_DIR ) ) ;
2021-12-14 05:34:18 +00:00
if ( blend_enabled ) {
2022-08-22 17:29:07 +00:00
Ref < DirAccess > da = DirAccess : : create ( DirAccess : : ACCESS_FILESYSTEM ) ;
if ( blender3_path . is_empty ( ) ) {
WARN_PRINT ( " 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. " ) ;
2022-08-24 19:55:11 +00:00
} else if ( ! da - > dir_exists ( blender3_path ) ) {
WARN_PRINT ( " Blend file import is enabled, but the Blender path doesn't point to an accessible directory. Blend files will not be imported. " ) ;
2022-08-22 17:29:07 +00:00
} else {
Ref < EditorSceneFormatImporterBlend > importer ;
importer . instantiate ( ) ;
ResourceImporterScene : : add_importer ( importer ) ;
2022-03-31 20:00:17 +00:00
2022-08-22 17:29:07 +00:00
Ref < EditorFileSystemImportFormatSupportQueryBlend > blend_import_query ;
blend_import_query . instantiate ( ) ;
EditorFileSystem : : get_singleton ( ) - > add_import_format_support_query ( blend_import_query ) ;
}
2021-12-14 05:34:18 +00:00
}
2021-12-14 05:34:18 +00:00
// 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 ) {
2022-03-23 09:08:58 +00:00
Ref < DirAccess > da = DirAccess : : create ( DirAccess : : ACCESS_FILESYSTEM ) ;
2021-12-14 05:34:18 +00:00
if ( fbx2gltf_path . is_empty ( ) ) {
2022-08-22 17:29:07 +00:00
WARN_PRINT ( " FBX file import is enabled in the project settings, but no FBX2glTF path is configured in the editor settings. FBX files will not be imported. " ) ;
2021-12-14 05:34:18 +00:00
} else if ( ! da - > file_exists ( fbx2gltf_path ) ) {
2022-08-22 17:29:07 +00:00
WARN_PRINT ( " FBX file import is enabled, but the FBX2glTF path doesn't point to an accessible file. FBX files will not be imported. " ) ;
2021-12-14 05:34:18 +00:00
} else {
Ref < EditorSceneFormatImporterFBX > importer ;
importer . instantiate ( ) ;
2022-04-12 14:07:09 +00:00
ResourceImporterScene : : add_importer ( importer ) ;
2021-12-14 05:34:18 +00:00
}
}
2020-12-21 15:39:32 +00:00
}
2021-12-14 05:34:18 +00:00
# endif // TOOLS_ENABLED
2020-12-21 15:39:32 +00:00
2022-09-19 01:35:13 +00:00
# define GLTF_REGISTER_DOCUMENT_EXTENSION(m_doc_ext_class) \
Ref < m_doc_ext_class > extension_ # # m_doc_ext_class ; \
extension_ # # m_doc_ext_class . instantiate ( ) ; \
GLTFDocument : : register_gltf_document_extension ( extension_ # # m_doc_ext_class ) ;
2022-05-03 09:56:08 +00:00
void initialize_gltf_module ( ModuleInitializationLevel p_level ) {
if ( p_level = = MODULE_INITIALIZATION_LEVEL_SCENE ) {
// glTF API available at runtime.
GDREGISTER_CLASS ( GLTFAccessor ) ;
GDREGISTER_CLASS ( GLTFAnimation ) ;
GDREGISTER_CLASS ( GLTFBufferView ) ;
GDREGISTER_CLASS ( GLTFCamera ) ;
GDREGISTER_CLASS ( GLTFDocument ) ;
GDREGISTER_CLASS ( GLTFDocumentExtension ) ;
GDREGISTER_CLASS ( GLTFDocumentExtensionConvertImporterMesh ) ;
GDREGISTER_CLASS ( GLTFLight ) ;
GDREGISTER_CLASS ( GLTFMesh ) ;
GDREGISTER_CLASS ( GLTFNode ) ;
GDREGISTER_CLASS ( GLTFSkeleton ) ;
GDREGISTER_CLASS ( GLTFSkin ) ;
GDREGISTER_CLASS ( GLTFSpecGloss ) ;
GDREGISTER_CLASS ( GLTFState ) ;
GDREGISTER_CLASS ( GLTFTexture ) ;
2021-10-04 15:49:42 +00:00
GDREGISTER_CLASS ( GLTFTextureSampler ) ;
2022-09-19 01:35:13 +00:00
// Register GLTFDocumentExtension classes with GLTFDocument.
bool is_editor = : : Engine : : get_singleton ( ) - > is_editor_hint ( ) ;
if ( ! is_editor ) {
GLTF_REGISTER_DOCUMENT_EXTENSION ( GLTFDocumentExtensionConvertImporterMesh ) ;
}
2022-05-03 09:56:08 +00:00
}
2021-12-14 05:34:18 +00:00
2020-12-21 15:39:32 +00:00
# ifdef TOOLS_ENABLED
2022-05-03 09:56:08 +00:00
if ( p_level = = MODULE_INITIALIZATION_LEVEL_EDITOR ) {
// Editor-specific API.
ClassDB : : APIType prev_api = ClassDB : : get_current_api ( ) ;
ClassDB : : set_current_api ( ClassDB : : API_EDITOR ) ;
GDREGISTER_CLASS ( EditorSceneFormatImporterGLTF ) ;
EditorPlugins : : add_by_type < SceneExporterGLTFPlugin > ( ) ;
// Project settings defined here so doctool finds them.
GLOBAL_DEF_RST ( " filesystem/import/blender/enabled " , true ) ;
GLOBAL_DEF_RST ( " filesystem/import/fbx/enabled " , true ) ;
GDREGISTER_CLASS ( EditorSceneFormatImporterBlend ) ;
GDREGISTER_CLASS ( EditorSceneFormatImporterFBX ) ;
2022-09-12 14:29:40 +00:00
// Can't (a priori) run external app on these platforms.
GLOBAL_DEF_RST ( " filesystem/import/blender/enabled.android " , false ) ;
GLOBAL_DEF_RST ( " filesystem/import/blender/enabled.web " , false ) ;
GLOBAL_DEF_RST ( " filesystem/import/fbx/enabled.android " , false ) ;
GLOBAL_DEF_RST ( " filesystem/import/fbx/enabled.web " , false ) ;
2022-05-03 09:56:08 +00:00
ClassDB : : set_current_api ( prev_api ) ;
EditorNode : : add_init_callback ( _editor_init ) ;
}
2022-03-31 20:00:17 +00:00
2021-12-14 05:34:18 +00:00
# endif // TOOLS_ENABLED
2020-12-21 15:39:32 +00:00
}
2022-05-03 09:56:08 +00:00
void uninitialize_gltf_module ( ModuleInitializationLevel p_level ) {
if ( p_level ! = MODULE_INITIALIZATION_LEVEL_SCENE ) {
return ;
}
2022-09-19 01:35:13 +00:00
GLTFDocument : : unregister_all_gltf_document_extensions ( ) ;
2020-12-21 15:39:32 +00:00
}
2022-03-28 12:10:28 +00:00
# endif // _3D_DISABLED