Splt By Material Leaves Extra Vertices?

Hi All,

I am trying to turn a multi-material mesh into multiple meshes each with a single material. The new mesh only contains faces from the source mesh whose material ID match the index of this mesh. So I have this 3 material cube. After running the script, I have 3 new mesh objects each with a single material. The material assigned to the newly created meshes is based upon the material ID from the source mesh.

The problem is my new meshes, while their faces look correct, still contain vertices from the source mesh. This is throwing off other parts of the script.

So I guess I am wondering is there an easy way to remove unused vertices?


import bpy

#Atom 08282011
def rounded_tuple(tup):
    return tuple(round(value,4) for value in tup) 

def splitByMaterial(ob,scene):
    # The mesh with modifiers applied.
    me = ob.to_mesh(scene,True,'RENDER')
    
    if len(me.materials) > 1:
        # A list of all the vertices.
        list_verts = []
        for vertex in me.vertices:
            list_verts.append(rounded_tuple(vertex.co.to_tuple()))

        # Fetch the vertex and face list from the provided mesh type object.
        list_faces_by_material = []
        for face in me.polygons:
            x = [f for f in face.vertices]
            list_faces_by_material.append([face.material_index,x])
        
        print("splitByMaterial: working on ["+ ob.name + "] it has " + str(len(me.materials))+" materials.")
        c = 1
        for mat in me.materials:
            print("splitByMaterial: processing material # "+ str(c) + ".")
            # Construct a mesh that represents only the faces for this material.
            list_faces = []
            
            # Sort by material index.
            list_faces_by_material.sort()
            for f in list_faces_by_material:
                if float(f[0]) == float((c-1)):
                    #This face should be part of this mesh because of it's material index value.
                    list_faces.append(f[1])
            
            if len(list_faces) > 0:
                tempName = str(int(c))+ "-3DL_" + ob.name       # User should never see this object in the outliner. 
                me = bpy.data.meshes.new(tempName)              # Create a new blank mesh.
                me.from_pydata(list_verts,[],list_faces)        # Give this empty mesh a list of verts and faces to call it's own.
                me.update(calc_edges=True)
                
                tempOb = bpy.data.objects.new(tempName, me)     # Make new object linked to this special mesh.
                scene.objects.link(tempOb)                      # Link it to the scene.
                try:
                    print("splitByMaterial: mapping location.")
                    tempOb.location = ob.location                   # Transfer location to this new object.
                    print("splitByMaterial: mapping rotation_euler.")
                    tempOb.rotation_euler = ob.rotation_euler       # Transfer rotation to this new object.
                    print("splitByMaterial: mapping scale.")
                    tempOb.scale = ob.scale                         # Transfer scale to this new object.
                except:
                    print("splitByMaterial: error transform mapping.")
                    pass
                try:
                    tempOb.data.materials.append(mat)               # Add the material for this material index.
                except:
                    print("splitByMaterial: error material mapping.")
                    pass
            c = c + 1

splitByMaterial(bpy.data.objects["Cube"],bpy.data.scenes[0])

Attachments


263_split_by_material.blend (473 KB)