2

New Babylon JS user, looking to get up to speed with this fantastic framework. Have had a play with the Sandbox and online Editor, worked up my own coded model from scratch using the standard components - Box, Sphere etc. My question relates to how to get more complex custom geometry loaded. Very comfortable with 3D CAD - STL/OBJ files, got some exports going from Blender to .Babylon format which import great into Babylon's online Sandbox & Editors. However, I can't seem to get the SceneLoader going to read a file from local C:/ drive. Code extract below:

// Create new Babylon Scene
var scene = new BABYLON.Scene(engine);

// Change scene background color
scene.clearColor = new BABYLON.Color3(1, 1, 1);

// Create and positions a free camera
var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 10, 0), scene);

 // Target the camera to scene origin
 camera.setTarget(BABYLON.Vector3.Zero());

// Attach camera to the canvas
camera.attachControl(canvas, true);

// Define built-in 'box' shape.
var box = BABYLON.Mesh.CreateBox("sphere1", 1, scene);

// Define 'ground' plane
var ground = BABYLON.Mesh.CreateGround("ground1", 100, 100, 100, scene);
            ground.position.y = 0;
//Load local .babylon file from root Dir
BABYLON.SceneLoader.Load("", "Test.babylon", engine, scene);

My model has a standard box for geometry with ground plane. All renders great in Babylon - until I add the SceneLoader line. When I add this I get stuck on the Babylon Loading intro splash screen (rotating Babylon logo). If I comment out the last line of code above the model renders fine with the box. Have had a look at various forum pages on this and wrecked my brain to point of being stuck e.g: http://www.html5gamedevs.com/topic/20924-stlobj-file-loader/ & https://www.eternalcoding.com/?p=313 I believe Google Chrome may be locking out local file links for security, have tried running in -Allow-Local-File-Access mode, still stuck on loading page. Do I need a web server (I wouldn't know where to start!) or can I run Babylon scenes locally?

gman
  • 100,619
  • 31
  • 269
  • 393
MarkGuk
  • 45
  • 1
  • 8
  • Do you get any useful output in the web console? I think it should complain if it it's related to cross origin requests. From what you describe, it sounds like you are on the right track by running Chrome with `--allow-file-access-from-files`. I remember having to fiddle with this as well, hope to get some time later to look into it. You definitely don't need the web server at this stage. – David Basalla Apr 19 '16 at 17:30
  • Yes - Chrome Dev tools console suggests multiple errors in accessing the local file. Three or four red console errors all relating to unable to access the .babylon file on c:/ path. – MarkGuk Apr 19 '16 at 21:08
  • XMLHttpRequest cannot load file:///C:/Javascript/Test.babylon.manifest?1461103045485. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. – MarkGuk Apr 19 '16 at 21:57

3 Answers3

2

First issue posed by OP: Browser is not loading mesh from file system.

Solution: Use a web server such as Simple HTTP Server (Python). The way to do this is slightly different depending on your Python version. To check Python version on Windows, open command prompt and type python --version. Remember the version number for later :)

Setting up simple web server with Python with command prompt:

  • Navigate to the directory with your index.html file in File Explorer
  • Left click on to a blank space inside the path box (where it says This PC > Documents, etc...)
  • Type cmd and it will open Command Prompt in the current directory
  • Enter the appropriate command...

    python -m SimpleHTTPServer [optional port number] if you are using Python 2

    python -m http.server [optional port number] if you are using Python 3

    I usually leave out the port number and simply type python -m http.server.

  • Now open your preferred browser and enter localhost:8000 into your address bar. (8000 is the default port number. If you specified a port, use the number which you specified.) It should load your mesh if the code has no errors.

Second issue posed by OP: SceneLoader.Load method overrides previously loaded meshes.

Solution:

I know this question is a few years old, but in case anyone still has issues with this I hope this answer helps.

Shadowfax
  • 157
  • 9
1

So I’m not 100% sure about this answer, but hopefully it will help. I followed this tutorial (Skip down to the section where the scene gets loaded). One issue is definitely the cross origin thing, the other how you call the SceneLoader.Load method.

When I try the code from the tutorial with regular Chrome I see three warnings in my web console. Two errors about Test.babylon.manifest (using your example file naming) and one about Test.babylon. You can ignore the ones regarding manifests afaik. The important one is the error about Test.babylon itself. So by default Cross origin requests are not allowed and the babylon file does not load (as expected).

Now, when I close Chrome and reopen it by running open -a "Google Chrome" --args --allow-file-access-from-files in the terminal (I’m on OSX Yosemite), and then load the page, the object loads fine. I still see two errors about manifests in the web console, but they can be ignored.

Note how the BABYLON.SceneLoader.Load function is being called. The import process is asynchronous, and the last parameter looks to be a callback function for what to do once the object has successfully loaded, so I don't think you can just pass scene as in your original code. Check out the function docs.

David Basalla
  • 2,996
  • 3
  • 18
  • 22
  • Thanks. I thought the callback was optional as stated in the function docs? The example tutorial is also creating a new scene and camera within the SceneLioader directly. In my case I'm trying to Load into an existing scene, so cut my Args back to remove callback function and reduced to scene. What should do I do in the callback function if I don't want to make a new scene/camera, re-render etc? Also tried the ImportMesh option which doesn't need a callback ( – MarkGuk Apr 20 '16 at 06:18
  • 1
    It's worth noting that my suggestion does not work any more on its own, as of BabylonJS 2.5. The `Tools.LoadFile` method in [babylon.tools.ts](https://github.com/BabylonJS/Babylon.js/blob/407b2d3f0ffe9d7e80ecc670ebab7fc3f14bae9d/src/Tools/babylon.tools.ts#L484-L492) uses an XmlHttpRequest and returns 0 instead of a 200 for local files. As a workaround, I added a special case to also trigger the next step for status code 0. – David Basalla Oct 25 '17 at 12:17
  • Thanks for pointing this out, it's the same in Babylon.js 3.0, I had to change the source (if (request.status >= 200 && request.status < 300 || (navigator.isCocoonJS && (request.status === 0))) changed to: if (request.status >= 200 && request.status < 300 || (request.status === 0)) otherwise local files won't work): – A.J.Bauer Apr 04 '18 at 10:26
1

Ok - porgress. I got it going using SceneLoader.ImportMesh but I had to setup a simple HTTP Server using Python (v3). This link helped a lot: http://www.linuxjournal.com/content/tech-tip-really-simple-http-server-python So you run the Python HTTP server from the directory that the Babylon index.html is based in, and it runs as if HTTP bypassing local file access constraints in Chrome. So my problem is all but answered. I now have my mesh geometry from the Test.Baylon file into my main scene. Still having issues using SceneLoader.Load as the new scene coming in supercedes my original scene and the original geometry disappears. David - I think you're right on the function being needed, although I thought this was optional. As I said, the Tutorial example creates a newScene and renders within the function, in my case I don't know what to do in the function... maybe just 'return'?

MarkGuk
  • 45
  • 1
  • 8