How to add animations .tres to Animation Player (gui tool) programmatically?

52 Views Asked by At

Is it possible to load animations .tres at same time programmatically to Animation Player (gui tool)? I know animations are resources, I did read some godot docs, I didn't find anything related to 'load to animationPlayer tool (gui)'. I have a lot of animations (.tree file extension) in the FilSystem (res://...) and I would like to load or add them to the Animation Player tool programatically if possible.

Just to clear up, I have attach an iamge of the load button of Animation Player too (gui). I would like to use that load way, but programmatically if possible.

enter image description here

1

There are 1 best solutions below

1
On

You don't.

The Animation panel (what you are calling "AnimationPlayer Tool (gui)") is to edit AnimationPlayer nodes, and there is no API to manipulate it.

You might be able to hack your way to it, see Overriding AnimationPlayer functions. But it won't be useful if the AnimationPlayer being edited does not have the animations.

Thus, what you would do is add Animations to an AnimationPlayer which is being edited in the Animation panel.


If you are writing an EditorPlugin, you would get a call to _handles to check if it can handle a Node, so you can implement it to tell Godot your EditorPlugin handles AnimationPlayers:

func _handles(object:Object) -> bool:
    return object is AnimationPlayer

And then you will get a call to _edit:

var edited_animation_player:AnimationPlayer = null

func _edit(object:Object) -> void:
    var animation_player := object as AnimationPlayer
    if is_instance_valid(animation_player):
        edited_animation_player = animation_player

That way you can have a reference to the currently edited AnimationPlayer, which should be the same in the Animation panel.


To add an animation to an AnimationPlayer, you need to get an AnimationLibrary (either get one from the AnimationPlayer or add a new one) and call add_animation on it. The default AnimationLibrary has empty name).

For example:

var animation_library = animation_player.get_animation_library("")
animation_library.add_animation(animation_name, animation)