register_types.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*************************************************************************/
  2. /* register_types.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "register_types.h"
  31. #ifndef _3D_DISABLED
  32. #include "extensions/gltf_light.h"
  33. #include "extensions/gltf_spec_gloss.h"
  34. #include "gltf_document.h"
  35. #include "gltf_document_extension.h"
  36. #include "gltf_document_extension_convert_importer_mesh.h"
  37. #include "gltf_state.h"
  38. #include "structures/gltf_accessor.h"
  39. #include "structures/gltf_animation.h"
  40. #include "structures/gltf_buffer_view.h"
  41. #include "structures/gltf_camera.h"
  42. #include "structures/gltf_mesh.h"
  43. #include "structures/gltf_node.h"
  44. #include "structures/gltf_skeleton.h"
  45. #include "structures/gltf_skin.h"
  46. #include "structures/gltf_texture.h"
  47. #ifdef TOOLS_ENABLED
  48. #include "core/config/project_settings.h"
  49. #include "editor/editor_node.h"
  50. #include "editor/editor_scene_exporter_gltf_plugin.h"
  51. #include "editor/editor_scene_importer_blend.h"
  52. #include "editor/editor_scene_importer_fbx.h"
  53. #include "editor/editor_scene_importer_gltf.h"
  54. #include "editor/editor_settings.h"
  55. static void _editor_init() {
  56. Ref<EditorSceneFormatImporterGLTF> import_gltf;
  57. import_gltf.instantiate();
  58. ResourceImporterScene::add_importer(import_gltf);
  59. // Blend to glTF importer.
  60. bool blend_enabled = GLOBAL_GET("filesystem/import/blender/enabled");
  61. // Defined here because EditorSettings doesn't exist in `register_gltf_types` yet.
  62. String blender3_path = EDITOR_DEF_RST("filesystem/import/blender/blender3_path", "");
  63. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,
  64. "filesystem/import/blender/blender3_path", PROPERTY_HINT_GLOBAL_DIR));
  65. if (blend_enabled) {
  66. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  67. if (blender3_path.is_empty()) {
  68. 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.");
  69. } else if (!da->file_exists(blender3_path)) {
  70. WARN_PRINT("Blend file import is enabled, but the Blender path doesn't point to an accessible file. Blend files will not be imported.");
  71. } else {
  72. Ref<EditorSceneFormatImporterBlend> importer;
  73. importer.instantiate();
  74. ResourceImporterScene::add_importer(importer);
  75. Ref<EditorFileSystemImportFormatSupportQueryBlend> blend_import_query;
  76. blend_import_query.instantiate();
  77. EditorFileSystem::get_singleton()->add_import_format_support_query(blend_import_query);
  78. }
  79. }
  80. // FBX to glTF importer.
  81. bool fbx_enabled = GLOBAL_GET("filesystem/import/fbx/enabled");
  82. // Defined here because EditorSettings doesn't exist in `register_gltf_types` yet.
  83. String fbx2gltf_path = EDITOR_DEF_RST("filesystem/import/fbx/fbx2gltf_path", "");
  84. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING,
  85. "filesystem/import/fbx/fbx2gltf_path", PROPERTY_HINT_GLOBAL_FILE));
  86. if (fbx_enabled) {
  87. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  88. if (fbx2gltf_path.is_empty()) {
  89. 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.");
  90. } else if (!da->file_exists(fbx2gltf_path)) {
  91. WARN_PRINT("FBX file import is enabled, but the FBX2glTF path doesn't point to an accessible file. FBX files will not be imported.");
  92. } else {
  93. Ref<EditorSceneFormatImporterFBX> importer;
  94. importer.instantiate();
  95. ResourceImporterScene::add_importer(importer);
  96. }
  97. }
  98. }
  99. #endif // TOOLS_ENABLED
  100. void initialize_gltf_module(ModuleInitializationLevel p_level) {
  101. if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) {
  102. // glTF API available at runtime.
  103. GDREGISTER_CLASS(GLTFAccessor);
  104. GDREGISTER_CLASS(GLTFAnimation);
  105. GDREGISTER_CLASS(GLTFBufferView);
  106. GDREGISTER_CLASS(GLTFCamera);
  107. GDREGISTER_CLASS(GLTFDocument);
  108. GDREGISTER_CLASS(GLTFDocumentExtension);
  109. GDREGISTER_CLASS(GLTFDocumentExtensionConvertImporterMesh);
  110. GDREGISTER_CLASS(GLTFLight);
  111. GDREGISTER_CLASS(GLTFMesh);
  112. GDREGISTER_CLASS(GLTFNode);
  113. GDREGISTER_CLASS(GLTFSkeleton);
  114. GDREGISTER_CLASS(GLTFSkin);
  115. GDREGISTER_CLASS(GLTFSpecGloss);
  116. GDREGISTER_CLASS(GLTFState);
  117. GDREGISTER_CLASS(GLTFTexture);
  118. }
  119. #ifdef TOOLS_ENABLED
  120. if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
  121. // Editor-specific API.
  122. ClassDB::APIType prev_api = ClassDB::get_current_api();
  123. ClassDB::set_current_api(ClassDB::API_EDITOR);
  124. GDREGISTER_CLASS(EditorSceneFormatImporterGLTF);
  125. EditorPlugins::add_by_type<SceneExporterGLTFPlugin>();
  126. // Project settings defined here so doctool finds them.
  127. GLOBAL_DEF_RST("filesystem/import/blender/enabled", true);
  128. GLOBAL_DEF_RST("filesystem/import/fbx/enabled", true);
  129. GDREGISTER_CLASS(EditorSceneFormatImporterBlend);
  130. GDREGISTER_CLASS(EditorSceneFormatImporterFBX);
  131. ClassDB::set_current_api(prev_api);
  132. EditorNode::add_init_callback(_editor_init);
  133. }
  134. #endif // TOOLS_ENABLED
  135. }
  136. void uninitialize_gltf_module(ModuleInitializationLevel p_level) {
  137. if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
  138. return;
  139. }
  140. }
  141. #endif // _3D_DISABLED