diff --git a/editor/import/resource_importer_scene.cpp b/editor/import/resource_importer_scene.cpp index fad1403edc2..c16e6096089 100644 --- a/editor/import/resource_importer_scene.cpp +++ b/editor/import/resource_importer_scene.cpp @@ -629,15 +629,12 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map } void ResourceImporterScene::_create_clips(Node *scene, const Array &p_clips, bool p_bake_all) { - if (!scene->has_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; @@ -757,13 +754,10 @@ void ResourceImporterScene::_filter_anim_tracks(Ref anim, Set } void ResourceImporterScene::_filter_tracks(Node *scene, const String &p_text) { - 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); Vector strings = p_text.split("\n"); for (int i = 0; i < strings.size(); i++) { @@ -864,13 +858,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); @@ -895,6 +886,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, Map, 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 99ad6763756..89488d01871 100644 --- a/editor/import/resource_importer_scene.h +++ b/editor/import/resource_importer_scene.h @@ -32,6 +32,7 @@ #define RESOURCE_IMPORTER_SCENE_H #include "core/io/resource_importer.h" +#include "scene/animation/animation_player.h" #include "scene/resources/animation.h" #include "scene/resources/mesh.h" #include "scene/resources/shape.h" @@ -144,6 +145,7 @@ public: virtual int get_import_order() const { return ResourceImporter::IMPORT_ORDER_SCENE; } void _find_meshes(Node *p_node, Map, 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, Map, Ref> &p_animations, Map, Ref> &p_materials, Map, Ref> &p_meshes);