Maciej Główka
Blog Games Contact

← Return to Blog Index

July 15, 2021

Batch GLTF material variants in Blender

python blender

I have been working for the last couple of months on an AR marketplace for furniture makers - Obviously we use Blender for our 3d model preparation. Lots of furniture pieces are customizable - usually in terms of finishing / materials. Even though the GLTF format supports shader variants nowadays, I am not aware of such a functionality in Blender's exporter.

So, in order to save us some clicking we have came up with a very simple automation that would create all the variants for us. Let's assume we have a model with two shader slots - eg. first slot would be for an overall material and the second one for details (fronts or legs or whatever). To start, we have to create a CSV file, describing the variants. The format could look like:

table_large, oak, oak
table_large, oak, white
table_large, oak, black
table_large, ash, ash
table_large, ash, white
table_large, ash, black

We would place the file in the same directory as the .blend file. The filename is 'warianty.csv', but can be easily changed in the script. Cell values obviously have to match object and shader names in Blender.

Next step is as simple as selecting the objects to be exported and running the script - through the play button in the built-in text editor. As a result we should get a single directory per object, each containing as many GLTF files as needed. The file naming scheme would be: objectName_shaderName_shaderName.glb.
If we do some changes to the models and run the script again, files would simply be updated. Finally we can upload those files and assign them to product variants in our DB - which we have also automated in Django, but that's for another post :)

← Pre-export file flattening in Blender OBJ with MTL and textures from THREE.js→