diff --git a/modules/gltf/doc_classes/GLTFDocumentExtension.xml b/modules/gltf/doc_classes/GLTFDocumentExtension.xml index 927ffb6aaeb8..24a86fdb758f 100644 --- a/modules/gltf/doc_classes/GLTFDocumentExtension.xml +++ b/modules/gltf/doc_classes/GLTFDocumentExtension.xml @@ -17,7 +17,7 @@ - Part of the export process. This method is run after [method _export_preflight] and before [method _export_node]. + Part of the export process. This method is run after [method _export_preflight] and before [method _export_preserialize]. Runs when converting the data from a Godot scene node. This method can be used to process the Godot scene node data into a format that can be used by [method _export_node]. @@ -28,7 +28,7 @@ - Part of the export process. This method is run after [method _convert_scene_node] and before [method _export_post]. + Part of the export process. This method is run after [method _export_preserialize] and before [method _export_post]. This method can be used to modify the final JSON of each node. @@ -49,6 +49,14 @@ The return value is used to determine if this [GLTFDocumentExtension] instance should be used for exporting a given GLTF file. If [constant OK], the export will use this [GLTFDocumentExtension] instance. If not overridden, [constant OK] is returned. + + + + + Part of the export process. This method is run after [method _convert_scene_node] and before [method _export_node]. + This method can be used to alter the state before performing serialization. It runs every time when generating a buffer with [method GLTFDocument.generate_buffer] or writing to the file system with [method GLTFDocument.write_to_filesystem]. + + diff --git a/modules/gltf/extensions/gltf_document_extension.cpp b/modules/gltf/extensions/gltf_document_extension.cpp index 2804a8b0a221..bd9404774aa7 100644 --- a/modules/gltf/extensions/gltf_document_extension.cpp +++ b/modules/gltf/extensions/gltf_document_extension.cpp @@ -44,6 +44,7 @@ void GLTFDocumentExtension::_bind_methods() { // Export process. GDVIRTUAL_BIND(_export_preflight, "state", "root"); GDVIRTUAL_BIND(_convert_scene_node, "state", "gltf_node", "scene_node"); + GDVIRTUAL_BIND(_export_preserialize, "state"); GDVIRTUAL_BIND(_export_node, "state", "gltf_node", "json", "node"); GDVIRTUAL_BIND(_export_post, "state"); } @@ -134,6 +135,13 @@ void GLTFDocumentExtension::convert_scene_node(Ref p_state, Ref p_state) { + ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); + Error err = OK; + GDVIRTUAL_CALL(_export_preserialize, p_state, err); + return err; +} + Error GLTFDocumentExtension::export_node(Ref p_state, Ref p_gltf_node, Dictionary &r_dict, Node *p_node) { ERR_FAIL_NULL_V(p_state, ERR_INVALID_PARAMETER); ERR_FAIL_NULL_V(p_gltf_node, ERR_INVALID_PARAMETER); diff --git a/modules/gltf/extensions/gltf_document_extension.h b/modules/gltf/extensions/gltf_document_extension.h index d922588a291a..c061a3c83660 100644 --- a/modules/gltf/extensions/gltf_document_extension.h +++ b/modules/gltf/extensions/gltf_document_extension.h @@ -53,6 +53,7 @@ class GLTFDocumentExtension : public Resource { // Export process. virtual Error export_preflight(Ref p_state, Node *p_root); virtual void convert_scene_node(Ref p_state, Ref p_gltf_node, Node *p_scene_node); + virtual Error export_preserialize(Ref p_state); virtual Error export_node(Ref p_state, Ref p_gltf_node, Dictionary &r_json, Node *p_node); virtual Error export_post(Ref p_state); @@ -69,6 +70,7 @@ class GLTFDocumentExtension : public Resource { // Export process. GDVIRTUAL2R(Error, _export_preflight, Ref, Node *); GDVIRTUAL3(_convert_scene_node, Ref, Ref, Node *); + GDVIRTUAL1R(Error, _export_preserialize, Ref); GDVIRTUAL4R(Error, _export_node, Ref, Ref, Dictionary, Node *); GDVIRTUAL1R(Error, _export_post, Ref); }; diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index d828363e03a0..923bd6dd6ab6 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -115,6 +115,12 @@ Error GLTFDocument::_serialize(Ref p_state, const String &p_path) { p_state->buffers.push_back(Vector()); } + for (Ref ext : document_extensions) { + ERR_CONTINUE(ext.is_null()); + Error err = ext->export_preserialize(p_state); + ERR_CONTINUE(err != OK); + } + /* STEP CONVERT MESH INSTANCES */ _convert_mesh_instances(p_state);