Skip to content

Commit acaa11f

Browse files
Add animation post import plugin (#86)
1 parent c676b5b commit acaa11f

5 files changed

+97
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <godot_cpp/classes/editor_scene_post_import_plugin.hpp>
4+
5+
using namespace godot;
6+
7+
class AnimationPostImportPlugin : public EditorScenePostImportPlugin {
8+
GDCLASS(AnimationPostImportPlugin, EditorScenePostImportPlugin)
9+
10+
public:
11+
virtual Variant _get_option_visibility(const String& p_path, bool p_for_animation, const String& p_option) const override;
12+
13+
virtual void _get_import_options(const String& p_path) override;
14+
virtual void _pre_process(Node* p_scene) override;
15+
16+
protected:
17+
static void _bind_methods();
18+
19+
private:
20+
void _export_animations(Node* p_node, Dictionary& p_animations, const String& p_export_path);
21+
};

src/editor/mm_editor_plugin.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@
1010

1111
MMEditorPlugin::MMEditorPlugin() {
1212
_editor = memnew(MMEditor);
13+
1314
_gizmo_plugin.instantiate();
1415
add_node_3d_gizmo_plugin(_gizmo_plugin);
16+
17+
_post_import_plugin.instantiate();
18+
add_scene_post_import_plugin(_post_import_plugin);
19+
1520
_bottom_panel_button = add_control_to_bottom_panel(_editor, "MMEditor");
1621
_editor->connect("animation_visualization_requested", callable_mp(_gizmo_plugin.ptr(), &MMEditorGizmoPlugin::on_anim_viz_requested));
1722
_make_visible(false);

src/editor/mm_editor_plugin.h

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef MM_EDITOR_PLUGIN_H
22
#define MM_EDITOR_PLUGIN_H
33

4+
#include "animation_post_import_plugin.h"
45
#include "mm_character.h"
56
#include "mm_editor.h"
67
#include "mm_editor_gizmo_plugin.h"
@@ -27,6 +28,7 @@ class MMEditorPlugin : public EditorPlugin {
2728
static void _bind_methods() {};
2829

2930
Ref<MMEditorGizmoPlugin> _gizmo_plugin;
31+
Ref<AnimationPostImportPlugin> _post_import_plugin;
3032

3133
MMEditor* _editor;
3234
Button* _bottom_panel_button;

src/register_types.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "mm_character.h"
66

7+
#include "editor/animation_post_import_plugin.h"
78
#include "editor/animation_tree_handler_plugin.h"
89
#include "editor/mm_data_tab.h"
910
#include "editor/mm_editor.h"
@@ -48,6 +49,7 @@ void initialize_motion_matching_module(ModuleInitializationLevel p_level) {
4849

4950
if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
5051
ClassDB::register_internal_class<AnimationTreeHandlerPlugin>();
52+
ClassDB::register_internal_class<AnimationPostImportPlugin>();
5153
ClassDB::register_internal_class<MMEditorGizmoPlugin>();
5254
ClassDB::register_internal_class<MMEditor>();
5355
ClassDB::register_internal_class<MMEditorPlugin>();
@@ -56,6 +58,7 @@ void initialize_motion_matching_module(ModuleInitializationLevel p_level) {
5658

5759
EditorPlugins::add_by_type<MMEditorPlugin>();
5860
EditorPlugins::add_by_type<AnimationTreeHandlerPlugin>();
61+
EditorPlugins::add_by_type<AnimationPostImportPlugin>();
5962
}
6063
}
6164

0 commit comments

Comments
 (0)