SceneLoader.Append shows error but loads fine

I have two questions: 1. I dont know if its a bug or what, but my models loads fine and the error function its called

2: How can i know, when the model its full loaded?

this is my code

function init(gltfData) {

    var canvas = document.getElementById('preview');

    var engine = null;

    var scene = null;

    var sceneToRender = null;

    var createDefaultEngine = function() { return new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true }); };

    var createScene = function() {

        var scene = new BABYLON.Scene(engine);

        BABYLON.SceneLoader.Append("", "data:" + gltfData, scene,

            function() {

                scene.createDefaultCamera(true, true, true);

                scene.activeCamera.alpha += Math.PI / 2;

                SceneLoader.ShowLoadingScreen = false;

                scene.clearColor = new BABYLON.Color4;

            },

            function() {

                console.log("### Loading 3d models")

            },

            function(error) {

                console.error("### Error loading model")

                console.error(error)

            });

        return scene;

    };

    engine = createDefaultEngine();

    if (!engine) throw 'engine should not be null.';

    scene = createScene();

    sceneToRender = scene

    engine.runRenderLoop(function() {

        if (sceneToRender) {

            sceneToRender.render();

        }

    });

    BABYLON.Tools.CreateScreenshotUsingRenderTarget(engine, scene.activeCamera, { width: 500, height: 300 }, function(img) {

        console.log(img)

    })

    // Resize

    window.addEventListener("resize", function() {

        engine.resize();

    });

}

Hello and welcome!

You can use F12 debugger to break on all exceptions. This way you will see which code generates the error

To know when all the scene is ready you can run:

scene.executeWhenReady(() => {
 // Here you are sure that the scene is ready to render
})
1 Like

Like i show in the picture, i can print de error, but i cand find the line that has de error, it is from the model? on the success function? idk… when i make console.error(error) prints an object with a bunch of attributes

That :slight_smile:

Append’s onError handler returns the scene, a message, and an optional exception object. If you can log the message, that will help a lot more than logging the scene :slight_smile:

2 Likes

Thanks! The error was inside the Success function, I was calling SceneLoader.ShowLoadingScreen instead of BABYLON.SceneLoader.ShowLoadingScreen, and the correct use is before calling Append

      BABYLON.SceneLoader.ImportMesh("", "", Url, scene,
                    function (newMeshes) {

                    },onError = function () {
                       alert();
                    });

It is used this way. I felt the need to share it for those who don’t know.

Yes, though that onError = is a bit weird.

BABYLON.SceneLoader.ImportMesh("", "", Url, scene,
    function (newMeshes) {
        // success
    },
    function () {
        // error
    });
1 Like