Getting an animation from a BlendSpace

Godot Version

4.4.dev3

Question

Hi!
I’m trying to get an animation from a BlendSpace, so I can get the animation length. Currently I have a simple setup with a BlendSpace1D containing 3 animations. These blend using an integer from 1-3 (indicating a certain level of animation).
In pseudocode:

var level: int = 1
var animation_length: float = blend_space.get_animation(level).length
await get_tree().create_timer(animation.length).timeout

As stated in this reddit topic I’m currently grabbing the length directly from the AnimationPlayer, which is a decent workaround.

I’ve looked into this post: Programmatically setup Animation trees? - Godot Forums

And learned there is a AnimationNodeAnimation class, which you use to programmatically assign animations to for example a AnimationNodeBlendSpace1D, and in there you can access the Animation. Maybe there is there a way to get an AnimationNodeAnimation from the BlendSpace1D?

I get access to the blendspace using graggu’s code:

var blendTree := self.animationTree.tree_root as AnimationNodeBlendTree
var blendSpace := blendTree.get_node("Block") as AnimationNodeBlendSpace1D

Where “Block” is the name of the BlendSpace1D.

Any ideas?

You can use AnimationNodeBlendSpace1D.get_blend_point_node() to get the AnimationRootNode which is the parent class of all other AnimationNode*. Then you can check if the node is an AnimationNodeAnimation, get the animation name with AnimationNodeAnimation.animation from it, and get the Animation resource with AnimationMixer.get_animation().

Something like:

var blendTree := self.animationTree.tree_root as AnimationNodeBlendTree
var blendSpace := blendTree.get_node("Block") as AnimationNodeBlendSpace1D
var animationNode = blendSpace.get_blend_point_node(0)
if animationNode is AnimationNodeAnimation:
    var animationName = animationNode.animation
    var animation = animationTree.get_animation(animationName)
    print(animation.length)

So it is only the name then. I think it’s a bit too much effort for my application, but at least I now know I can get it from the animation tree directly.

var animation_length = animation_tree.get_animation("character_animations/" + type + "_" + str(level)).length
await get_tree().create_timer(animation_length).timeout

This works for me!