Shopify/screenshot-glb

Add utility to call this from another node module

Opened this issue · 2 comments

@sleroux

It would be nice to have an official utility for importing this inside a node module. The current use case is for standalone use as a command line utility or a subprocess, but it's pretty simple to call this from node without spawning a subprocess.

I currently just wrapped the code in cli.js for my own use, but it would be nice for something like this to be part of the tool as well.

// Example
//   await screenshotFromGlb("./scene.glb",
//                           "./scene.jpg",
//                           {
//                              width: 1000,
//                              height: 1000,
//                              imageFormat: "image/jpg",
//                           });


const path = require('path');

const FileServer = require("@shopify/screenshot-glb/src/file-server")

const startBrowser = require("@shopify/screenshot-glb/src/start-browser")
const loadGLBAndScreenshot = require("@shopify/screenshot-glb/src/load-glb-and-screenshot")


async function screenshotFromGlb(glbIn, outputPath, options) {
   const libServer = new FileServer(
      path.join(
         path.dirname(require.resolve("@shopify/screenshot-glb/package.json")),
         "lib"
      )
   )

   const modelServer = new FileServer(path.dirname(glbIn))

   const width  = options.width || 1024;
   const height = options.height || 1024;
   const format = options.imageFormat || 'image/png';
   const quality = options.imageQuality || 0.92;

   await libServer.start()
   await modelServer.start()

   const glbPath = `http://localhost:${modelServer.port}/${path.basename(glbIn)}`;

   const {page, browser} = await startBrowser({width, height, libPort: libServer.port});

   await loadGLBAndScreenshot(page, {
      glbPath,
      outputPath: outputPath,
      format,
      quality,
   });

   await browser.close();
   await libServer.stop();
   await modelServer.stop();
}


module.exports = {
   screenshotFromGlb
}

Thanks for the feedback on the screenshot tool! I think having an API accessible to node would be a great idea. I would write up a PR for this but it looks like you've already done most of the writing in this issue. Would you be interested in throwing this into PR?

@sleroux sure, I'll give it a shot soon