Nodejs threejs GLTFExporter (server side) Blob issue

gltf-exporter

#1

Hi.

First to thanks in advance to who will waste his time under this question.

: )

I am trying to make a DAE2GLB convertor with very specific optimizations.
# node myConvertor.js input.dae output.glb

I am currently able to open a dae file or gltf/glb file and modify using three modules.

But my problem comes when I try yo use the GLTFExporter in the same way I used ColladataLoader or GLTFLoader because it depends on Blob objects.

UI have look into the other available exporters and ColladaExporter doesn’t use Blobs for anything, the same for STLExporter or ObjectExporter.

Unfortunately as far as I know Blobs are only available under browser context. I tried to search for a valid solution for nodejs on server side and Blob support but I didn’tBest regardsI really need to concluded anything.

I am able to open a DAE,GLTF orI really need to GLB file and modify reIits content (as for example custom buffergeometry vertex attributes) andsave everything into a JSON file.

I would prefer to use GLTF binary format (GLB) because it’s faster.

I am also aware that thre are browser emulation contexts for nodejs as Electron, but I would prefer not using it.

The question is if it would be possible to make GLTFExporter.js usable in a nodejs server runtime context

Best regards


#2

I’ve used the following hack to run GLTFExporter in Node.js —

const THREE = require('three');
const Canvas = require('canvas');
const { Blob, FileReader } = require('vblob');

// Patch global scope to imitate browser environment.
global.window = global;
global.Blob = Blob;
global.FileReader = FileReader;
global.THREE = THREE;
global.document = {
  createElement: (nodeName) => {
    if (nodeName !== 'canvas') throw new Error(`Cannot create node ${nodeName}`);
    const canvas = new Canvas(256, 256);
    // This isn't working — currently need to avoid toBlob(), so export to embedded .gltf not .glb.
    // canvas.toBlob = function () {
    //   return new Blob([this.toBuffer()]);
    // };
    return canvas;
  }
};

// https://github.com/mrdoob/three.js/issues/9562
require('three/examples/js/exporters/GLTFExporter');

At the time I didn’t find a fix for canvas.toBlob(), so you cannot go directly to .glb, but export to .gltf should work. There are various tools (e.g. https://glb-packer.glitch.me/) for packing glTF to GLB if needed.


#3

Hi.

Many many thanks for the information.

Could you please attach a full example?

I would prefer to obtain a glb instead a gltf but it is ok.

Best regards


#4

I can’t write a complete DAE Converter for you, but here’s a (more) complete example of the code above: https://gist.github.com/donmccurdy/9f094575c1f1a48a2ddda513898f6496

For a more general purpose converter, you may want to try https://github.com/KhronosGroup/COLLADA2GLTF and then apply the specific optimizations afterward.


#5

Hi.

First of all thanks a lot for the information.

I tried to install canvas on nodejs, but it has dependencies with external programs I cannot install at work (I am not admin in my PC). I am using a portable nodejs+npm installation.

Anyway using electron I could make GLTFExporter working without any problem in some minutes.

The problem of Electron is that runs in an emulated webbrowser context and this is exactly what I am trying to avoid. I want to make make my convertor (not only a convertor) to run in a headless mode.

Anyway many thanks for you time.


#6

Finally I did it.

Now I can import all supported THREE Formats, apply my own object modifications and finally at the end export one or many models to one or multiple GLB files.

It’s super fast.

: )

Many thanks again for the clue.