How to get/set animation and progress from code using an Animation StateMachine?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 2plus2makes5

I’m using an AnimationTree with an AnimationNodeStateMachine, how to get/set the current animation and its progress from code?

If i get the animationplayer node and print its current animation it writes nothing, while if i print current animation position it always gives me 0.

:bust_in_silhouette: Reply From: deaton64


So this what I’ve done:

extends AnimationTree
var _playback: AnimationNodeStateMachinePlayback
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
    _playback = get("parameters/playback")
    active = true	# make the animation active
func _make_active(myanimation) -> void:
func _whatisplaying():
   return _playback.get_current_node()

I then call _make_active("laugh") to play the animation.
To see what is playing: _whatisplaying()

Thanks! :smiley:

Is there any way to get/set the progress of the animation? I want to sincronize the animation in multiplayer, so i want to get/set not just the animation but also the exact point in the animation, if i use current animation position of animationPlayer it gives me 0.

2plus2makes5 | 2020-05-21 17:37

Yeah, not sure about that one. I’ve tried as well to get the position, but I also get 0.

deaton64 | 2020-05-22 07:43

Thanks again, i’ll try opening an issue.

2plus2makes5 | 2020-05-22 08:41

What I’ve done is to export a float variable from a script and set the first key frame on the animation to 0 and the last keyframe to whatever length you have for that anim. If your animation is 0.8, you would put a keyframe with 0.8 value at the end.

This way, the anim player will set the value of that var to the position of the animation. This works great if you have a blendtree and are changing the time scale of the animation.

jpscave | 2021-09-22 01:44