I'm trying to collapse some items which have multiple material slots that are unneeded (and when exported come in as distinct materials, which I do not want). I have the code needed so far to recognize when a material slot shares the same image map (so a candidate to eliminate that slot) if only I can select it. It does select A material slot, but not all of them, and I'm pretty sure it's because I'm still confused about the way to specify what material slot I need.
Here's the code:
imageTextures = []
# Iterate over all of the imported object's material slots
for m in imported.material_slots:
if m.material:
# Iterate over all the current material's texture slots
for t in m.material.texture_slots:
# If this is an image texture, with an active image append its name to the list
if t and t.texture.type == 'IMAGE' and t.texture.image:
if t.texture.image.name not in imageTextures:
imageTextures.append(t.texture.image.name)
print ("Image is ", t.texture.image.name)
# Go to Edit mode
bpy.ops.object.mode_set(mode='EDIT')
for textureFile in imageTextures:
print ("Our Textures are", textureFile)
# Deselect all the vertices
bpy.ops.mesh.select_all(action='DESELECT')
for m in imported.material_slots:
if m.material:
# Iterate over all the current material's texture slots
for t in m.material.texture_slots:
if t and t.texture.type == 'IMAGE' and t.texture.image:
if t.texture.image.name == textureFile:
# Select the vertices of the vertex group
print ("Selecting", m.material.name)
bpy.ops.object.material_slot_select()
So if someone could just correct me on that last part I'd be very grateful (after this I need to create a new mat slot and assign the selected verts to this, and then delete all the other mat slots, but I think I can handle those chores).