What is an alternative for deprecated SceneLoader.ImportMeshAsync?

Deprecated in this PR: Make SceneLoader module level functions PascalCase and add GetRegisteredSceneLoaderPluginMetadata by ryantrem · Pull Request #16154 · BabylonJS/Babylon.js · GitHub

Now linter gives a warning on using deprecated ImportMeshAsync but I don’t see a function that replaces it.

You can use BABYLON.appendSceneAsync("path/to/model.glb", scene).

See the announcement SceneLoader (and glTF) options for more details about the changes :slight_smile:

AppendSceneAsync doesn’t return meshes that are appended to the scene. It seems useless in my case. I want to have a reference to the appended mesh to manipulate it, so I don’t consider it a replacement for ImportMeshAsync.

In this case it is better to use LoadAssetContainerAsync, it has all needed options.
Note that loadAssetContainerAsync (starting with the small “l”) is deprecated.

2 Likes

Refactored to use LoadAssetContainerAsync and manually appending loaded stuff to a scene — all works. Thanks.

3 Likes

Hi @Andriy_Lysnevych !
could you give a small sample (playground) how you use LoadAssetContainerAsync to load glb manually (perhaps from arrayBuffer)? Thanks in advance!
Tim

@TimT77 Try converting the ArrayBuffer into a file.

You can also use a plain ArrayBufferView as the sceneSource, provided that you also specify the plugin extension to use.

LoadAssetContainerAsync(arrayBufferView, scene, { pluginExtension: ".glb" });
1 Like

@Kochab
Many thanks for reply!
I tried this before, but with this i got “Not allowed to load local resource: file://file.glb/”.

Hi @alexchuber !
Many thanks for reply!
How do i get a arrayBufferView?
I have a normal arrayBuffer with the glb as content.
How do i “convert” the arrayBuffer into a arrayBufferView?

If you’re running your babylon code on localhost, then localhost should serve the file. So if file.glb is in the same directory as index.html, then you should be able to do something like http://localhost:8080/file.glb. Note 8080 is an arbitrary port number I chose, and yours could be different. Or just load index.html directly in the browser as a local file file:///path/to/index.html, and then you’ll be able to load local files.

Super easy :smiley:

LoadAssetContainerAsync(new Uint8Array(arrayBuffer), scene, { pluginExtension: ".glb" });
2 Likes

@alexchuber
My hero of the day! :bouquet:

I think, the converting is correct now: the glb is loading in a playground:
Load GLB from arrayBuffer

But: i got a error message in my real project:
“Loading from ArrayBufferView can not be used with plugins that don’t support binary loading.”
Do you know the reason for that?

My best guess would be that there’s a missing import somewhere, but not sure without seeing a repro. Are you registering the plugin extensions like in this code snippet Babylon.js docs ?

Yes, indeed! :face_with_diagonal_mouth:
The missing import (loaders) caused the loading issues!

Sorry for wasting your time! :see_no_evil_monkey:

Now, the import works as expected.

But i have another question (if i may):
As you can see after downloading the file:
Download GLB (source: arrayBuffer)

there is a JSON header / meta data inside the glb which i need to extract for further doings.

Is there a possibility to get this header via the loader?
Or how to extract this JSON header from the glb?

Depending on what you need the glTF metadata for, you could do one of a few things:

  1. Use the onParsed callback option from LoadAssetContainerOptions:
await LoadAssetContainerAsync(source, scene, {
  pluginOptions: {
    glTF: {
      onParsed: (loaderData) => {
          console.log(loaderData.json.extras.myMetadata)
      }
    },
  },
});
  1. Create your own glTF loader extension. Read more about how to do that here.

Babylon has as many utilities like this as there are stars in the sky /s :laughing: You should lean on our documentation site to navigate these.

1 Like

@alexchuber Thank you so much for your help !!!
That’s exactly what i was looking for! :+1:

I shall heed your advice and will have a look to the documentation to this topic!

It’s a pleasure to have people like you here!
Have a nice day! See you next time…

1 Like