Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bpy
- # Create a dictionary to store the materials with the same base color
- materials_dict = {}
- # Iterate over all materials in the scene
- for mat in bpy.data.materials:
- # Get the base color of the material
- base_color = mat.diffuse_color
- # Convert the base color to a hex string
- base_color_hex = '#%02x%02x%02x' % (int(base_color[0]*255), int(base_color[1]*255), int(base_color[2]*255))
- # Check if the hex string is already in the dictionary
- if base_color_hex in materials_dict:
- # If it is, add the material to the list of materials with the same base color
- materials_dict[base_color_hex].append(mat)
- else:
- # If it isn't, create a new entry in the dictionary with the hex string as the key and the material as the value
- materials_dict[base_color_hex] = [mat]
- # Iterate over the materials in the dictionary
- for hex_string, materials in materials_dict.items():
- # If there is only one material with the base color, skip it
- if len(materials) == 1:
- continue
- # Create a new material
- new_mat = bpy.data.materials.new(name="Combined Material")
- # Set the base color of the new material
- new_mat.diffuse_color = materials[0].diffuse_color
- # Iterate over all objects in the scene
- for obj in bpy.context.scene.objects:
- # Iterate over all materials assigned to the object
- for mat in obj.data.materials:
- # If the material is in the list of materials with the same base color, assign the new material to the object
- if mat in materials:
- obj.data.materials[obj.data.materials.find(mat.name)] = new_mat
- # Delete the original materials
- bpy.data.materials.remove(materials)
Advertisement
Add Comment
Please, Sign In to add comment