Return imported mesh

Hi,

I’d like to return the mesh imported with the BABYLON.SceneLoader.ImportMesh function to use it in another context but i don’t know how to do that. I’m sure it’s quite simple but could you explain me how.

Thanks

What do you mean with “context”? a different scene?

No, i’m using the wrong word. I want that the function shall return the mesh to change its color, its position after in another function.

You can access/store a ref to the meshes ( and other things ) that were loaded in the onSuccess callback outlined in the docs.

Thanks for your answer but I think it does not work. Maybe I didn’t understand.

Here’s my playground : https://playground.babylonjs.com/#MJWKQ0#2

the onSuccess callback function is not totally finished before I search my mesh, so it does not find the mesh I’ve loaded.

That’s because you call profil_gltf and then tried to log right after. When you are trying to log “Lame”, the onSuccess has not fired yet. https://playground.babylonjs.com/#MJWKQ0#3 this is one way to go about it, making profil_gltf return a Promise instead and await it / resolve the Promise inside the onSuccess.

There is also importMeshAsync which you could tweak your code to get a similar result to what I did above.

1 Like

It works fine on the playground but not in my code and i really don’t understand why.

The console give me the answer “Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules”

That’s because await is only valid in async functions. Make sure your function is async, make sure you are not using await in the top level of your code, and if you do, make sure it is a module.

You can read about it here - async function - JavaScript | MDN

Thanks, it works…

1 Like