0
$\begingroup$

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

$\endgroup$
  • $\begingroup$ Is this part of an importer? What exactly are you meaning to do when you say "reuse", just not adding it to the list? $\endgroup$ – cmomoney Feb 8 '16 at 19:07
  • $\begingroup$ I mean something like what has explained in the manual linked above. I've also added an example to clarify. $\endgroup$ – alextoind Feb 8 '16 at 19:55
0
$\begingroup$

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)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.