<p>To anyone who is reading this in future:</p>

<p>I ended up taking an alternative route: Blender scripting.</p>

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

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

<p>Profit!</p>

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

CC BY-SA 4.0
2019-07-08T07:02:35.267Z
2019-07-08T07:02:35.267Z
shashanka n
<p>Building upon the answer by @shashankan and <a href="https://blender.stackexchange.com/a/62604/134890">another answer in Blender</a>, I wrote a Blender script to convert from FBX into a subfolder with GLTFs.</p>
<p>(You may want to remove my scaling code, which scales up the import and then resets the object to be scaled at 1.0 at the new size.)</p>
<pre><code>import bpy
import os

for root, dirs, files in os.walk('/path/to/directory'):
    for name in files:
        if name.endswith('fbx'):
            # clear the scene
            bpy.ops.object.select_all(action='SELECT')
            bpy.ops.object.delete()
            
            # import fbx, scale up by 3.28
            bpy.ops.import_scene.fbx(filepath=os.path.join(root, name), global_scale=3.28)
            bpy.ops.object.select_all(action='SELECT')
            
            # 'reset' transform to 1.0, putting current scale into data
            bpy.ops.object.transform_apply(scale=True, rotation=False, location=False, properties=False)
            
            # export as fbx, replacing file extension
            # change export_format as needed, see api for options
            # https://docs.blender.org/api/current/bpy.ops.export_scene.html
            bpy.ops.export_scene.gltf(filepath=os.path.join(root+'/gltf', name.removesuffix('fbx')+'gltf'), export_format='GLTF_SEPARATE', use_selection=True)
            
            bpy.ops.object.delete() # clear the scene
</code></pre>

CC BY-SA 4.0
2021-11-04T03:53:53.913Z
2021-11-04T03:53:53.913Z
Adam Thompson
<p>The <code>.bin</code> file that goes with a <code>.gltf</code> 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).</p>

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

<p>Let's look at the <a href="https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/DamagedHelmet" rel="nofollow noreferrer">Damaged Helmet</a> sample model for an example.  The JPG images are <a href="https://github.com/KhronosGroup/glTF-Sample-Models/blob/77f1a295e65c3a59c7131e6a15552c69817c9449/2.0/DamagedHelmet/glTF/DamagedHelmet.gltf#L92-L114" rel="nofollow noreferrer">called out in the <code>.gltf</code> file</a>, separately from the <code>.bin</code> file.  The data from the <code>.bin</code> file is used by <code>buffers</code>, which in turn are used by <code>bufferViews</code>, <code>accessors</code>, 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 <code>images</code>, which are referenced by <code>textures</code> which in turn are used by <code>materials</code>, which are referenced by mesh primitives.  For a more graphical version of this explanation, take a look at the <a href="https://github.com/KhronosGroup/glTF#overview" rel="nofollow noreferrer">Overview Chart</a> for glTF.</p>

<p>So when exporting from Blender, choose <code>glTF Separate</code> from the combo box in the export options panel.  This will produce a <code>.gltf</code> file, a <code>.bin</code> 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 <code>uri</code> fields in the <code>images</code> array inside of the <code>.gltf</code> file, and keep the same <code>.bin</code> file around (the one that came out of Blender).</p>

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

