Adding an animation to the AnimationPlayer via code

Godot Version

4.2

Question

Is there a way to manipulate animations via code? In the below example, there’s a few things I’d like to accomplish:

  1. Dynamically set the Sprite2D to be animated based on the current “active” node.
  2. Add an animation that simply rotates the sprite 4 degrees and back to 0 degrees in 0.2 seconds.
  3. Play that animation after creating it.

The node tree looks like this, where the active_node is going to be one of these Opt# that gets passed into it. Keep in mind each Bag sprite is different from node to node.

	var texture = active_option.get_node("Bag")
	var animation = Animation.new()
	var texture_track_index = animation.add_track(Animation.TYPE_VALUE)
	animation.track_set_path(texture_track_index, "texture")
	animation.track_insert_key(texture_track_index, 0.0, texture)
    var rota_track_index = animation.add_track(Animation.TYPE_VALUE)
	animation.track_set_path(rota_track_index, "rotation")
	animation.track_insert_key(rota_track_index, 0.0, 4)
	animation.track_insert_key(rota_track_index, 0.2, 0)
	
	var animation_player = AnimationPlayer.new()
	animation_player.add_animation("anim", animation)
	animation_player.play("anim")

The code above has been taken from various resources online for programmatically adding an animation to the animation player, however it seems that .add_animation() which was present in Godot 3, doesn’t appear in 4? Is there an equivalent function to add animations to the animation player in Godot 4?

It appears, that setting the animation name and referencing it gets me one step closer to playing the animation

animation.resource_name = "anim"
animation_player.play("anim")

but the animation player still doesn’t recognize the animation, which I assume still requires some add_to_library or add_animation method to be called first.

func test() -> void:
    var anim_lib = AnimationLibrary.new()
	for option in bag_options:
		var animation = Animation.new()
		animation.step = 0.1
		animation.length = 0.2

		var texture = option.find_children("Bag").back()
		var sprite_path: NodePath = scene_root.get_path_to(texture)
		
		var sprite_vis_path: NodePath = "%s:visible" % sprite_path
		var vis_track_index = animation.add_track(Animation.TYPE_VALUE)
		animation.track_set_path(vis_track_index, sprite_vis_path)
		animation.track_insert_key(vis_track_index, 0.0, true)
		
		var sprite_texture_path: NodePath = "%s:texture" % sprite_path
		var texture_track_index = animation.add_track(Animation.TYPE_VALUE)
		animation.track_set_path(texture_track_index, sprite_texture_path)
		animation.track_insert_key(texture_track_index, 0.0, texture)
		
		var sprite_rotation_path: NodePath = "%s:rotation" % sprite_path
		var rota_track_index = animation.add_track(Animation.TYPE_VALUE)
		animation.track_set_path(rota_track_index, sprite_rotation_path)
		animation.track_insert_key(rota_track_index, 0.0, 4)
		animation.track_insert_key(rota_track_index, 0.2, 0)
		
		anim_lib.add_animation(option.name, animation)
    animation_player.add_animation_library("bag_anim", anim_lib)

This is as far as I’ve gotten with a scripted version of creating animations, adding it to a library, then adding it to the animation player. For what its worth it appears to be working, the last thing left is to figure out why the textures are invisible. I have to assume its something to do with the animation missing some keys with the texture.

If I haven’t misunderstood your problem, a way going about solving it might be using tweens instead of animations.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.