Home > Back-end >  How do I programmatically load animations in Godot, separating animation from armature/skeleton?
How do I programmatically load animations in Godot, separating animation from armature/skeleton?

Time:08-07

I'm trying to load 3D animations separately from the 3D armature that will be using the animation. I can import animations from 3d formats (.gltf, .escn) and generate Godot animation files by setting the Animation->Storage option to either ".anim" or ".tres". How do I load one of those files (.anim, .tres) into an AnimationPlayer (or equivalent) from a GD script at runtime?

My ultimate goal is character customization. Different parts are assembled to compose a standard armature/skeleton, and the animation is loaded on top at runtime. However, I would like different animations under standard names. Like different "walk" cycles for different styles of character (the cloaked mage walks differently than the armored knight), but the code should just read something like:

$AnimationPlayer.play("walk")

I shy away from procedural animation because of the performance hit. There may be hundreds of characters on-scene at one time.

CodePudding user response:

You can load the animations you saved (E.g. from the animation panel, Animation -> Save as) and from GDScript load them with load and then call add_animation on the AnimationPlayer passing the the name and the loaded resource as parameters:

AnimationPlayer.add_animation("Walk", load("res://animations/walk.tres"))
  • Related