CC BY-SA 4.0
2019-06-01T14:35:48.427Z
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.
shashanka n
CC BY-SA 4.0
2019-06-02T00:50:56.750Z
In what language?  There's a TypeScript solution with a NodeJS npm package available for this:  https://github.com/najadojo/gltf-import-export
emackey
CC BY-SA 4.0
2019-06-02T14:47:06.827Z
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.
shashanka n
CC BY-SA 4.0
2019-06-02T17:54:28.063Z
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?
emackey
CC BY-SA 4.0
2019-06-03T04:42:26.017Z
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.
shashanka n
CC BY-SA 4.0
2019-06-03T13:41:56.510Z
To generate a GLB file, the `.gltf` and `.bin` are not enough, you need the `.jpg` and/or `.png` files that hold the textures.  The `.bin` file does not contain any images.  Take a look at [DamagedHelmet](https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/DamagedHelmet/glTF) for example.  It references [jpgs separately from the bin](https://github.com/KhronosGroup/glTF-Sample-Models/blob/77f1a295e65c3a59c7131e6a15552c69817c9449/2.0/DamagedHelmet/glTF/DamagedHelmet.gltf#L92-L114).  All of the texture data is in the JPGs, not the bin file.
emackey
CC BY-SA 4.0
2019-05-31T14:23:54.663Z
2019-06-03T13:52:25.887Z
2019-06-03T13:52:25.887Z
emackey
<p>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.</p>

<p><strong>What Im stuck with</strong></p>

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

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

<p><strong>What Ive tried</strong></p>

<p>Went through this question, but the selected answer there isnt dealing with images:</p>

<p><a href="https://computergraphics.stackexchange.com/questions/2338/how-to-generate-binary-gltf-array-buffers">How to generate binary gltf array buffers?</a></p>

<p>How do I go about generating a BIN file from a series of images?</p>

<3d><3dtexture><gltf>
Programmatically generating bin files for GLTF models
shashanka n
# Programmatically generating bin files for GLTF models

(Asked by: username_0 on 2019-05-31)

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?](https://computergraphics.stackexchange.com/questions/2338/how-to-generate-binary-gltf-array-buffers)

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

**Tags:** 3d, 3dtexture, gltf


---

## 3 answers

### Answer by username_0 on 2019-07-08. Score: 1 | Accepted answer

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.


---

### Answer by username_1 on 2021-11-04. Score: 2

Building upon the answer by @username_0 and [another answer in Blender](https://blender.stackexchange.com/a/62604/134890), I wrote a Blender script to convert from FBX into a subfolder with GLTFs.

(You may want to remove my scaling code, which scales up the import and then resets the object to be scaled at 1.0 at the new size.)

```
import bpy
import os

for root, dirs, files in os.walk('/path/to/directory'):
    for name in files:
        if name.endswith('fbx'):
            # clear the scene
            bpy.ops.object.select_all(action='SELECT')
            bpy.ops.object.delete()
            
            # import fbx, scale up by 3.28
            bpy.ops.import_scene.fbx(filepath=os.path.join(root, name), global_scale=3.28)
            bpy.ops.object.select_all(action='SELECT')
            
            # 'reset' transform to 1.0, putting current scale into data
            bpy.ops.object.transform_apply(scale=True, rotation=False, location=False, properties=False)
            
            # export as fbx, replacing file extension
            # change export_format as needed, see api for options
            # https://docs.blender.org/api/current/bpy.ops.export_scene.html
            bpy.ops.export_scene.gltf(filepath=os.path.join(root+'/gltf', name.removesuffix('fbx')+'gltf'), export_format='GLTF_SEPARATE', use_selection=True)
            
            bpy.ops.object.delete() # clear the scene

```


---

### Answer by username_2 on 2019-05-31. Score: 2

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](https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/DamagedHelmet) sample model for an example. The JPG images are [called out in the `.gltf` file](https://github.com/KhronosGroup/glTF-Sample-Models/blob/77f1a295e65c3a59c7131e6a15552c69817c9449/2.0/DamagedHelmet/glTF/DamagedHelmet.gltf#L92-L114), 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](https://github.com/KhronosGroup/glTF#overview) 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.

> **Comments:**
> **username_0**: 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.  
> **username_2**: In what language?  There's a TypeScript solution with a NodeJS npm package available for this:  https://github.com/najadojo/gltf-import-export  
> **username_0**: 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.  
> **username_2**: 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?  
> **username_0**: 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.  
> **username_2**: To generate a GLB file, the `.gltf` and `.bin` are not enough, you need the `.jpg` and/or `.png` files that hold the textures.  The `.bin` file does not contain any images.  Take a look at [DamagedHelmet](https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/DamagedHelmet/glTF) for example.  It references [jpgs separately from the bin](https://github.com/KhronosGroup/glTF-Sample-Models/blob/77f1a295e65c3a59c7131e6a15552c69817c9449/2.0/DamagedHelmet/glTF/DamagedHelmet.gltf#L92-L114).  All of the texture data is in the JPGs, not the bin file.  

---

