# How to load STL files to Playground?

**URL:** <https://forum.babylonjs.com/t/how-to-load-stl-files-to-playground/30425>\
**Category:** Questions\
**Tags:** mesh\
**Created:** [May 16, 2022, 5:56pm UTC](https://forum.babylonjs.com/t/how-to-load-stl-files-to-playground/30425 "2022-05-16T17:56:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff1](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeff1/32/22134_2.png) [@Jeff1](https://forum.babylonjs.com/u/Jeff1)\
**Post date:** [May 16, 2022, 5:56pm UTC](https://forum.babylonjs.com/t/how-to-load-stl-files-to-playground/30425/1 "2022-05-16T17:56:40Z")

</div>

I have the following babylon code in my html file [https://playground.babylonjs.com/#7CBW04#1006](https://playground.babylonjs.com/#7CBW04#1006)

I have included the following as well in my html:

```auto
   <script src="https://cdn.babylonjs.com/babylon.js"></script>
   <script src="https://cdn.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
   <script src="https://cdn.babylonjs.com/loaders/babylon.stlFileLoader.js"></script>

```

When I run everything, I only see the square and sphere, but not my STL file which I am trying to import using `BABYLON.SceneLoader.Load`. The STL file is in the same folder as my html+javascript. But the STL file does not load in the browser and it gives me the following error:  
`BJS - [13:43:39]: Unable to load from cube.stl: Error in onSuccess callback`

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [May 16, 2022, 7:30pm UTC](https://forum.babylonjs.com/t/how-to-load-stl-files-to-playground/30425/2 "2022-05-16T19:30:38Z")

</div>

Hello and welcome to the community!  
Are you using a local web server to serve your HTML file?

---

<div class="post-metadata">

**Author:** ![Jeff1](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeff1/32/22134_2.png) [@Jeff1](https://forum.babylonjs.com/u/Jeff1)\
**Post date:** [May 16, 2022, 8:21pm UTC](https://forum.babylonjs.com/t/how-to-load-stl-files-to-playground/30425/3 "2022-05-16T20:21:49Z")

</div>

ya, I am just running it on [http://127.0.0.1/](http://127.0.0.1/)

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [May 17, 2022, 4:20am UTC](https://forum.babylonjs.com/t/how-to-load-stl-files-to-playground/30425/4 "2022-05-17T04:20:21Z")

</div>

If your address is cube.stl (so it is in the same folder), the relative address will be ./cube.stl

So the loader call would be something like

```auto
        BABYLON.SceneLoader.Load("./", "cube.stl", engine, function (scene) { 
        });

```

---

<div class="post-metadata">

**Author:** ![Jeff1](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeff1/32/22134_2.png) [@Jeff1](https://forum.babylonjs.com/u/Jeff1)\
**Post date:** [May 17, 2022, 3:07pm UTC](https://forum.babylonjs.com/t/how-to-load-stl-files-to-playground/30425/5 "2022-05-17T15:07:59Z")

</div>

I did that, but the error is still the same. I have the stl file in the same folder as my html file running on local web server.

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [May 17, 2022, 3:28pm UTC](https://forum.babylonjs.com/t/how-to-load-stl-files-to-playground/30425/6 "2022-05-17T15:28:11Z")

</div>

Are you able to load this file in other programs?

---

<div class="post-metadata">

**Author:** ![Jeff1](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeff1/32/22134_2.png) [@Jeff1](https://forum.babylonjs.com/u/Jeff1)\
**Post date:** [May 17, 2022, 3:43pm UTC](https://forum.babylonjs.com/t/how-to-load-stl-files-to-playground/30425/7 "2022-05-17T15:43:53Z")

</div>

Yes, I also tried other stl files and I get the same error. The stl is in the same folder as the following html file:

```auto
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.babylonjs.com/babylon.js"></script>
    <script src="https://cdn.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
    <script src="https://cdn.babylonjs.com/loaders/babylon.stlFileLoader.js"></script>
    <style>
        canvas {
            width: 100%;
            height: 100%;
        }
        html, body {
            margin: 0;
        }
    </style>
</head>
<body>
    <canvas id="renderCanvas"></canvas>
    <script>      
        // get our canvas
        const canvas = document.getElementById('renderCanvas');
        // create BabylonJS engine
        const engine = new BABYLON.Engine(canvas, true);
        const createScene = () => {
            var scene = new BABYLON.Scene(engine);
            var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 10, new BABYLON.Vector3(0, 0, 0), scene);
            camera.setPosition(new BABYLON.Vector3(20, 200, 400));
            camera.attachControl(canvas, true);
            new BABYLON.AxesViewer(scene, 50);
            const localAxes = new BABYLON.AxesViewer(scene, 50);
            camera.upperBetaLimit = (Math.PI / 2) * 0.99;
            // Light
            var light = new BABYLON.PointLight("omni", new BABYLON.Vector3(50, 200, 0), scene);
           //Materials
            var groundMaterial = new BABYLON.StandardMaterial("ground", scene);
            groundMaterial.specularColor = BABYLON.Color3.Black();
            var redMat = new BABYLON.StandardMaterial("ground", scene);
            redMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
            redMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
            redMat.emissiveColor = BABYLON.Color3.Red();
            var blueMat = new BABYLON.StandardMaterial("ground", scene);
            blueMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
            blueMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
            blueMat.emissiveColor = BABYLON.Color3.Blue();
            / *************************************Meshes**************************************** /
            // Ground
            var ground = BABYLON.MeshBuilder.CreateGround("ground", {width:1000, height:1000}, scene, false);
            ground.material = groundMaterial;
            // Meshes
            var redSphere = BABYLON.MeshBuilder.CreateSphere("red", {diameter:20}, scene);
            redSphere.material = redMat;
            redSphere.position.y = 10;
            redSphere.position.x -= 100;
            var blueBox = BABYLON.MeshBuilder.CreateBox("blue", {size:20}, scene);
            blueBox.material = blueMat;
            blueBox.position.x += 100;
            blueBox.position.y = 10;
            var startingPoint;
            var currentMesh;
            // Create utility layer the gizmo will be rendered on
            var utilLayer = new BABYLON.UtilityLayerRenderer(scene);
            // Create the gizmo and attach to the box
            var gizmo = new BABYLON.PlaneRotationGizmo(new BABYLON.Vector3(0,2,0), BABYLON.Color3.FromHexString("#00b894"), utilLayer);
            gizmo.attachedMesh = blueBox;
       
        var getGroundPosition = function () {
            var pickinfo = scene.pick(scene.pointerX, scene.pointerY, function (mesh) { return mesh == ground; });
            if (pickinfo.hit) {
                return pickinfo.pickedPoint;
            }
            return null;
        }
        var pointerDown = function (mesh) {
                currentMesh = mesh;
                startingPoint = getGroundPosition();
                if (startingPoint) { // we need to disconnect camera from canvas
                    setTimeout(function () {
                        camera.detachControl(canvas);
                    }, 0);
                }
        }
        var pointerUp = function () {
            if (startingPoint) {
                camera.attachControl(canvas, true);
                startingPoint = null;
                return;
            }
        }
        var pointerMove = function () {
            if (!startingPoint) {
                return;
            }
            var current = getGroundPosition();
            if (!current) {
                return;
            }
            var diff = current.subtract(startingPoint);
            currentMesh.position.addInPlace(diff);
            startingPoint = current;
        }
    scene.onPointerObservable.add((pointerInfo) => {            
        switch (pointerInfo.type) {
            case BABYLON.PointerEventTypes.POINTERDOWN:
                if(pointerInfo.pickInfo.hit && pointerInfo.pickInfo.pickedMesh != ground) {
                    pointerDown(pointerInfo.pickInfo.pickedMesh)
                }
                break;
            case BABYLON.PointerEventTypes.POINTERUP:
                    pointerUp();
                break;
            case BABYLON.PointerEventTypes.POINTERMOVE:          
                    pointerMove();
                break;
        }
    });
        // The first parameter can be used to specify which mesh to import. Here we import all meshes
        BABYLON.SceneLoader.Load("./", "cube.stl", engine, function (scene) {
            // do something with the scene
            newScene.activeCamera = camera;
            engine.runRenderLoop(function () {
                    newScene.render();
            });
        });
    // Move the light with the camera
    scene.registerBeforeRender(function () {
        light.position = camera.position;
    });
    return scene;
    }
    const scene = createScene();
    engine.runRenderLoop(() => {
        scene.render();
    })
    </script>
</body>
</html>

```

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [May 17, 2022, 3:49pm UTC](https://forum.babylonjs.com/t/how-to-load-stl-files-to-playground/30425/8 "2022-05-17T15:49:18Z")

</div>

> [@Jeff1](#):
>
> ```auto
> BABYLON.SceneLoader.Load("./", "cube.stl", engine, function (scene) {
> // do something with the scene
> newScene.activeCamera = camera;
> engine.runRenderLoop(function () {
> newScene.render();
> });
> });
> 
> ```

You have an unreferenced variable `newScene` here. This is not anything related to Babylon, this is basic JavaScript.

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [May 17, 2022, 3:53pm UTC](https://forum.babylonjs.com/t/how-to-load-stl-files-to-playground/30425/9 "2022-05-17T15:53:18Z")

</div>

See corrected PG here - [https://playground.babylonjs.com/#7CBW04#1008](https://playground.babylonjs.com/#7CBW04#1008)  
Download and try it locally with your cube.stl

---

<div class="post-metadata">

**Author:** ![Jeff1](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeff1/32/22134_2.png) [@Jeff1](https://forum.babylonjs.com/u/Jeff1)\
**Post date:** [May 17, 2022, 4:00pm UTC](https://forum.babylonjs.com/t/how-to-load-stl-files-to-playground/30425/10 "2022-05-17T16:00:57Z")

</div>

Thanks, it works now. I got rid of the SceneLoader.Load and used the SceneLoader.ImportMesh
