HeadlinesBriefing favicon HeadlinesBriefing.com

Bevy Animation Guide: Player & Graph Basics

Hacker News •
×

So you just downloaded an animated 3D character .glb file on a free assets website. You have a basic Bevy application running. Now you want to spawn your character in your Bevy app. Easy enough, there's probably a function for that. You look up the basic Animated Mesh example on Bevy's website... and it quickly becomes apparent that things are not as simple as you've hoped. There's a lot of setting things up, animation-related types whose exact purpose is not obvious... You might be able to tweak the examples to fit your needs reasonably fast, but actually forming a mental model of how animation works in Bevy, based on the examples, is going to take some serious pondering.

This post is what I wish I had when trying to understand animation in Bevy a couple weeks ago. I start by building up, step by step, enough of a mental model to deal with basic animation in Bevy. Then I walk you through the official example, explaining how it relates to that mental model, and also holding your hand a bit when encountering methods you might not have encountered yet. If you are comfortable with the basics of Bevy's ECS, you're in this post's target audience.

First Intuition: Let's forget about Bevy and ECS for a second, and think (very abstractly) about what it takes to get an animated 3D model to move. You need two ingredients: A spawned 3D model (or some kind of reference to one), An animation to be played (or some kind of reference to one). So at first, the way you might expect to play an animation is something like that: my_model.play_animation(my_animation); Here's what our mental model looks like so far: Now this is actually somewhat close to the way an actual Bevy function works, namely, the play method from the Animation Player type... or at least it would be, if its self actually referred to a 3D model, and its animation argument actually referred to an animation. Right now, though, it's not clear that they do. But let's look in more detail into the way our two ingredients are represented in Bevy, and maybe we can reconcile our intuition with the way the play function works.

Animation Player: A way to control a 3D model's animations. Say you spawn a 3D model from a .glb file. Ideally, you'd like to refer to it using the ID of the spawned entity, except 3D models are generally not spawned as a single entity, but rather as a hierarchy of entities: To animate your model, you need to refer to it some other way. Bevy has a mechanism to do just that — refer to a 3D model for animation — in the form of the Animation Player type. Animation Player is a Component that is automatically inserted in an Entity... somewhere in the entity hierarchy corresponding to an animatable 3D model, when that model is spawned. Once you have gotten a hold of an Animation Player, you can tell it to play/pause an animation, access the one currently playing... but that's assuming you have animations in the first place! Let's focus on those now.

Animation Graph: A way to store and combine animations. The way animations are represented in Bevy is a little complex, because what you'll generally manipulate is not single animations, but instances of a data structure able to store several animations at once and combine them with one another. That structure is the Animation Graph. I won't get into the details of how Animation Graphs are used here; once you're comfortable with simple animations, you can learn more about general animation graphs with the the Animation Graph example. For now we'll focus on cases where our graph contains only one animation that we're interested in. In those cases, the data that identifies your animation will be: (A reference to) an Animation Graph, An identifier for where your animation actually sits inside the graph — that's what the Node Index type is for.

Putting it Together: Ok, say you have an Animation Player, as well as an Animation Graph and a Node Index. Here's how you connect them to play your animation: Insert a reference to ...