#How to fetch the StandardMaterial handle via its name defined in blender?
9 messages · Page 1 of 1 (latest)
yes, you can do it by swiping assets from Assets<Gltf> after loading the entire GLTF file, which has fields for named_materials, named_scenes etc. etc.
it is not the most ergonomic though. personally I used bevy_asset_loader, preloaded the entire GLTF into a collection and then ran a second loader to put the named files into collections. you don't have to use bevy_asset_loader but I would recommend preloading named handles into a resource. if you want to try something similar it's here:
https://github.com/zainthemaynnn/GRIN/blob/main/src/asset/src/lib.rs
Thanks! I'm using bevy_asset_loader. Could you please elaborate more? I'm using the loader to the load the entire GLB file into a Handle<Scene>. How can I add additional logic to lookup the materials being loaded and referenced by the Scene?
first you have to load every gltf that you are using into a Handle<Gltf> so that all the subassets also get loaded, and dump them into a resource. I did this in a PreLoading load state which runs before the Loading state for all my other assets.
after this, I grabbed my custom asset loader and added an object called GltfSubAsset which allows you to access the objects by name (shown in this file)
https://github.com/zainthemaynnn/GRIN/blob/main/assets/test.assets.ron#L82-L102
to load the sub assets I just used the custom asset loader to look up the asset by using the resource from before.
https://github.com/zainthemaynnn/GRIN/blob/main/src/asset/src/lib.rs#L255-L285
of course this only works if you're using asset keys. (well, this is what I recall from writing this ~6 months ago)
Great. Since I'm currently loading the entire GLB file (not gltf file) into a Handle<Scene>, I wonder if your process of loading it into gltf and swap assets after that process would change the material referred by Handle<Scene>?
I don't think I have a good understanding of how Handle<Scene> and Handle<Gltf> are related
it does not. there are multiple scenes per GLTF file, it qualifies as a sub asset the same way that a material does. you can think of the scene asset as just holding the node structure and transforms while holding references to the other assets from the GLTF master asset, which is a collection of all the different assets from the file. the same material can be shared between scenes and in fact every asset has a unique ID across the entire GLTF file, not for each scene.
GLTF vs glb makes no difference in terms of loading, glb is just compressed.
Ah. Thank you for the explanation. Let's say I have a GLTF file that contains a scene referencing a material called "material.001". When bevy load the scene from the GLTF file, it will also load that material and let the scene referencing that material right? With the GRIN example, you are customizing how bevy load all the assets (including the material "material.001"). So if I can identify a material via its name and modify its properties like color, then the loaded scene's material will have the color changed. Is that correct?
If that is case, does it matter if I process all the assets before loading the scene or after?
I guess my only confusion is whether bevy will create duplicate of the same assets in the scene loading process and your asset loading process