1
$\begingroup$

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).

$\endgroup$

1 Answer 1

2
$\begingroup$

Okay, I figured it out (and I'm dumb and if there is a way to delete these questions you answer yourself I would).

All I had to do was to set the active index of the material and then do a material slot select (as I already had). So this works:

for num, m in enumerate(imported.material_slots):
    ...
    bpy.context.object.active_material_index = num
    bpy.ops.object.material_slot_select()

Now just to make a new material, assign the faces to it, and delete all the old ones, but I think I have that.

$\endgroup$

You must log in to answer this question.

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