SceneLoader.ImportMesh - How do I group the meshes in the callback?

Sorry for what is bound to be a silly question, but ImportMesh returns an array of meshes. I would like to add the ‘object’ that I have imported in a single reference as I need to access it later.

If I import an obj file which was exported from Blender, the array contains several meshes where each mesh in assigned a material BUT it needs to behave like a single mesh, for example if I need to move it. Only the first mesh in the array has the actual model name, the rest have some name that must be assigned when importing, but I think I can assume all the meshes belong to the same object. So I need to parent these so I can treat it as a single object.

If I import a glb file of the same object, I get a Default Light and all the meshes of the object. I don’t want to import the light into the scene. I only want the meshes for the object. They all seem to have the model name as the name on the mesh bit with _1, _2, _3 appended so I think it’s safe to assume the meshes are all part of the same object. Or is this not true. Again I want a single reference to an object so I can refer to it later (and move/rotate as a single object).

Or should I be using some other method to load meshes. The scene is dynamic and objects can be added/deleted/moved/rotated at any time by the user.

You could create a transformNode and then put all the meshes you receive as children of it so that you could manipulate only the transformNode after loading.

Done some more reading and that looks OK. So if I use the AssetsManger to load my 3D models, in the onSuccess callback

  1. Create a new mesh to act as a parent and add as children clones of the meshes just loaded
  2. Add the parent mesh to my own meshes array
  3. Use my own meshes array to find the 3D model and call clone on the parent to create a new object when I want to add a new one to the scene.

I presume the clone iterates through the child meshes, or do I have to do that?

Also, in the callback from the AssetManager task load, I have read that I should disable the meshes that are originally loaded if I don’t want them in the scene. Should I remove them from the scene in onSuccess after I have created the parent and added it to my meshes or does this have some knock on effect?

So clone can clone the children (there is a parameter for that)
AssetsManager will add everything to the scene and you may want to block that in the onSuccess callback

That being said maybe you want to use an assetContainer instead? Asset Containers | Babylon.js Documentation (babylonjs.com)

I’m sorry to have to ask, but I am still getting used to the documentation and it is not the clearest, but how do I block adding to the scene in onSuccess?

I thought the AssetContainer also added to the scene?

Ok…well :frowning:

AssetContainer will not add to the scene unless you call addAllToScene()

For AssetManager, you cannot block them to be added but you can simply disable them (mesh.setEnabled(false))

I did the mesh.setEnabled(false). The interesting thing is when I cloned the mesh, it is added to the scene but I have to enable each child mesh individually rather than enabling the parent (which seems to be enabled already but has no impact on the children).

I will try the AssetContainer. I assume I have to search the meshes array to clone individual objects to the scene because I don’t want to add them all at once?

I also have just seen AssetContainer.Parse which I believe will take a 3D model as a JSON string and create a mesh. This is interesting to me as my current project (using ThreeJS) has all the models defined as js files and there does not appear to be a JSON file importer.

You need to do the setEnabled(false) only for the parent node:) (I believe I commented it somewhere in another topic as well)

If you do not want to add all meshes from an assetcontainer you can do it manually (but we could also think about adding a user predicate to the addalltoscene to let you define what to add)

Parse didn’t do what I thought it did. It looks like I have to add a parser for a 3D model file first. Or?

Also, I was expecting the AssetContainer to have something like addMeshTask to download the 3D model as the AssetManager does. How do I load a 3D model into the AssetContainer?

By the way, thanks for all your help on this. Coming from ThreeJS, there is a mind shift.

Haha no worries , we are here to help:)
You can load models like in this demo: Loading Any File Type | Babylon.js Documentation

1 Like

Thanks. I have that working now. I will do some more experiments.

1 Like