#Help with GLTF assets

3 messages · Page 1 of 1 (latest)

cinder geyser
#

I'm having issues with Gltf assets. I'm trying to use the named_scenes feature specifically but Res<Asset<Gltf>>.get is always returning None.

fn spawn_named_ground(
    mut commands: Commands,
    ass: Res<AssetServer>,
    assets_gltf: Res<Assets<Gltf>>,
) {
    let gltf = ass.load("models/terrain_and_stuff.glb");
    info!("gltf {:?}", gltf);
    // Always None
    if let Some(pack) = assets_gltf.get(&gltf) {
        info!("pack found");
        commands.spawn(SceneBundle {
            scene: pack.named_scenes["Ground"].clone(),
            transform: Transform::from_xyz(0., 0., 0.),
            ..default()
        });
    }
}

I'm able to spawn the entire scene using this, so I think my glb file is ok:

fn spawn_ground(mut commands: Commands, ass: Res<AssetServer>) {
    let gltf = ass.load("models/terrain_and_stuff.glb#Scene0");
    info!("gltf {:?}", gltf);
    commands.spawn(SceneBundle {
        scene: gltf.clone(),
        transform: Transform::from_xyz(0., 0., 0.),
        ..default()
    });
}

Any ideas what I'm missing?
bevy 0.13.2

graceful dagger
#

The asset server only return handles to assets, and not their content, because it returns immediately and kicks off loading in the background. This is why when you try to .get it right after on your first code block, it returns None: the loading hasn't completed because you immediately tried to get the underlying content

#

Instead, what you need to do is call load, keep the handle somewhere (in a Resource or a Component, depending on your use-case), and have a system that checks if the asset is fully loaded, and only then can you .get your asset and manipulate it
https://docs.rs/bevy/latest/bevy/asset/struct.AssetServer.html#method.is_loaded_with_dependencies