How to move a glb mesh

Hi, I’m trying to import a glb file in the scene then I would like to move it 1 unit every 1 second.
But I don’t have access to this mesh, and I get the undefined error.

This is my code:

        var cup;

        var createScene = function(){

        var scene   = new BABYLON.Scene(engine);
        var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 0, new BABYLON.Vector3(0, 
        0, 0), scene);

        BABYLON.SceneLoader.ImportMesh("", "model/", "108.glb", scene, function (meshes) {
            scene.createDefaultCameraOrLight(true, true, true);
            cup = meshes[0];

        scene.clearColor    = new BABYLON.Color4(0,0,0,0.0000000000000001);
        scene.ambientColor  = new BABYLON.Color3(1, 1, 1);

        return scene;

    //even fom here I don't have access
    var engine = new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true });
    var scene = createScene();

    engine.runRenderLoop(function () {

Welcome to the forum,

you need to do it in the callback exactly like you would so in a c++ lambdas, except you do not have to define what to capture.

You could for instance the line after cup = meshes[0] put the following code:

scene.beforeRender = () => {
cup.position.y += 0.001; // for example

You could else globally wait for cup being available before starting to move it.

Another cool way would be to use ImportMeshAsync and await it to be sur that cup is available the method after.


Thanks for prompt reply. It works! :blush:

1 Like