From a54301ef75e58f0287353ad72cd9ae949bd32d5b Mon Sep 17 00:00:00 2001 From: zodywoolsey Date: Sat, 26 Apr 2025 01:29:50 -0600 Subject: [PATCH] fix fbx runtime import not generating meshes properly Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> Co-authored-by: K. S. Ernest (iFire) Lee Co-authored-by: Naming-things-is-hard-btw --- modules/fbx/SCsub | 2 ++ modules/fbx/extensions/SCsub | 11 +++++++ modules/fbx/fbx_document.cpp | 52 ++++++++++++++++++++++++++-------- modules/fbx/register_types.cpp | 13 +++++++-- 4 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 modules/fbx/extensions/SCsub diff --git a/modules/fbx/SCsub b/modules/fbx/SCsub index 05a15c3be29..8200ae81675 100644 --- a/modules/fbx/SCsub +++ b/modules/fbx/SCsub @@ -40,6 +40,8 @@ module_obj = [] env_fbx.add_source_files(module_obj, "*.cpp") env_fbx.add_source_files(module_obj, "structures/*.cpp") +SConscript("extensions/SCsub") + if env.editor_build: env_fbx.add_source_files(module_obj, "editor/*.cpp") diff --git a/modules/fbx/extensions/SCsub b/modules/fbx/extensions/SCsub new file mode 100644 index 00000000000..ddeee800078 --- /dev/null +++ b/modules/fbx/extensions/SCsub @@ -0,0 +1,11 @@ +#!/usr/bin/env python +from misc.utility.scons_hints import * + +Import("env") +Import("env_modules") + +env_gltf = env_modules.Clone() + +# Godot source files + +env_gltf.add_source_files(env.modules_sources, "*.cpp") diff --git a/modules/fbx/fbx_document.cpp b/modules/fbx/fbx_document.cpp index a9ee9b3c0ea..3af1ce69ae6 100644 --- a/modules/fbx/fbx_document.cpp +++ b/modules/fbx/fbx_document.cpp @@ -2103,6 +2103,15 @@ Error FBXDocument::_parse(Ref p_state, String p_path, Ref WARN_PRINT(vformat("FBX: ignored %d further ufbx warnings", ignored_warning_count)); } + document_extensions.clear(); + for (Ref ext : all_document_extensions) { + ERR_CONTINUE(ext.is_null()); + err = ext->import_preflight(p_state, p_state->json["extensionsUsed"]); + if (err == OK) { + document_extensions.push_back(ext); + } + } + err = _parse_fbx_state(p_state, p_path); ERR_FAIL_COND_V(err != OK, err); @@ -2130,6 +2139,27 @@ Node *FBXDocument::generate_scene(Ref p_state, float p_bake_fps, bool _import_animation(state, ap, i, p_trimming, p_remove_immutable_tracks); } } + for (KeyValue E : state->scene_nodes) { + ERR_CONTINUE(!E.value); + for (Ref ext : document_extensions) { + ERR_CONTINUE(ext.is_null()); + Dictionary node_json; + if (state->json.has("nodes")) { + Array nodes = state->json["nodes"]; + if (0 <= E.key && E.key < nodes.size()) { + node_json = nodes[E.key]; + } + } + Ref gltf_node = state->nodes[E.key]; + Error err = ext->import_node(p_state, gltf_node, node_json, E.value); + ERR_CONTINUE(err != OK); + } + } + for (Ref ext : document_extensions) { + ERR_CONTINUE(ext.is_null()); + Error err = ext->import_post(p_state, root); + ERR_CONTINUE(err != OK); + } ERR_FAIL_NULL_V(root, nullptr); return root; } @@ -2148,12 +2178,11 @@ Error FBXDocument::append_from_buffer(PackedByteArray p_bytes, String p_base_pat state->base_path = p_base_path.get_base_dir(); err = _parse(state, state->base_path, file_access); ERR_FAIL_COND_V(err != OK, err); - // TODO: 202040118 // fire - // for (Ref ext : get_all_gltf_document_extensions()) { - // ERR_CONTINUE(ext.is_null()); - // err = ext->import_post_parse(state); - // ERR_FAIL_COND_V(err != OK, err); - // } + for (Ref ext : document_extensions) { + ERR_CONTINUE(ext.is_null()); + err = ext->import_post_parse(state); + ERR_FAIL_COND_V(err != OK, err); + } return OK; } @@ -2247,12 +2276,11 @@ Error FBXDocument::append_from_file(String p_path, Ref p_state, uint3 state->base_path = base_path; err = _parse(p_state, base_path, file); ERR_FAIL_COND_V(err != OK, err); - // TODO: 20240118 // fire - // for (Ref ext : document_extensions) { - // ERR_CONTINUE(ext.is_null()); - // err = ext->import_post_parse(p_state); - // ERR_FAIL_COND_V(err != OK, err); - // } + for (Ref ext : document_extensions) { + ERR_CONTINUE(ext.is_null()); + err = ext->import_post_parse(p_state); + ERR_FAIL_COND_V(err != OK, err); + } return OK; } diff --git a/modules/fbx/register_types.cpp b/modules/fbx/register_types.cpp index ba6a0b7719a..c1a2bd5bfcb 100644 --- a/modules/fbx/register_types.cpp +++ b/modules/fbx/register_types.cpp @@ -30,6 +30,7 @@ #include "register_types.h" +#include "../gltf/extensions/gltf_document_extension_convert_importer_mesh.h" #include "fbx_document.h" #ifdef TOOLS_ENABLED @@ -53,10 +54,19 @@ static void _editor_init() { } #endif // TOOLS_ENABLED +#define FBX_REGISTER_DOCUMENT_EXTENSION(m_doc_ext_class) \ + Ref extension_##m_doc_ext_class; \ + extension_##m_doc_ext_class.instantiate(); \ + FBXDocument::register_gltf_document_extension(extension_##m_doc_ext_class); + void initialize_fbx_module(ModuleInitializationLevel p_level) { if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) { GDREGISTER_CLASS(FBXDocument); GDREGISTER_CLASS(FBXState); + bool is_editor = Engine::get_singleton()->is_editor_hint(); + if (!is_editor) { + FBX_REGISTER_DOCUMENT_EXTENSION(GLTFDocumentExtensionConvertImporterMesh); + } } #ifdef TOOLS_ENABLED @@ -77,6 +87,5 @@ void uninitialize_fbx_module(ModuleInitializationLevel p_level) { if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { return; } - // TODO: 20240118 // fire - // FBXDocument::unregister_all_gltf_document_extensions(); + FBXDocument::unregister_all_gltf_document_extensions(); }