Importing Meshes

Hello, I am working with a TypeScript program, and I am currently using a normal Box mesh as a placeholder for another 3D model. Basically, I am working inside of a constructor and currently I set "this.deviceMesh = BABYLON.MeshBuilder.CreateBox(“deviceMesh”, {
size: 0.25 })
However, when I try to replace the MeshBuilder with a SceneLoader ImportMesh and set this.deviceMesh to getChildMeshes, it does not work. How can I easily import the mesh and set the variable(which is of type BABYLON.Mesh) to it?

Hello @Guha_Sriram , how are you doing?

For most API loading a mesh is an asynchronous process and so you will only received the resulting mesh once the callback is called (when using ImportMesh) or you wait on the returned promise object (when using ImportMeshAsync).

Also, most those methods will give you a result of type AbstractMesh, not a Mesh. If you want to store it as a Mesh you can try to cast it as I show in the following playground:

Available Mesh BoomBox | Babylon.js Playground (babylonjs-playground.com)

If you don’t want to do the casting you can store the AbstractMesh type instead.

If this does not solve your issue can you please provide e Playground example so we can use it as a reference?

3 Likes

Thank you for your answer. I’m not sure if I can provide a Playground example since this 3D render I’m trying to do in the program is part of a much larger project, and I don’t think I could accurately represent that on a Playground. Also, I’m not able to see your casting on the Playground link you sent. How would I go about casting an AbstractMesh to an AbstractMesh or a Mesh? Thanks very much.

If it is just about the type, you can cast in typescript (Type Casting). If it is a technical question, we will need some reproduction to be able to help.

Ok, thanks. I will try to reproduce the best I can.
Basically, I have an object constructor that has the property ‘deviceMesh’, which is of the type ‘Mesh’.

let carMesh;
      BABYLON.SceneLoader.ImportMesh("", "../../assets/", "updatedcar.glb", scene, (newMeshes) => {
          carMesh = newMeshes[0].getChildMeshes();          
      });    
      this.deviceMesh = carMesh;

This does not compile, and I have tried several casting approaches such as setting deviceMesh to an AbstractMesh(where I get the error ‘Type undefined is not assignable to type AbstractMesh on the last line’), as well as casting carMesh to different types.

This will not run as

this.deviceMesh = carMesh;

will be executed before your mesh has been loaded

See the difference between

You might want to consider using

3 Likes

Hello just checking in if you’d like any more help on this? @Guha_Sriram