How to use thinInstanceAdd and createInstance on an imported model (type AbstractMesh)

Hello everyone!

This is an official PG: 123 | Babylon.js Playground

In the official example, the AbstractMesh type can use createInstance, but the compiler tells me that this function does not exist, and also the thinInstanceAdd method. But in fact, AbstractMesh has this method, but using it causes an error."

Babylon.js Playground - #M74DQG

How should I create multiple models in batches?

Thanks.

It is indeed AbstractMesh: Babylon.js docs Is this a bug? Will .meshes ever contain AbstractMesh or are they not always Mesh?

Since I would assume the latter, I would just cast it: https://playground.babylonjs.com/#M74DQG#2

Using as to specify it as the Mesh type, although Mesh has createInstance and thinInstanceAdd, using thinInstanceAdd will cause an error.

Just as a genreal FYI, just in case: This has nothing to do with the types here. Types are just “hints”. If the mesh is a Mesh (instanceof) then it has to work.

re the thin instances: Normalizing the tree can fix the issue: https://playground.babylonjs.com/#M74DQG#3 I would do this in the 3d model app though.

Thank you so much for your guidance! I’ve learned a lot and you’ve saved me from countless detours.:grinning_face:

Babylon.js Playground - #M74DQG#4

This method doesn’t seem to be universal. I switched to another GLB model, and thinInstanceAdd stopped working.

The previous model was a .babylon file type. Now you switched to a .glb file type.

When you load .glb files, there need to happen some conversion stuff (due to different cooridnate systems). In consequence, each glb file you load in Babylon, gets an extra root node. This root node is an empty mesh and all it does is pass down some transforms (rotation, scaling).

So basically, in your code, you are cloning an empty mesh - which perfectly works :grin:


Looking at your new playground, you have animations there, too. I do not think you can easily bake away transforms now. (At least without needing to adapt animations). So if your final goal is to just load a model once and then duplicate it, I think Babylon AssetContainers can be used for that.

I have never used them but when googled “babylon asset container”, there was a complete AI response showcasing how to set them up (incl code) :+1: