Calling scene.getWorldExtends() inside a onNewMeshAddedObservable callback throws

Hi, not a big issue for me as I could get by by delaying the call to getWorldExtends() but just happened to notice that if you call getWorldExtends() inside the onNewMeshAddedObservable callback.

At a quick glance the following seems to happen:

  • getWorldExtends() calls computeWorldMatrix() for each mesh
  • After the compute, mesh calls its _afterComputeWorldMatrix() which checks for this.hasThinInstances
  • hasThinInstances tries to access the _thinInstanceDataStorage that has not been initialised yet
  • The initialisation will happen quite early on in the constructor of a new mesh, but the onNewMeshAddedObservable is being called by the call to super (AbstractMesh) in the constructor which is the first thing that happens in new Mesh()

Where I noticed this I seemed to also get some error about accessing _LODLevels on undefined as I was loading the scene from a file, but didn’t dig deeper into that.

Sorry for the somewhat messy explanation

Playground repro:
https://playground.babylonjs.com/#6506MQ

The explanations were quite clear!

Unfortunately, I don’t really see a solution to this problem, as the notification happens early (in the AbstrachMesh constructor) and you’re not guaranteed that everything has been already initialized in the mesh instance (in fact, all properties specific to the Mesh class are not (or may not be) properly initialized yet).

It’s “by design” and the user should not access properties (directly or indirectly) not initialized yet in their onNewMeshAddedObservable observer.