Skip to content

Commit

Permalink
Add animation post import plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeGSousa committed Jan 31, 2025
1 parent c676b5b commit e271d06
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/editor/animation_post_import_plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "animation_post_import_plugin.h"

#include "godot_cpp/classes/animation_player.hpp"
#include "godot_cpp/classes/file_access.hpp"
#include "godot_cpp/classes/node.hpp"

Variant AnimationPostImportPlugin::_get_option_visibility(const String& p_path, bool p_for_animation, const String& p_option) const {
if (p_option == "export/animation_export_path") {
return p_for_animation;
}
return EditorScenePostImportPlugin::_get_option_visibility(p_path, p_for_animation, p_option);
}

void AnimationPostImportPlugin::_get_import_options(const String& p_path) {
add_import_option_advanced(Variant::STRING, "export/animation_export_path", "", PROPERTY_HINT_DIR, "");
}

void AnimationPostImportPlugin::_pre_process(Node* p_scene) {

const String export_path = get_option_value("export/animation_export_path");

if (export_path.is_empty()) {
return;
}

Dictionary animations;
_export_animations(p_scene, animations, export_path);

Dictionary subresources = get_option_value("_subresources");
subresources["animations"] = animations;
}

void AnimationPostImportPlugin::_bind_methods() {
}

void AnimationPostImportPlugin::_export_animations(Node* p_node, Dictionary& p_animations, const String& p_export_path) {
AnimationPlayer* anim_node = Object::cast_to<AnimationPlayer>(p_node);

if (anim_node) {
PackedStringArray anim_list = anim_node->get_animation_list();

for (int32_t i = 0; i < anim_list.size(); i++) {
StringName anim_name = anim_list[i];

Dictionary animation;
animation["save_to_file/enabled"] = true;
animation["save_to_file/keep_custom_tracks"] = "";

String clean_anim_name = anim_name.validate_filename();
String file_path = p_export_path.path_join(clean_anim_name) + ".res";
int idx = 1;
while (FileAccess::file_exists(file_path)) {
file_path = p_export_path.path_join(clean_anim_name + String::num_int64(idx)) + ".res";
idx++;
}

animation["save_to_file/path"] = file_path;

p_animations[anim_name] = animation;
}
}

for (int32_t i = 0; i < p_node->get_child_count(); i++) {
_export_animations(p_node->get_child(i), p_animations, p_export_path);
}
}
21 changes: 21 additions & 0 deletions src/editor/animation_post_import_plugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <godot_cpp/classes/editor_scene_post_import_plugin.hpp>

using namespace godot;

class AnimationPostImportPlugin : public EditorScenePostImportPlugin {
GDCLASS(AnimationPostImportPlugin, EditorScenePostImportPlugin)

public:
virtual Variant _get_option_visibility(const String& p_path, bool p_for_animation, const String& p_option) const override;

virtual void _get_import_options(const String& p_path) override;
virtual void _pre_process(Node* p_scene) override;

protected:
static void _bind_methods();

private:
void _export_animations(Node* p_node, Dictionary& p_animations, const String& p_export_path);
};
5 changes: 5 additions & 0 deletions src/editor/mm_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@

MMEditorPlugin::MMEditorPlugin() {
_editor = memnew(MMEditor);

_gizmo_plugin.instantiate();
add_node_3d_gizmo_plugin(_gizmo_plugin);

_post_import_plugin.instantiate();
add_scene_post_import_plugin(_post_import_plugin);

_bottom_panel_button = add_control_to_bottom_panel(_editor, "MMEditor");
_editor->connect("animation_visualization_requested", callable_mp(_gizmo_plugin.ptr(), &MMEditorGizmoPlugin::on_anim_viz_requested));
_make_visible(false);
Expand Down
2 changes: 2 additions & 0 deletions src/editor/mm_editor_plugin.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef MM_EDITOR_PLUGIN_H
#define MM_EDITOR_PLUGIN_H

#include "animation_post_import_plugin.h"
#include "mm_character.h"
#include "mm_editor.h"
#include "mm_editor_gizmo_plugin.h"
Expand All @@ -27,6 +28,7 @@ class MMEditorPlugin : public EditorPlugin {
static void _bind_methods() {};

Ref<MMEditorGizmoPlugin> _gizmo_plugin;
Ref<AnimationPostImportPlugin> _post_import_plugin;

MMEditor* _editor;
Button* _bottom_panel_button;
Expand Down
3 changes: 3 additions & 0 deletions src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "mm_character.h"

#include "editor/animation_post_import_plugin.h"
#include "editor/animation_tree_handler_plugin.h"
#include "editor/mm_data_tab.h"
#include "editor/mm_editor.h"
Expand Down Expand Up @@ -48,6 +49,7 @@ void initialize_motion_matching_module(ModuleInitializationLevel p_level) {

if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
ClassDB::register_internal_class<AnimationTreeHandlerPlugin>();
ClassDB::register_internal_class<AnimationPostImportPlugin>();
ClassDB::register_internal_class<MMEditorGizmoPlugin>();
ClassDB::register_internal_class<MMEditor>();
ClassDB::register_internal_class<MMEditorPlugin>();
Expand All @@ -56,6 +58,7 @@ void initialize_motion_matching_module(ModuleInitializationLevel p_level) {

EditorPlugins::add_by_type<MMEditorPlugin>();
EditorPlugins::add_by_type<AnimationTreeHandlerPlugin>();
EditorPlugins::add_by_type<AnimationPostImportPlugin>();
}
}

Expand Down

0 comments on commit e271d06

Please sign in to comment.