1
$\begingroup$

I have around 200 materials that I want to apply to an object, one at a time, and export as glb, so I have one version of the model with every texture. Is it possible write a script that applies one material, exports as a glb file, applies the next material, exports, and so on until all the materials have been applied and exported?

$\endgroup$
4
  • 1
    $\begingroup$ Yes it is fairly easy to automate with scripting. you loop on materials. In the loop set material slot 0 of the object to the current material and call the glb exporter. $\endgroup$ Jan 25, 2022 at 16:55
  • 2
    $\begingroup$ Hello and welcome. As it stands this question is too broad to be answerable without requiring an extensive tutorial or description. You should show efforts towards reaching your goal, describing what you have tried and why it failed, so we don't risk recommending something you already know. The scope should also focus on a particular step you encountered an issue with. Describing the whole procedure from start to finish would be too lengthy to explain, and is beyond the goal of this site. If you could edit your post to focus on where you are stuck we can vote to reopen it. $\endgroup$ Jan 25, 2022 at 17:24
  • $\begingroup$ @DuarteFarrajotaRamos This question is much less broad than many similar questions we leave open, an d already has an answer. I'm voting to reopen. $\endgroup$ Jan 25, 2022 at 18:19
  • $\begingroup$ I have tried to specify a little, but the answer I got really helped, so I think it should be possible to understand what my problem is. $\endgroup$ Jan 25, 2022 at 19:01

1 Answer 1

3
$\begingroup$

Sure, here is pseudocode for an example script that would do that:

import bpy

object = bpy.context.active_object

materials_to_use = ['reddish', 'bluish']

for material_name in materials_to_use:
    object.material_slots[0].material = bpy.data.materials[material_name]
    bpy.ops.export_scene.gltf()

This script makes an assumption that you only have a single object that you want to change the material on and that you only care about the first material. Adjust accordingly.

The 'pseudo' part comes in figuring out materials_to_use. Above I manually listed material names. That's fine for a few materials but you have around 200 so you'll need to construct the list some other way. Or you could simply do it for every material in the blend file and then sort it out by deleting outputs. In that case

import bpy

object = bpy.context.active_object

for material in bpy.data.materials:
    object.material_slots[0].material = material
    bpy.ops.export_scene.gltf()

Oh, and I left out a step: setting the file name for export_scene to use. bpy.ops.export_scene.gltf() takes a filepath="SOME PATH argument. You need to output each material to a different path. The usual technique is to set a directory name before the loop and create a file name like material.name + '.glb' in the loop and using the combined directory + file name as the filepath argument.

Here is one way to do that with my original example

import bpy
from pathlib import Path

object = bpy.context.active_object

materials_to_use = ['reddish', 'bluish']

dir = Path("C:/tmp")

for material_name in materials_to_use:
    object.material_slots[0].material = bpy.data.materials[material_name]
    file = material_name + ".glb"
    bpy.ops.export_scene.gltf(filepath=str(dir / file))

and for every material

import bpy
from pathlib import Path

object = bpy.context.active_object
dir = Path("C:/tmp")

for material in bpy.data.materials:
    object.material_slots[0].material = material
    file = material.name + ".glb"
    bpy.ops.export_scene.gltf(filepath=str(dir / file))
$\endgroup$
5
  • $\begingroup$ After some trial and error I got it to work with a few manually listed items, but I didn't understand how to do it with every material in the blend file, like you said I could do. Could you explain a little more? $\endgroup$ Jan 25, 2022 at 18:51
  • $\begingroup$ it's my second loop. you want for material in bpy.data.materials but instead of material name when you create the file string use material.name I've written it out in my answer now. (Also added the missing right parenthesis) $\endgroup$ Jan 25, 2022 at 18:55
  • $\begingroup$ When I run that script, it only exports with the first material. Any ideas on what the problem could be? I accepted your answer, but I don't have enough reputation to upvote $\endgroup$ Jan 25, 2022 at 19:17
  • $\begingroup$ No problem on the upvote. Off the top of my head I don't. The last script should work for every material in your file -- at least it does in my version. Are you sure you've got the code exactly right? $\endgroup$ Jan 25, 2022 at 20:17
  • 2
    $\begingroup$ I just figured it out. I had to remove the second bpy.ops.export_scene.gltf(). Guess the program got confused when there was nothing in the parenthesis or something. Also had to add an r before the filepath: dir = Path(r"C:\tmp"). $\endgroup$ Jan 25, 2022 at 20:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .