From b9f8901b5e0e708f18c57a06c0815630806c9197 Mon Sep 17 00:00:00 2001 From: Morris Tabor <80684659+mortarroad@users.noreply.github.com> Date: Mon, 12 Sep 2022 17:22:16 +0200 Subject: [PATCH] Fix finding AnimationPlayer in scene import The scene importer always assumed that the AnimationPlayer is called "AnimationPlayer". This is not always true: for example the GLTF importer just creates an AnimationPlayer with the default name, which may be "animation_player", depending on the project settings. This fix instead chooses the first node that is an AnimationPlayer, and warns if there is more than one. --- editor/import/resource_importer_scene.cpp | 41 ++++++++++++++--------- editor/import/resource_importer_scene.h | 2 ++ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/editor/import/resource_importer_scene.cpp b/editor/import/resource_importer_scene.cpp index 049574e11..1e9838cbc 100644 --- a/editor/import/resource_importer_scene.cpp +++ b/editor/import/resource_importer_scene.cpp @@ -636,15 +636,12 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, RBMaphas_node(String("AnimationPlayer"))) { + AnimationPlayer *anim = _find_animation_player(scene); + if (!anim) { + WARN_PRINT("Creating clips is enabled, but no animation player was found."); return; } - Node *n = scene->get_node(String("AnimationPlayer")); - ERR_FAIL_COND(!n); - AnimationPlayer *anim = Object::cast_to(n); - ERR_FAIL_COND(!anim); - if (!anim->has_animation("default")) { ERR_FAIL_COND_MSG(p_clips.size() > 0, "To create clips, animations must be named \"default\"."); return; @@ -788,13 +785,10 @@ void ResourceImporterScene::_filter_anim_tracks(Ref anim, RBSethas_node(String("AnimationPlayer"))) { + AnimationPlayer *anim = _find_animation_player(scene); + if (!anim) { return; } - Node *n = scene->get_node(String("AnimationPlayer")); - ERR_FAIL_COND(!n); - AnimationPlayer *anim = Object::cast_to(n); - ERR_FAIL_COND(!anim); Vector strings = p_text.split("\n"); for (int i = 0; i < strings.size(); i++) { @@ -895,13 +889,10 @@ void ResourceImporterScene::_filter_tracks(Node *scene, const String &p_text) { } void ResourceImporterScene::_optimize_animations(Node *scene, float p_max_lin_error, float p_max_ang_error, float p_max_angle) { - if (!scene->has_node(String("AnimationPlayer"))) { + AnimationPlayer *anim = _find_animation_player(scene); + if (!anim) { return; } - Node *n = scene->get_node(String("AnimationPlayer")); - ERR_FAIL_COND(!n); - AnimationPlayer *anim = Object::cast_to(n); - ERR_FAIL_COND(!anim); List anim_names; anim->get_animation_list(&anim_names); @@ -926,6 +917,24 @@ static String _make_extname(const String &p_str) { return ext_name; } +AnimationPlayer *ResourceImporterScene::_find_animation_player(Node *p_node) { + // Find a direct child that is an AnimationPlayer. + AnimationPlayer *ret = nullptr; + + for (int i = 0; i < p_node->get_child_count(); i++) { + AnimationPlayer *child = Object::cast_to(p_node->get_child(i)); + if (child) { + if (ret == nullptr) { + ret = child; + } else { + WARN_PRINT("More than one animation player: \"" + ret->get_name() + "\" and \"" + child->get_name() + "\"."); + } + } + } + + return ret; +} + void ResourceImporterScene::_find_meshes(Node *p_node, RBMap, Transform> &meshes) { MeshInstance *mi = Object::cast_to(p_node); diff --git a/editor/import/resource_importer_scene.h b/editor/import/resource_importer_scene.h index fd36d9361..326abb5cd 100644 --- a/editor/import/resource_importer_scene.h +++ b/editor/import/resource_importer_scene.h @@ -42,6 +42,7 @@ class Shape; class Animation; class Mesh; class ArrayMesh; +class AnimationPlayer; class EditorSceneImporter : public Reference { GDCLASS(EditorSceneImporter, Reference); @@ -151,6 +152,7 @@ public: virtual int get_import_order() const { return ResourceImporter::IMPORT_ORDER_SCENE; } void _find_meshes(Node *p_node, RBMap, Transform> &meshes); + AnimationPlayer *_find_animation_player(Node *p_node); void _make_external_resources(Node *p_node, const String &p_base_path, bool p_make_animations, bool p_animations_as_text, bool p_keep_animations, bool p_make_materials, bool p_materials_as_text, bool p_keep_materials, bool p_make_meshes, bool p_meshes_as_text, RBMap, Ref> &p_animations, RBMap, Ref> &p_materials, RBMap, Ref> &p_meshes);