From 637fe3ccdd826828414c85460c7652e6b42f5ace Mon Sep 17 00:00:00 2001 From: Lyuma Date: Tue, 24 Dec 2024 07:34:24 -0800 Subject: [PATCH] Allow post-import plugins to modify _subresources The old code fetched some data before the `EditorScenePostImportPlugin._pre_process` callback. While the callback could modify existing keys, this prevented users from adding new data on a fresh import. By fetching the keys after pre_process, this means users can consistently modify import options for nodes, meshes, materials and animations in a post-import plugin. --- doc/classes/EditorScenePostImportPlugin.xml | 1 + editor/import/3d/resource_importer_scene.cpp | 57 +++++++++++--------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/doc/classes/EditorScenePostImportPlugin.xml b/doc/classes/EditorScenePostImportPlugin.xml index 0004ec65c646..ddcbb757ad32 100644 --- a/doc/classes/EditorScenePostImportPlugin.xml +++ b/doc/classes/EditorScenePostImportPlugin.xml @@ -71,6 +71,7 @@ Pre Process the scene. This function is called right after the scene format loader loaded the scene and no changes have been made. + Pre process may be used to adjust internal import options in the [code]"nodes"[/code], [code]"meshes"[/code], [code]"animations"[/code] or [code]"materials"[/code] keys inside [code]get_option_value("_subresources")[/code]. diff --git a/editor/import/3d/resource_importer_scene.cpp b/editor/import/3d/resource_importer_scene.cpp index 695c12150df3..fe9e58014197 100644 --- a/editor/import/3d/resource_importer_scene.cpp +++ b/editor/import/3d/resource_importer_scene.cpp @@ -2934,38 +2934,22 @@ Error ResourceImporterScene::import(ResourceUID::ID p_source_id, const String &p Dictionary subresources = p_options["_subresources"]; - Dictionary node_data; - if (subresources.has("nodes")) { - node_data = subresources["nodes"]; - } - - Dictionary material_data; - if (subresources.has("materials")) { - material_data = subresources["materials"]; - } - - Dictionary animation_data; - if (subresources.has("animations")) { - animation_data = subresources["animations"]; - } - - Dictionary mesh_data; - if (subresources.has("meshes")) { - mesh_data = subresources["meshes"]; - } - Error err = OK; // Check whether any of the meshes or animations have nonexistent save paths // and if they do, fail the import immediately. - err = _check_resource_save_paths(mesh_data); - if (err != OK) { - return err; + if (subresources.has("meshes")) { + err = _check_resource_save_paths(subresources["meshes"]); + if (err != OK) { + return err; + } } - err = _check_resource_save_paths(animation_data); - if (err != OK) { - return err; + if (subresources.has("animations")) { + err = _check_resource_save_paths(subresources["animations"]); + if (err != OK) { + return err; + } } List missing_deps; // for now, not much will be done with this @@ -3005,6 +2989,27 @@ Error ResourceImporterScene::import(ResourceUID::ID p_source_id, const String &p post_importer_plugins.write[i]->pre_process(scene, p_options); } + // data in _subresources may be modified by pre_process(), so wait until now to check. + Dictionary node_data; + if (subresources.has("nodes")) { + node_data = subresources["nodes"]; + } + + Dictionary material_data; + if (subresources.has("materials")) { + material_data = subresources["materials"]; + } + + Dictionary animation_data; + if (subresources.has("animations")) { + animation_data = subresources["animations"]; + } + + Dictionary mesh_data; + if (subresources.has("meshes")) { + mesh_data = subresources["meshes"]; + } + float fps = 30; if (p_options.has(SNAME("animation/fps"))) { fps = (float)p_options[SNAME("animation/fps")];