Exporting forge viewer scene to GLTF

I am trying to convert some isolated elements on forge viewer to GLTF I have included this script

<script src="https://unpkg.com/[email protected]/examples/js/exporters/GLTFExporter.js"></script>

I am using this code to export to GLTF

const exporter=new THREE.GLTFExporter()
const exportGLTF = () => {
  var scene = viewer.impl.scene
  exporter.parse( scene, function ( gltf ) {
    const output = JSON.stringify( gltf, null, 2 );
    console.log(output)    
    //saveString( output, 'scene.gltf' );
  }, {trs: true} );
}

but unfortunately I receive empty GLTF output like this

{
  "asset": {
    "version": "2.0",
    "generator": "THREE.GLTFExporter"
  },
  "scenes": [
    {}
  ],
  "scene": 0
}

Upvotes: 0

Views: 322

Answers (2)

michael beale
michael beale

Reputation: 1196

R103+ THREE.GLTFExporter() is not compatible with THREE R71 (since we use a modified BufferGeometry), hence why you are getting no geometry.

Alex is right, use glTF-convert-utils.

Here's an example node.js script, with DBID filter. Use this to filter out a sub-set of the full drawing:

// convert SVF to dstPath/output.glb (with zeux compression)
// but only convert a subset of objects (see filter on line 23)

// INSTALL:
// > npm install forge-convert-utils forge-server-utils fs-extra gltfpack

// RUN:
// > node convert url  guid  token  dstPath

const path = require('path');
const fs = require('fs-extra');
var gltfpack = require('gltfpack');
const { SvfReader, GltfWriter } = require('forge-convert-utils');

async function convert(urn, guid, token, dstPath) {

  // Convert SVF to glTF
  const reader = await SvfReader.FromDerivativeService(urn, guid, { token });
  const writer = new GltfWriter({
    ignoreLineGeometry: true,
    ignorePointGeometry: true,
    skipUnusedUvs: false,
    center: false,
    filter: (dbid) => (
        [34044, 40936, 41095, 39471, 40933, 40939, 41092, 41097, 41090, 40946].indexOf(dbid)>-1)
  });
  const svf = await reader.read();
  const gltfDir = path.join(path.dirname(dstPath), 'gltf');
  fs.ensureDirSync(gltfDir);
  await writer.write(svf, gltfDir);

  gltfpack.pack(['-cc', '-i', './gltf/output.gltf', '-o', 'output.glb'], { read: fs.readFileSync, write: fs.writeFileSync})
}


const urn = `dXJuOm...j0z`;
const guid = `2588ca45-8487-cddf-b974-7f04179909a2`;
const token = `eyJhbG......gJHNA`
const dstPath = `out`;

convert(urn, guid, token, dstPath);

Remember to fill in the urn, guid & token with your forge details. The script will connect to forge, download svf, convert to gltf, then convert to glb (with MeshOpt compression using glTFpack).

Upvotes: 1

AlexAR
AlexAR

Reputation: 1324

I am not sure it's possible to extract the Forge scene with ThreeJS exporters.

If you want to extract your Forge model into glTF format, one of the best way (as of today) is to use the Forge Convert Utils package.

Upvotes: 0

Related Questions