Change Keyframe Value via Script

Godot Version

4.3

Question

I’m trying to change a Keyframe Value inside my AnimationPlayer via Script.
In my case i want to change the anchor_top Value of my MarginContainer Node(name:"TextBoxContainer).
The animation is called “TextBox_BuildUp_Normal”.
The animation Track for anchor_top has only one Keyframe(Index should be 0).

How can i do that? :expressionless:

Would it be better to use a Tween?

1 Like

You can modify an Animation resource.

Example:

@onready var animation_player: AnimationPlayer = $AnimationPlayer


func _ready() -> void:
	# Get the animation
	var animation = animation_player.get_animation("my_animation")
	# Find the track Panel:anchor_top and modify the first keyframe value
	for idx in animation.get_track_count():
		var path = animation.track_get_path(idx)
		if path.get_concatenated_names().contains("Panel") and path.get_concatenated_subnames().contains("anchor_top"):
			animation.track_set_key_value(idx, 0, 0.5)
	# Play it
	animation_player.play("my_animation")
``
1 Like

Tweening sound’s interesting.
Watched a tutorial and will use it for other stuff.
In this case i will try the keyframe way,
because it works better with my setup.

Thx, this helped allot.
Here my actual Script:

func Open_Textbox(Textbox_name: String,Textbox_avatar: String,Textbox_size: float, SetActiveEvent):
	Active_Event = SetActiveEvent
	self.visible = true
	Play_sound("OpenTextbox")
	var animation = animation_player.get_animation("TextBox_BuildUp_Normal")
	for idx in animation.get_track_count():
		var path = animation.track_get_path(idx)
		if path.get_concatenated_names().contains("TextBoxContainer") and path.get_concatenated_subnames().contains("offset_top"):
			animation.track_set_key_value(idx, 1, Textbox_size)
	animation_player.play("TextBox_BuildUp_Normal")
	await animation_player.animation_finished
	print($".".offset_top)

The print shows the correct Value and now i got another problem.
The Childs of my TextBoxContainer (MarginContainer) aren’t scaling with the Container.

–TextBoxContainer (MarginContainer)
----Textbox_Frame (NinePatchRect)
----Textbox_VBoxContainer (VBoxContainer)

Both Child’s have vertical/horizontal fill active.
What do i oversee?