Search code examples
godotgdscript

How to get the nodes within an animation in AnimationPlayer?


I'm trying to get all the nodes which are effected by an animation

enter image description here

I know that it's possible to find them through looping tracks:

for track_indx in animation.get_track_count():
        var track_node=get_node(animation.track_get_path(track_indx));
        if(!list.has(track_node))
            list.append(track_node);

but it seems unnecessary iterating over all the tracks just to get the nodes, especially when there are too many tracks attached to just one node.

Is there a better way to achieve this?


Solution

  • There isn't a better way.


    I just want to remind you that the paths of tracks are relative to the root_node of the AnimationPlayer. You can deal with relative NodePaths by getting the node they are relative to, and using get_node from it:

    var animation_player := get_node("AnAnimationPlayer") as AnimationPlayer
    var animation := animation_player.get_animation("AnAnimation")
    var root_node := animation_player.get_node(animation_player.root_node)
    
    var list := []
    
    for track_indx in animation.get_track_count():
        var track_node := root_node.get_node(animation.track_get_path(track_indx))
        if not list.has(track_node):
            list.append(track_node)
    
    for item in list:
        print(item)