Import 2 objects make just the first one usable

Ciao, I have this function

public addCube() {

        const material = new BABYLON.StandardMaterial("defaultMaterial", this.scene!);

        material.alpha = 1;

        material.diffuseColor = new BABYLON.Color3(1.0, 0, 0);

        material.alpha = 0.8;

        const obj1 = BABYLON.SceneLoader.Append("https://dl.dropbox.com/s/kum4ztqput0l3gx/", "demo20%bar.stl", this.scene, () => {

            const mesh = this.scene?.getMeshByName("stlmesh");

            if (mesh != null) {

                mesh.material = material;

            }

            const pointerDragBehavior = new BABYLON.PointerDragBehavior({ dragAxis: new BABYLON.Vector3(1, 0, 0) });

            pointerDragBehavior.useObjectOrientationForDragging = false;

            mesh!.addBehavior(pointerDragBehavior, true);

        });

        const obj2 = BABYLON.SceneLoader.Append("https://dl.dropbox.com/s/kum4ztqput0l3gx/", "demo20%bar.stl", this.scene, () => {

            const mesh = this.scene?.getMeshByName("stlmesh");

            if (mesh != null) {

                mesh.material = material;

            }

            const pointerDragBehavior = new BABYLON.PointerDragBehavior({ dragAxis: new BABYLON.Vector3(1, 0, 0) });

            pointerDragBehavior.useObjectOrientationForDragging = false;

            mesh!.addBehavior(pointerDragBehavior, true);

        });

    }

I see both of the objects imported, but the first one has the correct material and the correct behaviour, the second one is visible but with a gray material and is not draggable. What am I doing wrong?

Thank you

Toni

The second callback will get the first object when doing:

this.scene?.getMeshByName("stlmesh");

as the two objects have the same name.

You should probably put the code const obj2 = ... inside the callback of const obj1 = .... That way, you will be able change the name of the object (mesh.name = stlmesh0") before loading for the second time. By doing so, the 2nd this.scene?.getMeshByName("stlmesh") call will get the right mesh (the one loaded by the SceneLoader.Append call).

3 Likes

Thank you, you saved my day!