4
$\begingroup$

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?

$\endgroup$

3 Answers 3

2
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ 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. $\endgroup$ Commented Jun 1, 2019 at 14:35
  • $\begingroup$ In what language? There's a TypeScript solution with a NodeJS npm package available for this: github.com/najadojo/gltf-import-export $\endgroup$
    – emackey
    Commented Jun 2, 2019 at 0:50
  • $\begingroup$ 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. $\endgroup$ Commented Jun 2, 2019 at 14:47
  • $\begingroup$ 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? $\endgroup$
    – emackey
    Commented Jun 2, 2019 at 17:54
  • $\begingroup$ 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. $\endgroup$ Commented Jun 3, 2019 at 4:42
2
$\begingroup$

Building upon the answer by @shashankan and another answer in Blender, 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
$\endgroup$
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.