ResourceImporterScene: Replace animation bool with an import type string enum

This commit is contained in:
Aaron Franke 2023-10-06 00:17:33 -05:00
parent a2f097d603
commit 9dd71c6414
No known key found for this signature in database
GPG Key ID: 40A1750B977E56BF
16 changed files with 84 additions and 63 deletions

View File

@ -6888,10 +6888,10 @@ EditorNode::EditorNode() {
import_shader_file.instantiate();
ResourceFormatImporter::get_singleton()->add_importer(import_shader_file);
Ref<ResourceImporterScene> import_scene = memnew(ResourceImporterScene(false, true));
Ref<ResourceImporterScene> import_scene = memnew(ResourceImporterScene("PackedScene", true));
ResourceFormatImporter::get_singleton()->add_importer(import_scene);
Ref<ResourceImporterScene> import_animation = memnew(ResourceImporterScene(true, true));
Ref<ResourceImporterScene> import_animation = memnew(ResourceImporterScene("AnimationLibrary", true));
ResourceFormatImporter::get_singleton()->add_importer(import_animation);
{

View File

@ -1216,7 +1216,7 @@ void FileSystemDock::_select_file(const String &p_path, bool p_select_in_favorit
}
if (is_imported) {
SceneImportSettingsDialog::get_singleton()->open_settings(p_path, resource_type == "AnimationLibrary");
SceneImportSettingsDialog::get_singleton()->open_settings(p_path, resource_type);
} else if (resource_type == "PackedScene") {
EditorNode::get_singleton()->open_request(fpath);
} else {

View File

@ -54,7 +54,7 @@ void PostImportPluginSkeletonRestFixer::get_internal_import_options(InternalImpo
}
}
Variant PostImportPluginSkeletonRestFixer::get_internal_option_visibility(InternalImportCategory p_category, bool p_for_animation, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
Variant PostImportPluginSkeletonRestFixer::get_internal_option_visibility(InternalImportCategory p_category, const String &p_scene_import_type, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
if (p_category == INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE) {
if (p_option.begins_with("retarget/rest_fixer/fix_silhouette/")) {
if (!bool(p_options["retarget/rest_fixer/fix_silhouette/enable"])) {

View File

@ -38,7 +38,7 @@ class PostImportPluginSkeletonRestFixer : public EditorScenePostImportPlugin {
public:
virtual void get_internal_import_options(InternalImportCategory p_category, List<ResourceImporter::ImportOption> *r_options) override;
virtual Variant get_internal_option_visibility(InternalImportCategory p_category, bool p_for_animation, const String &p_option, const HashMap<StringName, Variant> &p_options) const override;
virtual Variant get_internal_option_visibility(InternalImportCategory p_category, const String &p_scene_import_type, const String &p_option, const HashMap<StringName, Variant> &p_options) const override;
virtual void internal_process(InternalImportCategory p_category, Node *p_base_scene, Node *p_node, Ref<Resource> p_resource, const Dictionary &p_options) override;
PostImportPluginSkeletonRestFixer();

View File

@ -96,9 +96,10 @@ void EditorSceneFormatImporter::get_import_options(const String &p_path, List<Re
GDVIRTUAL_CALL(_get_import_options, p_path);
}
Variant EditorSceneFormatImporter::get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option, const HashMap<StringName, Variant> &p_options) {
Variant EditorSceneFormatImporter::get_option_visibility(const String &p_path, const String &p_scene_import_type, const String &p_option, const HashMap<StringName, Variant> &p_options) {
Variant ret;
GDVIRTUAL_CALL(_get_option_visibility, p_path, p_for_animation, p_option, ret);
// For compatibility with the old API, pass the import type as a boolean.
GDVIRTUAL_CALL(_get_option_visibility, p_path, p_scene_import_type == "AnimationLibrary", p_option, ret);
return ret;
}
@ -172,13 +173,16 @@ void EditorScenePostImportPlugin::get_internal_import_options(InternalImportCate
GDVIRTUAL_CALL(_get_internal_import_options, p_category);
current_option_list = nullptr;
}
Variant EditorScenePostImportPlugin::get_internal_option_visibility(InternalImportCategory p_category, bool p_for_animation, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
Variant EditorScenePostImportPlugin::get_internal_option_visibility(InternalImportCategory p_category, const String &p_scene_import_type, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
current_options = &p_options;
Variant ret;
GDVIRTUAL_CALL(_get_internal_option_visibility, p_category, p_for_animation, p_option, ret);
// For compatibility with the old API, pass the import type as a boolean.
GDVIRTUAL_CALL(_get_internal_option_visibility, p_category, p_scene_import_type == "AnimationLibrary", p_option, ret);
current_options = nullptr;
return ret;
}
Variant EditorScenePostImportPlugin::get_internal_option_update_view_required(InternalImportCategory p_category, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
current_options = &p_options;
Variant ret;
@ -198,10 +202,10 @@ void EditorScenePostImportPlugin::get_import_options(const String &p_path, List<
GDVIRTUAL_CALL(_get_import_options, p_path);
current_option_list = nullptr;
}
Variant EditorScenePostImportPlugin::get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
Variant EditorScenePostImportPlugin::get_option_visibility(const String &p_path, const String &p_scene_import_type, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
current_options = &p_options;
Variant ret;
GDVIRTUAL_CALL(_get_option_visibility, p_path, p_for_animation, p_option, ret);
GDVIRTUAL_CALL(_get_option_visibility, p_path, p_scene_import_type == "AnimationLibrary", p_option, ret);
current_options = nullptr;
return ret;
}
@ -245,11 +249,22 @@ void EditorScenePostImportPlugin::_bind_methods() {
/////////////////////////////////////////////////////////
String ResourceImporterScene::get_importer_name() const {
return animation_importer ? "animation_library" : "scene";
// For compatibility with 4.2 and earlier we need to keep the "scene" and "animation_library" names.
// However this is arbitrary so for new import types we can use any string.
if (_scene_import_type == "PackedScene") {
return "scene";
} else if (_scene_import_type == "AnimationLibrary") {
return "animation_library";
}
return _scene_import_type;
}
String ResourceImporterScene::get_visible_name() const {
return animation_importer ? "Animation Library" : "Scene";
// This is displayed on the UI. Friendly names here are nice but not vital, so fall back to the type.
if (_scene_import_type == "PackedScene") {
return "Scene";
}
return _scene_import_type.capitalize();
}
void ResourceImporterScene::get_recognized_extensions(List<String> *p_extensions) const {
@ -257,11 +272,14 @@ void ResourceImporterScene::get_recognized_extensions(List<String> *p_extensions
}
String ResourceImporterScene::get_save_extension() const {
return animation_importer ? "res" : "scn";
if (_scene_import_type == "PackedScene") {
return "scn";
}
return "res";
}
String ResourceImporterScene::get_resource_type() const {
return animation_importer ? "AnimationLibrary" : "PackedScene";
return _scene_import_type;
}
int ResourceImporterScene::get_format_version() const {
@ -269,27 +287,28 @@ int ResourceImporterScene::get_format_version() const {
}
bool ResourceImporterScene::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
if (animation_importer) {
if (_scene_import_type == "PackedScene") {
if (p_option.begins_with("animation/")) {
if (p_option != "animation/import" && !bool(p_options["animation/import"])) {
return false;
}
}
} else if (_scene_import_type == "AnimationLibrary") {
if (p_option == "animation/import") { // Option ignored, animation always imported.
return false;
}
} else if (p_option.begins_with("animation/")) {
if (p_option != "animation/import" && !bool(p_options["animation/import"])) {
return false;
if (p_option == "nodes/root_type" || p_option == "nodes/root_name" || p_option.begins_with("meshes/") || p_option.begins_with("skins/")) {
return false; // Nothing to do here for animations.
}
}
if (animation_importer && (p_option == "nodes/root_type" || p_option == "nodes/root_name" || p_option.begins_with("meshes/") || p_option.begins_with("skins/"))) {
return false; // Nothing to do here for animations.
}
if (p_option == "meshes/lightmap_texel_size" && int(p_options["meshes/light_baking"]) != 2) {
// Only display the lightmap texel size import option when using the Static Lightmaps light baking mode.
return false;
}
for (int i = 0; i < post_importer_plugins.size(); i++) {
Variant ret = post_importer_plugins.write[i]->get_option_visibility(p_path, animation_importer, p_option, p_options);
Variant ret = post_importer_plugins.write[i]->get_option_visibility(p_path, _scene_import_type, p_option, p_options);
if (ret.get_type() == Variant::BOOL) {
if (!ret) {
return false;
@ -298,7 +317,7 @@ bool ResourceImporterScene::get_option_visibility(const String &p_path, const St
}
for (Ref<EditorSceneFormatImporter> importer : scene_importers) {
Variant ret = importer->get_option_visibility(p_path, animation_importer, p_option, p_options);
Variant ret = importer->get_option_visibility(p_path, _scene_import_type, p_option, p_options);
if (ret.get_type() == Variant::BOOL) {
if (!ret) {
return false;
@ -2283,7 +2302,7 @@ bool ResourceImporterScene::get_internal_option_visibility(InternalImportCategor
}
for (int i = 0; i < post_importer_plugins.size(); i++) {
Variant ret = post_importer_plugins.write[i]->get_internal_option_visibility(EditorScenePostImportPlugin::InternalImportCategory(p_category), animation_importer, p_option, p_options);
Variant ret = post_importer_plugins.write[i]->get_internal_option_visibility(EditorScenePostImportPlugin::InternalImportCategory(p_category), _scene_import_type, p_option, p_options);
if (ret.get_type() == Variant::BOOL) {
return ret;
}
@ -2877,13 +2896,11 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p
int import_flags = 0;
if (animation_importer) {
if (_scene_import_type == "AnimationLibrary") {
import_flags |= EditorSceneFormatImporter::IMPORT_ANIMATION;
import_flags |= EditorSceneFormatImporter::IMPORT_DISCARD_MESHES_AND_MATERIALS;
} else {
if (bool(p_options["animation/import"])) {
import_flags |= EditorSceneFormatImporter::IMPORT_ANIMATION;
}
} else if (bool(p_options["animation/import"])) {
import_flags |= EditorSceneFormatImporter::IMPORT_ANIMATION;
}
if (bool(p_options["skins/use_named_skins"])) {
@ -3101,7 +3118,7 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p
flags |= ResourceSaver::FLAG_COMPRESS;
}
if (animation_importer) {
if (_scene_import_type == "AnimationLibrary") {
Ref<AnimationLibrary> library;
for (int i = 0; i < scene->get_child_count(); i++) {
AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(scene->get_child(i));
@ -3122,13 +3139,14 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p
print_verbose("Saving animation to: " + p_save_path + ".res");
err = ResourceSaver::save(library, p_save_path + ".res", flags); //do not take over, let the changed files reload themselves
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save animation to file '" + p_save_path + ".res'.");
} else {
} else if (_scene_import_type == "PackedScene") {
Ref<PackedScene> packer = memnew(PackedScene);
packer->pack(scene);
print_verbose("Saving scene to: " + p_save_path + ".scn");
err = ResourceSaver::save(packer, p_save_path + ".scn", flags); //do not take over, let the changed files reload themselves
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save scene to file '" + p_save_path + ".scn'.");
} else {
ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "Unknown scene import type: " + _scene_import_type);
}
memdelete(scene);
@ -3150,20 +3168,20 @@ bool ResourceImporterScene::has_advanced_options() const {
}
void ResourceImporterScene::show_advanced_options(const String &p_path) {
SceneImportSettingsDialog::get_singleton()->open_settings(p_path, animation_importer);
SceneImportSettingsDialog::get_singleton()->open_settings(p_path, _scene_import_type);
}
ResourceImporterScene::ResourceImporterScene(bool p_animation_import, bool p_singleton) {
ResourceImporterScene::ResourceImporterScene(const String &p_scene_import_type, bool p_singleton) {
// This should only be set through the EditorNode.
if (p_singleton) {
if (p_animation_import) {
if (p_scene_import_type == "AnimationLibrary") {
animation_singleton = this;
} else {
} else if (p_scene_import_type == "PackedScene") {
scene_singleton = this;
}
}
animation_importer = p_animation_import;
_scene_import_type = p_scene_import_type;
}
ResourceImporterScene::~ResourceImporterScene() {

View File

@ -44,10 +44,10 @@
#include "scene/resources/animation.h"
#include "scene/resources/mesh.h"
class Material;
class AnimationPlayer;
class ImporterMesh;
class Material;
class EditorSceneFormatImporter : public RefCounted {
GDCLASS(EditorSceneFormatImporter, RefCounted);
@ -78,7 +78,7 @@ public:
virtual void get_extensions(List<String> *r_extensions) const;
virtual Node *import_scene(const String &p_path, uint32_t p_flags, const HashMap<StringName, Variant> &p_options, List<String> *r_missing_deps, Error *r_err = nullptr);
virtual void get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options);
virtual Variant get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option, const HashMap<StringName, Variant> &p_options);
virtual Variant get_option_visibility(const String &p_path, const String &p_scene_import_type, const String &p_option, const HashMap<StringName, Variant> &p_options);
virtual void handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const {}
EditorSceneFormatImporter() {}
@ -140,13 +140,13 @@ public:
void add_import_option_advanced(Variant::Type p_type, const String &p_name, const Variant &p_default_value, PropertyHint p_hint = PROPERTY_HINT_NONE, const String &p_hint_string = String(), int p_usage_flags = PROPERTY_USAGE_DEFAULT);
virtual void get_internal_import_options(InternalImportCategory p_category, List<ResourceImporter::ImportOption> *r_options);
virtual Variant get_internal_option_visibility(InternalImportCategory p_category, bool p_for_animation, const String &p_option, const HashMap<StringName, Variant> &p_options) const;
virtual Variant get_internal_option_visibility(InternalImportCategory p_category, const String &p_scene_import_type, const String &p_option, const HashMap<StringName, Variant> &p_options) const;
virtual Variant get_internal_option_update_view_required(InternalImportCategory p_category, const String &p_option, const HashMap<StringName, Variant> &p_options) const;
virtual void internal_process(InternalImportCategory p_category, Node *p_base_scene, Node *p_node, Ref<Resource> p_resource, const Dictionary &p_options);
virtual void get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options);
virtual Variant get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option, const HashMap<StringName, Variant> &p_options) const;
virtual Variant get_option_visibility(const String &p_path, const String &p_scene_import_type, const String &p_option, const HashMap<StringName, Variant> &p_options) const;
virtual void pre_process(Node *p_scene, const HashMap<StringName, Variant> &p_options);
virtual void post_process(Node *p_scene, const HashMap<StringName, Variant> &p_options);
@ -237,7 +237,7 @@ class ResourceImporterScene : public ResourceImporter {
void _optimize_track_usage(AnimationPlayer *p_player, AnimationImportTracks *p_track_actions);
bool animation_importer = false;
String _scene_import_type = "PackedScene";
public:
static ResourceImporterScene *get_scene_singleton() { return scene_singleton; }
@ -253,6 +253,9 @@ public:
static void clean_up_importer_plugins();
String get_scene_import_type() const { return _scene_import_type; }
void set_scene_import_type(const String &p_type) { _scene_import_type = p_type; }
virtual String get_importer_name() const override;
virtual String get_visible_name() const override;
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
@ -303,7 +306,7 @@ public:
virtual bool can_import_threaded() const override { return false; }
ResourceImporterScene(bool p_animation_import = false, bool p_singleton = false);
ResourceImporterScene(const String &p_scene_import_type = "PackedScene", bool p_singleton = false);
~ResourceImporterScene();
template <typename M>

View File

@ -678,26 +678,26 @@ void SceneImportSettingsDialog::update_view() {
update_view_timer->start();
}
void SceneImportSettingsDialog::open_settings(const String &p_path, bool p_for_animation) {
void SceneImportSettingsDialog::open_settings(const String &p_path, const String &p_scene_import_type) {
if (scene) {
_cleanup();
memdelete(scene);
scene = nullptr;
}
editing_animation = p_for_animation;
editing_animation = p_scene_import_type == "AnimationLibrary";
scene_import_settings_data->settings = nullptr;
scene_import_settings_data->path = p_path;
// Visibility.
data_mode->set_tab_hidden(1, p_for_animation);
data_mode->set_tab_hidden(2, p_for_animation);
if (p_for_animation) {
data_mode->set_tab_hidden(1, editing_animation);
data_mode->set_tab_hidden(2, editing_animation);
if (editing_animation) {
data_mode->set_current_tab(0);
}
action_menu->get_popup()->set_item_disabled(action_menu->get_popup()->get_item_id(ACTION_EXTRACT_MATERIALS), p_for_animation);
action_menu->get_popup()->set_item_disabled(action_menu->get_popup()->get_item_id(ACTION_CHOOSE_MESH_SAVE_PATHS), p_for_animation);
action_menu->get_popup()->set_item_disabled(action_menu->get_popup()->get_item_id(ACTION_EXTRACT_MATERIALS), editing_animation);
action_menu->get_popup()->set_item_disabled(action_menu->get_popup()->get_item_id(ACTION_CHOOSE_MESH_SAVE_PATHS), editing_animation);
base_path = p_path;
@ -771,7 +771,7 @@ void SceneImportSettingsDialog::open_settings(const String &p_path, bool p_for_a
// Start with the root item (Scene) selected.
scene_tree->get_root()->select(0);
if (p_for_animation) {
if (editing_animation) {
set_title(vformat(TTR("Advanced Import Settings for AnimationLibrary '%s'"), base_path.get_file()));
} else {
set_title(vformat(TTR("Advanced Import Settings for Scene '%s'"), base_path.get_file()));

View File

@ -243,7 +243,7 @@ public:
bool is_editing_animation() const { return editing_animation; }
void request_generate_collider();
void update_view();
void open_settings(const String &p_path, bool p_for_animation = false);
void open_settings(const String &p_path, const String &p_scene_import_type = "PackedScene");
static SceneImportSettingsDialog *get_singleton();
Node *get_selected_node();
SceneImportSettingsDialog();

View File

@ -124,7 +124,7 @@ Node *EditorSceneFormatImporterFBX2GLTF::import_scene(const String &p_path, uint
#endif
}
Variant EditorSceneFormatImporterFBX2GLTF::get_option_visibility(const String &p_path, bool p_for_animation,
Variant EditorSceneFormatImporterFBX2GLTF::get_option_visibility(const String &p_path, const String &p_scene_import_type,
const String &p_option, const HashMap<StringName, Variant> &p_options) {
// Remove all the FBX options except for 'fbx/importer' if the importer is fbx2gltf.
// These options are available only for ufbx.

View File

@ -49,7 +49,7 @@ public:
List<String> *r_missing_deps, Error *r_err = nullptr) override;
virtual void get_import_options(const String &p_path,
List<ResourceImporter::ImportOption> *r_options) override;
virtual Variant get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option,
virtual Variant get_option_visibility(const String &p_path, const String &p_scene_import_type, const String &p_option,
const HashMap<StringName, Variant> &p_options) override;
virtual void handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const override;
};

View File

@ -88,7 +88,7 @@ Node *EditorSceneFormatImporterUFBX::import_scene(const String &p_path, uint32_t
return fbx->generate_scene(state, state->get_bake_fps(), (bool)p_options["animation/trimming"], false);
}
Variant EditorSceneFormatImporterUFBX::get_option_visibility(const String &p_path, bool p_for_animation,
Variant EditorSceneFormatImporterUFBX::get_option_visibility(const String &p_path, const String &p_scene_import_type,
const String &p_option, const HashMap<StringName, Variant> &p_options) {
return true;
}

View File

@ -53,7 +53,7 @@ public:
List<String> *r_missing_deps, Error *r_err = nullptr) override;
virtual void get_import_options(const String &p_path,
List<ResourceImporter::ImportOption> *r_options) override;
virtual Variant get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option,
virtual Variant get_option_visibility(const String &p_path, const String &p_scene_import_type, const String &p_option,
const HashMap<StringName, Variant> &p_options) override;
virtual void handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const override;
};

View File

@ -336,7 +336,7 @@ Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path, uint32_
#endif
}
Variant EditorSceneFormatImporterBlend::get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option,
Variant EditorSceneFormatImporterBlend::get_option_visibility(const String &p_path, const String &p_scene_import_type, const String &p_option,
const HashMap<StringName, Variant> &p_options) {
if (p_path.get_extension().to_lower() != "blend") {
return true;

View File

@ -74,7 +74,7 @@ public:
List<String> *r_missing_deps, Error *r_err = nullptr) override;
virtual void get_import_options(const String &p_path,
List<ResourceImporter::ImportOption> *r_options) override;
virtual Variant get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option,
virtual Variant get_option_visibility(const String &p_path, const String &p_scene_import_type, const String &p_option,
const HashMap<StringName, Variant> &p_options) override;
};

View File

@ -100,7 +100,7 @@ void EditorSceneFormatImporterGLTF::handle_compatibility_options(HashMap<StringN
}
}
Variant EditorSceneFormatImporterGLTF::get_option_visibility(const String &p_path, bool p_for_animation,
Variant EditorSceneFormatImporterGLTF::get_option_visibility(const String &p_path, const String &p_scene_import_type,
const String &p_option, const HashMap<StringName, Variant> &p_options) {
return true;
}

View File

@ -50,7 +50,7 @@ public:
virtual void get_import_options(const String &p_path,
List<ResourceImporter::ImportOption> *r_options) override;
virtual void handle_compatibility_options(HashMap<StringName, Variant> &p_import_params) const override;
virtual Variant get_option_visibility(const String &p_path, bool p_for_animation,
virtual Variant get_option_visibility(const String &p_path, const String &p_scene_import_type,
const String &p_option, const HashMap<StringName, Variant> &p_options) override;
};