How to control imported mesh?

I imported a mesh but the problem is that I need to set its position and rotation in real time. Either controlled by the player with the keyboard or set by an external source.

How does it work? So I import the mesh and then store it in a variable and then acces the mesh using the variable?

I have the same doubts about animation controls.

First of all, please check this thread and demo at CharacterController Demo

As for your question how to control the mesh I’ll try to explain how it is going.

  1. You import you model (usually it is not just one mesh, but a bunch of them). Depending on your needs you may import it with simple standard function, with Asset Container, with Task Manager, with promises etc. All these ways are described in Documentation.

  2. After you import the model, you’ll need to find its Transform node in order to transform it - change position, scaling and rotation of the whole model. Here is small example how to do it - https://playground.babylonjs.com/#05Q46H#11

     BABYLON.SceneLoader.ImportMesh("", "scenes/", "skull.babylon", scene, function (newMeshes) {
     // Set the target of the camera to the first imported mesh
     camera.target = newMeshes[0];
     newMeshes[0].position.x = -20;
     newMeshes[0].scaling.y = 2;
     newMeshes.rotation.z = 1.2;    });
    

So, the function variable newMeshes[0] gets the Transform node (or root node). Actually here newMeshes is a simple array, so you will be able to find all other meshes of your model with the help of this variable.
These are just basics; please search Playground and forum for more examples since this is very popular question. You can also upload your model to Sandbox, open the Inspector and find all nodes of your model with their names, IDs etc.
Here is another example with WASD control and animation - https://www.babylonjs-playground.com/#15EY4F#15

4 Likes

Is there a simpler way? Like for example, if you create a box or other primitive shape from Babylon you can just say:

Box.position.x += 0.1;

What I’m trying to do is to sync the player position from the server to the client because is a multiplayer game.

Yes, it is possible to “simplify” the code as you like:

    var skull =  newMeshes[0];
    camera.target = skull;
    skull.position.x = -2;
    skull.scaling.y = 2;
    skull.rotation.z = 0.2;

Yes, but I wouldn’t be able to access the “skull” outside that scope.

If you want to use it outside this function, you may use, for example
scene.getMeshByName ("test").position.y +=0.01;
But you must be sure that your mesh was loaded before you call it, otherwise it will not work. In this case I would recommend to use Asset Manager or Task Manager.
You’ll find more functions for mesh calling at Scene | Babylon.js Documentation

3 Likes

Do you have any recommendation on a situation where you get null from scene.getMeshByName("sample")?

I see that the scene’s meshes are not fully loaded when I run above code :frowning:

I am looking for a way to wait for it to be loaded so that I can call by its name…

The easiest way to do this is probably to have a flag that gets set when the mesh loads in, something like this: Boolean load flag | Babylon.js Playground (babylonjs.com)

But you could also simply do nothing while scene.getMeshByName() returns null, and then only start doing whatever it is when that value is non-null.

A third approach (slightly more optimal) would be to register whatever behavior you want only after the mesh has loaded, like so: Register behavior after load | Babylon.js Playground (babylonjs.com)

2 Likes

Thanks for your suggestion!

Would this

const myFunction = async (name) => {
    const targetMesh = await scene.getMeshByName("meshName");
}

be right approach?

Your solutions seem amazing! but what I want to do is to just manipulate… perhaps, like a color of a mesh.

So it would be just a one time thing. Doesn’t scene.registerBeforeRender() run at every frame before render?

Thanks anyway! I will keep digging in! :slight_smile: :+1:

Do not use await as it does not return a promise,

2 Likes

In that case, the code you want is even simpler: async/await import mesh | Babylon.js Playground (babylonjs.com)

You want to await the call to ImportMeshAsync (important you use the async function here as it is promise-based rather than callback-based). Afterwards, you can freely call scene.getMeshByName.

Make sure to make the calling function as async as you cannot use await from a synchronous function.

2 Likes