Scene Loader Append, Get Refernce Outside the Function

Hello!

I am using Append from Babylon’s scene loader in my project to import my meshes. I have noticed that I only have control over my meshes inside the append function. Is there a way to have a reference to the meshes outside the append function?

Here is a playground demo: https://www.babylonjs-playground.com/#NAQLLD#1

Any help is appreciated, thank you!

All meshes are accessible from the scene.meshes array.

You can use some specific functions of scene to retrieve a mesh: getMeshByName, getMeshById, etc

Hello @Evgeni_Popov,

Yes I am aware of what you said, but these only give you control of the meshes inside the append function. Is there a way to access these meshes outside the append function?

You can use these functions anywhere you want, as long as the meshes are imported in your scene before you try to use them - which depends on your game logic.

Hi @YouJ,

By “inside the append function”, I assume you mean the asynchronous callback that is declared:

BABYLON.SceneLoader.Append("", "https://dl.dropbox.com/s/b7bjy69g307d64h/Thonker.glb", scene, function (scene) {
   //Here?
 });

It might make more sense if you switch to the AppendAsync method, which returns a Promise that can be awaited:

await BABYLON.SceneLoader.AppendAsync("", "https://dl.dropbox.com/s/b7bjy69g307d64h/Thonker.glb", scene);
for(const mesh of scene.meshes) {
  //do something with your meshes "outside" of the AppendAsync function
}

Here is a playground showing the changes.

https://www.babylonjs-playground.com/#NAQLLD#2

2 Likes

@jkeys Yes exactly what I wanted!

You are right about inside the append function, that is what I meant.

Thank you so much!

Yw! Recommended reading on programming with Promises:

1 Like