Using Babylon JS and Loading Files in Node JS

Hello!

I am trying to use Babylon JS in a node application. There are two issues that I am facing:

  • Using require: I am currently developing with import statement, since in the GitHub repo I couldn’t locate the files in imports here: Babylon.js docs
  • FileReader in fileTools.ReadFile: Node JS does not have built-in FileReader class. Is there a better way to load models (i.e. multiple files for glTF)

Here’s the test script in index.js

const engine = new NullEngine();
const scene = new Scene(engine);

fs.readFile('file.glb', (err, data) => {
  if (err) throw err;
  SceneLoader.AppendAsync("", new File([data], scene).then(() => {
    scene.createDefaultCamera(true, true, true);
    console.log("scene loaded");
  });
});

I would appreciate any discussion and help.

Thanks!

you are mixing to concepts here , frontend and backend.

Nodejs is backend , there is no problem using any nodejs code for nodejs purposes , meaning backend scripts/code.

All files that are front end files of your application would not use any nodejs code. They would communicate with the backend via requests.

Of coure a front end can make a request to a file using a *relative path , browser technology handles this via requests and so babylonjs does not need the fs module to do this , it would be using the built in file request methods of javascript engines.

  • ( on the same domain ) or using absolute paths to some other domain (so long it is not restricted by CORS ) , but they remain sandboxed browser requests non the less, You cannot execute file system code ( like reading a directory or file ) from a front end application.
1 Like

Thanks. Maybe I should better explain what I am trying to do.

I have a set of model files stored in a database, some of which have a mesh with a particular name. If a mesh in the model satisfies a specific condition, I will store the pose matrix of that mesh, dispose it, then serialize the model again to override the subject model.

If the models were glTF, I could try to mathematically obtain the pose matrix of the mesh by interpreting its JSON fields, however I’d prefer utilizing Babylon JS for that, also files are stored as glb and obtaining the glTF file from that would be troublesome.

I would like to implement a Node JS function to be called by backend between acquiring the blob/file and exporting the newly generated scene hierarchy. I am basically trying to avoid implementing getPoseMatrix in C# :slight_smile:

I would be glad for any other idea. Thank you!

ok sorry i didnt read what you were asking in detail , im not sure why you note you cant use ‘require’ ?
you have to require the fs module in some way , i use the promises version myself , eg

const fs = require("fs").promises;

i see node documentation showing this :

const fs = require('node:fs');

i prefer the promises version because i can use await , eg :

 try {
    var result = await fs.readFile(filePath, "utf-8");
    return result;
  } catch (error) {
    logger.error(error.message);
    logger.info("error trying to read file " + filePath);
  }

Oh that require detail was a little unnecessary. What I wanted to point out was I cannot find “dist/preview release” and therefore cannot import (require) them in my code.

if the file is not on disk - you could always retrieve manually - ie:
Babylon.js/packages/dev/loaders/test/integration/babylon.sceneLoader.test.ts at master · BabylonJS/Babylon.js (github.com)

If your model is across multiple physical files - then your approach of loading data with fs will not work. if you are loading only .glb (single file) then you need to specify “binary” encoding (instead of utf-8). The sceneLoader appendAsync accepts File, but also data:... and array buffer view.

The actual code will depend on your version of Node.js as well as the physical file locations - assuming something like an expressjs server - they are deployed with the server. If you are using something like cloud storage to serve via HTTP (like a gateway) then you can bypass HTTP and access cloud storage directly from Node as well.

1 Like

cc @RaananW as the paths have changed I guess.

I was able to proceed with passing the buffer. Thank you!

Update to the docs:

Update serverSide.md by RaananW · Pull Request #1107 · BabylonJS/Documentation (github.com)