# Programmatically generating bin files for GLTF models

I have a need to generate a series of GLB files. I have a base template for a GLTF file which I have to derive off of. The models thus generated will only have varying textures on them. The base mesh doesn't change at all.

What Im stuck with

Im able to generate the GLB, GLTF files from blender when exporting 3D models. After feeding these two files to: https://sbtron.github.io/makeglb/ Im able to create a GLB file which loads and works fine, with all the textures included.

However I am unable to generate the bin file programmatically. Ive tried searching google for generate bin file from images, but the first few pages dont seem to have anything relating to this.

What Ive tried

Went through this question, but the selected answer there isnt dealing with images:

How to generate binary gltf array buffers?

How do I go about generating a BIN file from a series of images?

The .bin file that goes with a .gltf only includes mesh data (position, normal, uv, other vertex attributes as needed). It does not normally include any images (unless you're using the base64 export "glTF Embedded", but don't use that for this, use "glTF Separate" instead).

If you look in the JSON text of the .gltf file, you'll see an images array, where image files are called out by uri (filename, if local, or relative URLs for the web).

Let's look at the Damaged Helmet sample model for an example. The JPG images are called out in the .gltf file, separately from the .bin file. The data from the .bin file is used by buffers, which in turn are used by bufferViews, accessors, and then mesh primitives. It is exclusively vertex mesh data, no images. This is a separate chain from how the JPGs are being used. The JPGs are called out by images, which are referenced by textures which in turn are used by materials, which are referenced by mesh primitives. For a more graphical version of this explanation, take a look at the Overview Chart for glTF.

So when exporting from Blender, choose glTF Separate from the combo box in the export options panel. This will produce a .gltf file, a .bin file, and may copy several images (be sure to export to a different folder from your source files, so nothing gets overwritten). To change the images, just update the uri fields in the images array inside of the .gltf file, and keep the same .bin file around (the one that came out of Blender).

That way, you can swap out images with other images, but keep the same mesh data.

• Im packing it into a GLB file to gain the advantage of packing everything into a single file. I was using GLTF files with separate textures until now. Jun 1 '19 at 14:35
• In what language? There's a TypeScript solution with a NodeJS npm package available for this: github.com/najadojo/gltf-import-export Jun 2 '19 at 0:50
• Im using that very package to generate my glb files from the command line. My only issue is now to figure out how to pack a bunch of images into the .bin file programmatically with images. Jun 2 '19 at 14:47
• So, images can go in a .glb file, but they do not go in the .bin file, nor in the binary mesh portion of the .glb file. So I'm having trouble understanding what you're trying to do here, can you explain it more? Jun 2 '19 at 17:54
• OK here is my understanding: GLB files contain everything packed into one single file. But to generate a GLB file, one needs : a .BIN file containing all the image data and the .GLTF file containing the mesh. Basically I have a bunch of 3D models which need to be programmatically generated. The GLTF file for these doesnt change at all since the base mesh is the same. How these models differ is via the textures applied on them. Since in my understanding, the BIN file contains all the textures, Im trying to ask how I can generate the BIN file dynamically. Jun 3 '19 at 4:42

To anyone who is reading this in future:

I ended up taking an alternative route: Blender scripting.

Blender comes with a built-in GLTF / GLB exporter. Here is what the render pipeline looks like in my code:

1. Define blender model which will act as template, complete with textures and materials.
2. Define a meta JSON which defines the images for all sides
3. In blenders Python script panel, read the meta JSON defined in #2 and start replacing the images in #1 in a loop
4. After each replace, call the export plugins GLB functionality

Profit!

Unfortunately, I am not allowed to post the script I wrote here / open source it.