How to script Animation TYPE_ROTATION_3D?

Godot Version

4.1.2

Question

I found next to no documentation regarding to how to animate using gdscipt:

In my script file I create a StaticBody3D and add that as child, then I try to create an animation to get that rotating around Y axis - but its not working, despite any values in the Quaternion

                var animation = Animation.new()
		var track_index = animation.add_track(Animation.TYPE_ROTATION_3D)
		var q = Quaternion(0, PI, 0, 0)
		animation.track_set_path(track_index, ".")
		animation.rotation_track_insert_key(track_index, 1, q)
		animation.loop_mode = Animation.LOOP_LINEAR
		animation.step = 0.1
		var player = AnimationPlayer.new()
		var anim_library = AnimationLibrary.new()
		anim_library.add_animation('rotate', animation)
		player.add_animation_library("alib", anim_library)
		player.set_name('AnimPlayer')
		add_child(player)
		player.play('rotate')

… the animation just not do anything. The object is wont move on screen. There are no errors.

If you use print(player.get_animation_list()) you will notice that the animation’s name is actually alib/rotate. If you fix that, you will also get errors because (0, PI, 0, 0) is not a valid rotation - try var q = Quaternion.from_euler(Vector3(0, PI * 0.25, 0)) instead to see an effect.

(I’m using Godot 4.3, so if I said anything not applicable to you, I apologise.)

1 Like

Thank you! - I just figure out the “alib/rotate” myself, yet that from_euler saved me from lot of head ache.

1 Like

About working solution:

var animation = Animation.new()var track_index = animation.add_track(Animation.TYPE_ROTATION_3D)
animation.track_set_path(track_index, '.')
var q1 = Quaternion.from_euler(Vector3(0, 0, 0))
animation.rotation_track_insert_key(track_index, 0.0, q1.normalized())
var q2 = Quaternion.from_euler(Vector3(0, PI, 0))
animation.rotation_track_insert_key(track_index, 2.0, q2.normalized())
var q3 = Quaternion.from_euler(Vector3(0, 2 * PI, 0))
animation.rotation_track_insert_key(track_index, 3.0, q3.normalized())
animation.length = 3
animation.loop_mode = Animation.LOOP_LINEAR
var player = AnimationPlayer.new()
var anim_library = AnimationLibrary.new()
anim_library.add_animation('rotate', animation)
player.add_animation_library("alib", anim_library)
player.set_name('AnimPlayer')
add_child(player)
player.play('alib/rotate')
1 Like

Thanks for sharing the working code, I’m looking into modifiying animations through script myself atm.

I don’t know if you know, but using tweens is a simpler way to do basic animating, like spinning.