diff --git a/doc/classes/AnimationNode.xml b/doc/classes/AnimationNode.xml
index 3b7cd9adad71..4948ad94f4a6 100644
--- a/doc/classes/AnimationNode.xml
+++ b/doc/classes/AnimationNode.xml
@@ -152,11 +152,24 @@
Gets the value of a parameter. Parameters are custom local memory used for your animation nodes, given a resource can be reused in multiple trees.
+
+
+
+ Returns the object id of the [AnimationTree] that owns this node.
+ [b]Note:[/b] This method should only be called from within the [method AnimationNodeExtension._process] method, and will return an invalid id otherwise.
+
+
- Returns whether the given path is filtered.
+ Returns [code]true[/code] if the given path is filtered.
+
+
+
+
+
+ Returns [code]true[/code] if this animation node is being processed in test-only mode.
diff --git a/doc/classes/AnimationNodeExtension.xml b/doc/classes/AnimationNodeExtension.xml
new file mode 100644
index 000000000000..3373b95a33bb
--- /dev/null
+++ b/doc/classes/AnimationNodeExtension.xml
@@ -0,0 +1,38 @@
+
+
+
+ Base class for extending [AnimationRootNode]s from GDScript, C#, or C++.
+
+
+ [AnimationNodeExtension] exposes the APIs of [AnimationRootNode] to allow users to extend it from GDScript, C#, or C++. This class is not meant to be used directly, but to be extended by other classes. It is used to create custom nodes for the [AnimationTree] system.
+
+
+
+
+
+
+
+
+
+ A version of the [method AnimationNode._process] method that is meant to be overridden by custom nodes. It returns a [PackedFloat32Array] with the processed animation data.
+ The [PackedFloat64Array] parameter contains the playback information, containing the following values encoded as floating point numbers (in order): playback time and delta, start and end times, whether a seek was requested (encoded as a float greater than [code]0[/code]), whether the seek request was externally requested (encoded as a float greater than [code]0[/code]), the current [enum Animation.LoopedFlag] (encoded as a float), and the current blend weight.
+ The function must return a [PackedFloat32Array] of the node's time info, containing the following values (in order): animation length, time position, delta, [enum Animation.LoopMode] (encoded as a float), whether the animation is about to end (encoded as a float greater than [code]0[/code]) and whether the animation is infinite (encoded as a float greater than [code]0[/code]). All values must be included in the returned array.
+
+
+
+
+
+
+
+ Returns the animation's remaining time for the given node info. For looping animations, it will only return the remaining time if [param break_loop] is [code]true[/code], a large integer value will be returned otherwise.
+
+
+
+
+
+
+ Returns [code]true[/code] if the animation for the given [param node_info] is looping.
+
+
+
+
diff --git a/scene/animation/animation_node_extension.cpp b/scene/animation/animation_node_extension.cpp
new file mode 100644
index 000000000000..8536da9d0f12
--- /dev/null
+++ b/scene/animation/animation_node_extension.cpp
@@ -0,0 +1,83 @@
+/**************************************************************************/
+/* animation_node_extension.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#include "animation_node_extension.h"
+
+AnimationNode::NodeTimeInfo AnimationNodeExtension::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
+ PackedFloat32Array r_ret;
+
+ GDVIRTUAL_CALL(
+ _process,
+ _playback_info_to_array(p_playback_info),
+ p_test_only,
+ r_ret);
+
+ return _array_to_node_time_info(r_ret);
+}
+
+bool AnimationNodeExtension::is_looping(const PackedFloat32Array &p_node_info) {
+ return _array_to_node_time_info(p_node_info).is_looping();
+}
+
+double AnimationNodeExtension::get_remaining_time(const PackedFloat32Array &p_node_info, bool p_break_loop) {
+ return _array_to_node_time_info(p_node_info).get_remain(p_break_loop);
+}
+
+void AnimationNodeExtension::_bind_methods() {
+ ClassDB::bind_static_method("AnimationNodeExtension", D_METHOD("is_looping", "node_info"), &AnimationNodeExtension::is_looping);
+ ClassDB::bind_static_method("AnimationNodeExtension", D_METHOD("get_remaining_time", "node_info", "break_loop"), &AnimationNodeExtension::get_remaining_time);
+ GDVIRTUAL_BIND(_process, "playback_info", "test_only");
+}
+
+AnimationNode::NodeTimeInfo AnimationNodeExtension::_array_to_node_time_info(const PackedFloat32Array &p_node_info) {
+ ERR_FAIL_COND_V_MSG(p_node_info.size() != 6, AnimationNode::NodeTimeInfo(), "Invalid node info.");
+ AnimationNode::NodeTimeInfo ret_val;
+ ret_val.length = p_node_info[0];
+ ret_val.position = p_node_info[1];
+ ret_val.delta = p_node_info[2];
+ ret_val.loop_mode = static_cast(p_node_info[3]);
+ ret_val.will_end = p_node_info[4] > 0.0;
+ ret_val.is_infinity = p_node_info[5] > 0.0;
+ return ret_val;
+}
+
+PackedFloat64Array AnimationNodeExtension::_playback_info_to_array(const AnimationMixer::PlaybackInfo &p_playback_info) {
+ PackedFloat64Array playback_info_array;
+ playback_info_array.push_back(p_playback_info.time);
+ playback_info_array.push_back(p_playback_info.delta);
+ playback_info_array.push_back(p_playback_info.start);
+ playback_info_array.push_back(p_playback_info.end);
+ playback_info_array.push_back(p_playback_info.seeked);
+ playback_info_array.push_back(p_playback_info.is_external_seeking);
+ playback_info_array.push_back(p_playback_info.looped_flag);
+ playback_info_array.push_back(p_playback_info.weight);
+
+ return playback_info_array;
+}
diff --git a/scene/animation/animation_node_extension.h b/scene/animation/animation_node_extension.h
new file mode 100644
index 000000000000..08e4b4f00c29
--- /dev/null
+++ b/scene/animation/animation_node_extension.h
@@ -0,0 +1,55 @@
+/**************************************************************************/
+/* animation_node_extension.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#ifndef ANIMATION_NODE_EXTENSION_H
+#define ANIMATION_NODE_EXTENSION_H
+
+#include "scene/animation/animation_tree.h"
+
+class AnimationNodeExtension : public AnimationRootNode {
+ GDCLASS(AnimationNodeExtension, AnimationRootNode);
+
+public:
+ virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
+
+ static bool is_looping(const PackedFloat32Array &p_node_info);
+ static double get_remaining_time(const PackedFloat32Array &p_node_info, bool p_break_loop = false);
+
+protected:
+ static void _bind_methods();
+
+ GDVIRTUAL2R_REQUIRED(PackedFloat32Array, _process, PackedFloat64Array, bool);
+
+private:
+ static AnimationNode::NodeTimeInfo _array_to_node_time_info(const PackedFloat32Array &p_array);
+ static PackedFloat64Array _playback_info_to_array(const AnimationMixer::PlaybackInfo &p_playback_info);
+};
+
+#endif // ANIMATION_NODE_EXTENSION_H
diff --git a/scene/animation/animation_tree.cpp b/scene/animation/animation_tree.cpp
index d676e2acf466..dc0a27b00518 100644
--- a/scene/animation/animation_tree.cpp
+++ b/scene/animation/animation_tree.cpp
@@ -417,6 +417,16 @@ bool AnimationNode::is_deletable() const {
return closable;
}
+ObjectID AnimationNode::get_processing_animation_tree_instance_id() const {
+ ERR_FAIL_NULL_V(process_state, ObjectID());
+ return process_state->tree->get_instance_id();
+}
+
+bool AnimationNode::is_process_testing() const {
+ ERR_FAIL_NULL_V(process_state, false);
+ return process_state->is_testing;
+}
+
bool AnimationNode::is_path_filtered(const NodePath &p_path) const {
return filter.has(p_path);
}
@@ -542,6 +552,10 @@ void AnimationNode::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_filter_enabled", "enable"), &AnimationNode::set_filter_enabled);
ClassDB::bind_method(D_METHOD("is_filter_enabled"), &AnimationNode::is_filter_enabled);
+ ClassDB::bind_method(D_METHOD("get_processing_animation_tree_instance_id"), &AnimationNode::get_processing_animation_tree_instance_id);
+
+ ClassDB::bind_method(D_METHOD("is_process_testing"), &AnimationNode::is_process_testing);
+
ClassDB::bind_method(D_METHOD("_set_filters", "filters"), &AnimationNode::_set_filters);
ClassDB::bind_method(D_METHOD("_get_filters"), &AnimationNode::_get_filters);
diff --git a/scene/animation/animation_tree.h b/scene/animation/animation_tree.h
index eb25a8db1b73..08e9f87e8792 100644
--- a/scene/animation/animation_tree.h
+++ b/scene/animation/animation_tree.h
@@ -226,6 +226,10 @@ class AnimationNode : public Resource {
void set_deletable(bool p_closable);
bool is_deletable() const;
+ ObjectID get_processing_animation_tree_instance_id() const;
+
+ bool is_process_testing() const;
+
virtual bool has_filter() const;
#ifdef TOOLS_ENABLED
diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp
index 8a048e9cc302..87f1ce56c1cf 100644
--- a/scene/register_scene_types.cpp
+++ b/scene/register_scene_types.cpp
@@ -37,6 +37,7 @@
#include "scene/animation/animation_blend_space_2d.h"
#include "scene/animation/animation_blend_tree.h"
#include "scene/animation/animation_mixer.h"
+#include "scene/animation/animation_node_extension.h"
#include "scene/animation/animation_node_state_machine.h"
#include "scene/animation/animation_player.h"
#include "scene/animation/animation_tree.h"
@@ -512,6 +513,7 @@ void register_scene_types() {
GDREGISTER_CLASS(AnimationNodeBlendSpace2D);
GDREGISTER_CLASS(AnimationNodeStateMachine);
GDREGISTER_CLASS(AnimationNodeStateMachinePlayback);
+ GDREGISTER_VIRTUAL_CLASS(AnimationNodeExtension);
GDREGISTER_INTERNAL_CLASS(AnimationNodeStartState);
GDREGISTER_INTERNAL_CLASS(AnimationNodeEndState);