Skip to content

Instantly share code, notes, and snippets.

@ChristopherBiscardi
Created June 3, 2025 19:45
Show Gist options
  • Select an option

  • Save ChristopherBiscardi/1d5fa346b8cdbe5854501379d721bafd to your computer and use it in GitHub Desktop.

Select an option

Save ChristopherBiscardi/1d5fa346b8cdbe5854501379d721bafd to your computer and use it in GitHub Desktop.

Revisions

  1. ChristopherBiscardi created this gist Jun 3, 2025.
    84 changes: 84 additions & 0 deletions main.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    use bevy::{prelude::*, scene::SceneInstanceReady};

    fn main() -> AppExit {
    App::new()
    .add_plugins(DefaultPlugins)
    .add_observer(
    |_trigger: Trigger<SceneInstanceReady>,
    mut query: Query<(
    Entity,
    &mut AnimationPlayer,
    )>,
    move_cube: Res<MoveCube>,
    mut commands: Commands| {
    info!("here");
    for (entity, mut player) in &mut query {
    // commenting out this line "breaks" the player
    commands.entity(entity).insert(
    AnimationGraphHandle(
    move_cube.0.clone(),
    ),
    );
    player.play(1.into());
    info!("a player");
    }
    },
    )
    .add_systems(Update, gltfs)
    .add_systems(Startup, startup)
    .run()
    }

    #[derive(Resource)]
    struct G(Handle<Gltf>);

    #[derive(Resource)]
    struct MoveCube(Handle<AnimationGraph>);

    fn startup(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
    ) {
    let g = asset_server.load("boxy.gltf");
    commands.insert_resource(G(g));
    }

    fn gltfs(
    mut reader: EventReader<AssetEvent<Gltf>>,
    gltfs: Res<Assets<Gltf>>,
    mut commands: Commands,
    mut animation_graphs: ResMut<Assets<AnimationGraph>>,
    ) {
    for event in reader.read() {
    let AssetEvent::LoadedWithDependencies { id } =
    event
    else {
    continue;
    };
    info!(?event, "event");
    let Some(gltf) = gltfs.get(*id) else { continue };

    info!(animations = ?gltf.named_animations);

    let move_cube = gltf
    .named_animations
    .get("MoveCube")
    .unwrap()
    .clone();

    let mut animation_graph = AnimationGraph::new();
    animation_graph.add_clip(
    move_cube,
    1.0,
    animation_graph.root,
    );
    let animation_graph =
    animation_graphs.add(animation_graph);

    commands.insert_resource(MoveCube(animation_graph));

    commands.spawn(SceneRoot(
    gltf.named_scenes.get("Scene").unwrap().clone(),
    ));
    }
    }