Automatic reuse same-property materials I have many objects created by importing some .wrl files and joining several meshes toghether. The result is a set of blender objects, each with a long list of materials from its submeshes. However, these - nearly a thousand - materials are only ten different colors or so...

I know that I can manually create ten "master materials" and assign each mesh to the proper color (following the manual: reusing-existing-materials), but it is really annoying...

Is there a way to automatically "skim" the materials to ends up with a small list of colors reused by all the submeshes of the objects?

I mean something like this:

list_of_unique_materials = []
for material in bpy.data.materials:
    if material.users:
        if not **material in list_of_unique_materials**:
            list_of_unique_materials.append(material)
        else:
            **reuse the same material in the list**


where I'm not really sure about how to implement the code inside the asterisks...

Many thanks!

EDIT: Example

What I have now (len(bpy.data.materials) &gt;&gt; 805):


Object1


Mesh.001: Material.001
Mesh.002: Material.002
Mesh.003: Material.003 (same as Material.001)
Mesh.004: Material.004
...

Object2


Mesh.101: Material.101 (same as Material.001)
Mesh.102: Material.102 (same as Material.002)
Mesh.103: Material.103 (same as Material.002)
Mesh.104: Material.104 (same as Material.004)
...

...


What I would like (len(bpy.data.materials) &gt;&gt; 12):


Object1


Mesh.001: Material.001
Mesh.002: Material.002
Mesh.003: Material.001
Mesh.004: Material.004
...

Object2


Mesh.101: Material.001
Mesh.102: Material.002
Mesh.103: Material.002
Mesh.104: Material.004
...

...


This is due to the .wrl importer which has named with a new identifier every single color of the meshes that I have imported (and the total number of distinct colors in the meshes is no more than 12).
I've made up this workaround:

import bpy

def find_material(material, list):
    for elem in list:
        if material.diffuse_color == elem.diffuse_color and \
           material.specular_color == elem.specular_color:
            return elem
    return False

# creates a list of unique materials
list_of_unique_materials = []
for material in bpy.data.materials:
    if material.users and not find_material(material, list_of_unique_materials):
        list_of_unique_materials.append(material)

# updates materials of each mesh
for obj in bpy.data.objects:
    for mat in obj.material_slots:
        mat.material = find_material(mat.material, list_of_unique_materials)

# removes unused materials
for material in bpy.data.materials:
    if not material.users:
        bpy.data.materials.remove(material)