Generating Animation Track through script doesn't work anymore

Godot Version

v4.5.1.stable.official [f62fdbde1]

Question

Hello, I have this script that generate animation that shake an object.

@tool
extends AnimationPlayer


@export var target_node2d:Node2D
@export var shake_gen:bool:
	set(v):
		if v and Engine.is_editor_hint() and target_node2d != null:
			generate_shake_animation(_length, _spacing, _magnitude)

@export var _length := 0.5
@export var _spacing := 0.03
@export var _magnitude := 20.0

func generate_shake_animation(length:float, spacing:float = 0.03, magnitude:float = 20.0):
	var animation := Animation.new()
	animation.length = length
	
	var idx := animation.add_track(Animation.TYPE_VALUE)
	var node_path = str(get_parent().get_path_to(target_node2d))
	animation.track_set_path(idx,node_path+":position")
	animation.track_set_interpolation_type(idx,Animation.INTERPOLATION_CUBIC)
	
	animation.track_insert_key(idx,0.0,target_node2d.position)
	var current_position := spacing
	
	var last_track_id := -1
	while current_position < length:
		var mag := lerpf(magnitude,0.0,current_position/length)
		last_track_id = animation.track_insert_key(idx,current_position,
			Vector2(
				randf_range(-mag,mag),
				randf_range(-mag,mag)
			)
		)
		current_position += spacing
	
	if last_track_id != -1:
		if is_equal_approx(current_position, length):
			animation.remove_track(last_track_id)
	
	animation.track_insert_key(idx,length,target_node2d.position)
	
	# append animation
	var animation_library := get_animation_library("")
	if animation_library.has_animation("shake"):
		animation_library.remove_animation("shake")
	animation_library.add_animation("shake",animation)

This script used to work on older version of Godot 4. But now it doesn’t.

If I tried to make position track, it will instead create a new track with exact same name and path.

Why is that? What changes? Is there a way to fix it? I’m not sure where the it goes wrong. I wonder if it’s a bug?

it supposed to look like this:

==============
Another note, I always wanted to create script that can shake object on demand instead of generating animation track like this. But nothing beats those cubic interpolation! However I have no idea how to re-create those interpolated move nvm I found Vector2 has cubic interpolation.
Still!! I will be generating more animation tracks through script in the future other than this shaker, so I still wanted to fix my problem.

Did you change the AnimationMixer.root_node property? AnimationPlayer will use that NodePath as the root node of the Animation track path. Try changing it to:

var node_path = str(get_node(root_node).get_path_to(target_node2d))

You could try using tweens.

1 Like

THanks!! it works. I’m not sure why it works. I don’t think I changed anything of that sort, I don’t do anything that is outside of this script too. get_parent() is just a guess.

I tried to reproduce the problem in new project by putting the node in deep tree with the same configuration which is node2d object as the sibling of the AnimationPlayer (because I thought that’s the cause of the problem) but,… it works even in newer version.

It seems like root_node is ../../../../../../.., but it usually is ... But why, how does this happen (?) It causes problem when I’m trying to save the child as it’s own tscn file. Maybe I accidentally changed it. I tried again in the same project by recreating the animation player and it’s fine now.

I don’t think tween is the same. I can set tween’s interpolation to cubic and ease-in-out but it will not look good in my experience.

But I just found about Vector2.cubic_interpolate(), maybe I’ll experiment with it in the future.