Skip to content

Commit

Permalink
Expose the current animation tree and player to AnimationNode
Browse files Browse the repository at this point in the history
Exposes the `tree` and `player` members of `AnimationNode::State` available
in C++ to scripts, enabling their use in custom nodes.

Without this various obtuse and potentially problematic workarounds
are required.

For example, with this patch, the following code can be changed from
this (which has undesired side effects):

```gdscript
# seek the animation to its beginning, which will cause `blend_node` to
# return its length
duration = blend_node(animation_node_name, animation_node, 0.0, true, false,
	0.0, AnimationNode.FILTER_IGNORE, true)
```

To the more obvious and direct form:

```gdscript
duration = get_player().get_animation(animation_name).length
```
  • Loading branch information
maiself committed May 27, 2022
1 parent 9d28e10 commit 53930d4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions doc/classes/AnimationNode.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@
Gets the value of a parameter. Parameters are custom local memory used for your nodes, given a resource can be reused in multiple trees.
</description>
</method>
<method name="get_player" qualifiers="const">
<return type="AnimationPlayer" />
<description>
Gets the [AnimationPlayer] referenced by the [AnimationTree] currently processing this node. Returns [code]null[/code] when called outside of [method _process], as multiple trees may reference a single [AnimationNode] resource.
</description>
</method>
<method name="get_tree" qualifiers="const">
<return type="AnimationTree" />
<description>
Gets the [AnimationTree] currently processing this node. Returns [code]null[/code] when called outside of [method _process], as multiple trees may reference a single [AnimationNode] resource.
</description>
</method>
<method name="is_path_filtered" qualifiers="const">
<return type="bool" />
<argument index="0" name="path" type="NodePath" />
Expand Down
15 changes: 15 additions & 0 deletions scene/animation/animation_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,18 @@ Ref<AnimationNode> AnimationNode::get_child_by_name(const StringName &p_name) {
return Ref<AnimationNode>();
}

AnimationTree *AnimationNode::get_tree() const {
ERR_FAIL_NULL_V_EDMSG(state, nullptr, "get_tree() called outside of _process()");

return state->tree;
}

AnimationPlayer *AnimationNode::get_player() const {
ERR_FAIL_NULL_V_EDMSG(state, nullptr, "get_player() called outside of _process()");

return state->player;
}

void AnimationNode::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_input_count"), &AnimationNode::get_input_count);
ClassDB::bind_method(D_METHOD("get_input_name", "input"), &AnimationNode::get_input_name);
Expand All @@ -426,6 +438,9 @@ void AnimationNode::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_parameter", "name", "value"), &AnimationNode::set_parameter);
ClassDB::bind_method(D_METHOD("get_parameter", "name"), &AnimationNode::get_parameter);

ClassDB::bind_method(D_METHOD("get_tree"), &AnimationNode::get_tree);
ClassDB::bind_method(D_METHOD("get_player"), &AnimationNode::get_player);

ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_filter_enabled", "is_filter_enabled");
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "filters", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_filters", "_get_filters");

Expand Down
3 changes: 3 additions & 0 deletions scene/animation/animation_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ class AnimationNode : public Resource {

virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name);

AnimationTree *get_tree() const;
AnimationPlayer *get_player() const;

AnimationNode();
};

Expand Down

0 comments on commit 53930d4

Please sign in to comment.