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:

        # 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]
        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.
            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.
            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.
                tempOb = bpy.data.objects.new(tempName, me)     # Make new object linked to this special mesh.
                scene.objects.link(tempOb)                      # Link it to the scene.
                    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.
                    print("splitByMaterial: error transform mapping.")
                    tempOb.data.materials.append(mat)               # Add the material for this material index.
                    print("splitByMaterial: error material mapping.")
            c = c + 1



263_split_by_material.blend (473 KB)