SceneLoader glb (glTF): How to get loaded object?

How to load a model and access it?

SceneLoader.Append('./assets/', 'cube.glb', this.scene, (scene) => {
  console.log('Equal:', this.scene === scene); // Result: true

I expected the scene from callback function is the loaded scene model. But nope, it’s actually the scene in which the modal is loaded. Is there no way to get the loaded object?

My current solution is scene.getMeshByName('__root__'). But this is just a workaround. It’s because the glb has that root node. What if you load multiple models? The funciton must return the loaded model, not the same scene you have already access to. It makes no sense.

One of numerous possible examples -


Thanks, it’s much better.

const model = await SceneLoader.ImportMeshAsync('', './assets/cube.glb');
// Now you can work with that.
const modelRoot = model.meshes[0];
if (modelRoot && this.transformNode) {
  modelRoot.parent = this.transformNode;

It’s still a bit weird in babylon.js. For example, why is my model added to the scene when no scene is passed to the function? Can I load models without directly adding it to scene (add it later)?

Btw. it would be better when babylon.js just let gives me a root object (transformNode). Instead of accessing the meshes array (which has 5 entries for my simple cube). Not sure but there is also a transformNode array with only one entry which also works well to e.g. move the model. I would only have one transformNode which is the root object. Not sure, but this seems to be the better solution. The transformNode also has the name of my root object I had in Blender. You can inspect it in chrome (console.log(model)).

const modelRoot = model.transformNodes[0];

Sure, just use AssetContainers. Example - Babylon.js Playground
Docs - Babylon.js docs

Interessting. Babylon is a bit weird. You still need to pass the scene. And if not, babylon uses a global variable to acces the last created scene. I expected something like (pseudo code, similar to three.js):

const model = await Loader.load('path/to/model.glb');

But anyway… It seems to work. Thanks a lot. :slight_smile:

1 Like

Babylon has a bit different logic, but I hope you’ll be quite comfortable with it quite soon. Besides the documentation there is a lot of examples as well.