How to move all meshes imported via scene locally

So I imported a bunch of meshes via its babylon file and I am manually importing the meshes.

        BABYLON.SceneLoader.LoadAssetContainer('./assets/scene/test/', 'Wasteland_Base_Demo.babylon', this.Game.scene, (container) => {
            console.log('our import', container);

            container.meshes.forEach((mesh, index) => {
                setTimeout(() => {
                console.log('the mesh', mesh);

                // mesh.position = new BABYLON.Vector3(0, 0, 0);
                mesh.isVisible = true;

                console.log('hey', mesh.getPositionExpressedInLocalSpace(), mesh.position);

                this.Game.scene.addMesh(mesh);

                // mesh.position.x -= 255;
                // mesh.position.z -= 255;
                // mesh.position.y -= 15;
                }, 25 * index);
            });
            // do something with the scene
        });

How do I move all the meshes, keeping them locally in their position relative to the scene, but moving the absolute position of everything to my terrain? ( where my player is )

pinging @JohnK

Hi Abstract:

This is how I usually solve this. I am not sure if this is best practice though.

var mesh = BABYLON.SceneLoader.ImportMesh("", "scenes/", "mdl_roterBaron_v064_awhg.babylon", scene, function (newMeshes) {
                    for (mesh in newMeshes){
                    newMeshes[mesh].scaling = new BABYLON.Vector3(1, 1, 1);
                    newMeshes[mesh].position = new BABYLON.Vector3(0,1,0);
                }
				});

this is a bit different because this is a scene that has local positions that need to be saved. I don’t want their position to all be exactly the same. A thought I had would be to put a dummy mesh in the middle of the scene, give it a name then reference it in my code, find the difference between all the other meshes position and the center dummy mesh(origin of scene) and then set the position of the center dummy mesh and apply the base difference of the positions to the meshes locally to the center dummy mesh.

When I do incremental position deduction (mesh.position.x -= 10;) it works as intended for meshes that don’t have a rotation. But meshes with rotations and its position will not move as intended. Is their a way to move a mesh with the offset of a rotation in case it has one?

First questions is did you create the meshes you are importing? If not can you guarantee they were all saved at the same scale?

Scaling can be a major issue. Take this PG https://www.babylonjs-playground.com/#QY1WYT#0

and look at line 28, the rabbit has been reduced in scale to give it an appropriate size relative to the dude. Comment out line 28 and see what happens.

Changes in scale will also affect relative positioning.

You may want to read What is the standard length unit in a scene

I did not create the meshes, I don’t think its a scaling issue? If I deduct all positioning from a static number, majority of the props move properly, its the ones that have rotation that go out of wack. I am not doing any manual scaling, and the scene looks proportional to my player.

Just to clarify do the ones with rotation

a: move to the correct location but have the wrong rotation?
b: move to the wrong location.

they move to the wrong location, when i reduce x by 10 its like its reducing x by 40

Can you import just one of the models that do not position correctly into a playground and show the issue?

https://doc.babylonjs.com/resources/external_pg_assets

I figured it out, it was because the scene was importing meshes with parents, i remove all the parents and set absolute positions now.

1 Like