Create Instance of imported AbstractMesh

I want to create instances of an Imported mesh, is that possible?

I’m using Typescript and importing a mesh with ImportMeshAsync

const importResult = await SceneLoader.ImportMeshAsync(null, "/assets/", "stuff.gltf", this.scene)

const mesh = importResult.meshes[0]
mesh.isVisible = false
for (var index = 0; index < 2; index++) {
      let newInstance = mesh.createInstance("i" + index)

But TS gives an error since the import result meshes are AbstractMeshes and createInstance()is not a valid method for them.

What would be the correct approach here if I want to make instances of the imported mesh?

From the docs: Instances | Babylon.js Documentation

You can ‘cast’ the type from AbstractMesh to Mesh:

const mesh = importResult.meshes[0] as BABYLON.Mesh


Right, I’ve actually done that, but is this the correct way? Won’t it have any implications like possible errors of some sort?

Not sure if I can explain this clearly: It is your gltf model and if you tested it works, it won’t change. So there won’t be surprising runtime error.

If you don’t know it in advance, you could do a type check like this:

if (mesh instanceof BABYLON.Mesh) {
1 Like