|
| 1 | +#include "animation_post_import_plugin.h" |
| 2 | + |
| 3 | +#include "godot_cpp/classes/animation_player.hpp" |
| 4 | +#include "godot_cpp/classes/file_access.hpp" |
| 5 | +#include "godot_cpp/classes/node.hpp" |
| 6 | + |
| 7 | +Variant AnimationPostImportPlugin::_get_option_visibility(const String& p_path, bool p_for_animation, const String& p_option) const { |
| 8 | + if (p_option == "export/animation_export_path") { |
| 9 | + return p_for_animation; |
| 10 | + } |
| 11 | + return EditorScenePostImportPlugin::_get_option_visibility(p_path, p_for_animation, p_option); |
| 12 | +} |
| 13 | + |
| 14 | +void AnimationPostImportPlugin::_get_import_options(const String& p_path) { |
| 15 | + add_import_option_advanced(Variant::STRING, "export/animation_export_path", "", PROPERTY_HINT_DIR, ""); |
| 16 | +} |
| 17 | + |
| 18 | +void AnimationPostImportPlugin::_pre_process(Node* p_scene) { |
| 19 | + |
| 20 | + const String export_path = get_option_value("export/animation_export_path"); |
| 21 | + |
| 22 | + if (export_path.is_empty()) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + Dictionary animations; |
| 27 | + _export_animations(p_scene, animations, export_path); |
| 28 | + |
| 29 | + Dictionary subresources = get_option_value("_subresources"); |
| 30 | + subresources["animations"] = animations; |
| 31 | +} |
| 32 | + |
| 33 | +void AnimationPostImportPlugin::_bind_methods() { |
| 34 | +} |
| 35 | + |
| 36 | +void AnimationPostImportPlugin::_export_animations(Node* p_node, Dictionary& p_animations, const String& p_export_path) { |
| 37 | + AnimationPlayer* anim_node = Object::cast_to<AnimationPlayer>(p_node); |
| 38 | + |
| 39 | + if (anim_node) { |
| 40 | + PackedStringArray anim_list = anim_node->get_animation_list(); |
| 41 | + |
| 42 | + for (int32_t i = 0; i < anim_list.size(); i++) { |
| 43 | + StringName anim_name = anim_list[i]; |
| 44 | + |
| 45 | + Dictionary animation; |
| 46 | + animation["save_to_file/enabled"] = true; |
| 47 | + animation["save_to_file/keep_custom_tracks"] = ""; |
| 48 | + |
| 49 | + String clean_anim_name = anim_name.validate_filename(); |
| 50 | + String file_path = p_export_path.path_join(clean_anim_name) + ".res"; |
| 51 | + int idx = 1; |
| 52 | + while (FileAccess::file_exists(file_path)) { |
| 53 | + file_path = p_export_path.path_join(clean_anim_name + String::num_int64(idx)) + ".res"; |
| 54 | + idx++; |
| 55 | + } |
| 56 | + |
| 57 | + animation["save_to_file/path"] = file_path; |
| 58 | + |
| 59 | + p_animations[anim_name] = animation; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + for (int32_t i = 0; i < p_node->get_child_count(); i++) { |
| 64 | + _export_animations(p_node->get_child(i), p_animations, p_export_path); |
| 65 | + } |
| 66 | +} |
0 commit comments