Skip to main content
Changed c.selected_objects[-1] to c.active_object
Source Link
Ectras
  • 391
  • 1
  • 6

This is what I came up with. Select the object with the "wrong" materials first (like 'torso.001',...) and the object with the "original" materials last (like 'torso',...). Then run the script. It compares all material names and if one of the "wrong" materials starts with the name of one of the "right" materials, it will be replaced by this mat. (So the material 'torso.001' would be replaced by the material 'torso').

The script does not update the 3dView, so be sure to do some action (like zoom in, etc.) to update the viewport after you run the script!

Also this might not work correctly, if some names are combined of other names. E.g. if you have two materials 'torsoUpper' and 'torso'. If that is a problem, I can edit the script.

Hope this helps.

import bpy
  
c = bpy.context

# Select multiple objects. 
# The last selected should be the object with the "original" colors
# The script loops through all materials of the other objects and compares
# them to the materials of the "original" object. If their names start with the same
# letters, the material is replaced with the original.
# HINT: The script does not update the 3dView!

print("---RUNNING SCRIPT---")
src = c.selected_objects[-1]active_object

for i in c.selected_objects:
    if (i == src):
        continue
    for src_id in range(len(src.data.materials)):
        for replace_id in range(len(i.data.materials)):
            src_name = src.data.materials[src_id].name
            rep_name = i.data.materials[replace_id].name
            
            if (rep_name.startswith(src_name)):
                i.data.materials[replace_id] = src.data.materials[src_id]
                print("Replaced "+rep_name+" by "+src_name)
                break

This is what I came up with. Select the object with the "wrong" materials first (like 'torso.001',...) and the object with the "original" materials last (like 'torso',...). Then run the script. It compares all material names and if one of the "wrong" materials starts with the name of one of the "right" materials, it will be replaced by this mat. (So the material 'torso.001' would be replaced by the material 'torso').

The script does not update the 3dView, so be sure to do some action (like zoom in, etc.) to update the viewport after you run the script!

Also this might not work correctly, if some names are combined of other names. E.g. if you have two materials 'torsoUpper' and 'torso'. If that is a problem, I can edit the script.

Hope this helps.

import bpy
  
c = bpy.context

# Select multiple objects. 
# The last selected should be the object with the "original" colors
# The script loops through all materials of the other objects and compares
# them to the materials of the "original" object. If their names start with the same
# letters, the material is replaced with the original.
# HINT: The script does not update the 3dView!

print("---RUNNING SCRIPT---")
src = c.selected_objects[-1]

for i in c.selected_objects:
    if (i == src):
        continue
    for src_id in range(len(src.data.materials)):
        for replace_id in range(len(i.data.materials)):
            src_name = src.data.materials[src_id].name
            rep_name = i.data.materials[replace_id].name
            
            if (rep_name.startswith(src_name)):
                i.data.materials[replace_id] = src.data.materials[src_id]
                print("Replaced "+rep_name+" by "+src_name)
                break

This is what I came up with. Select the object with the "wrong" materials first (like 'torso.001',...) and the object with the "original" materials last (like 'torso',...). Then run the script. It compares all material names and if one of the "wrong" materials starts with the name of one of the "right" materials, it will be replaced by this mat. (So the material 'torso.001' would be replaced by the material 'torso').

The script does not update the 3dView, so be sure to do some action (like zoom in, etc.) to update the viewport after you run the script!

Also this might not work correctly, if some names are combined of other names. E.g. if you have two materials 'torsoUpper' and 'torso'. If that is a problem, I can edit the script.

Hope this helps.

import bpy
  
c = bpy.context

# Select multiple objects. 
# The last selected should be the object with the "original" colors
# The script loops through all materials of the other objects and compares
# them to the materials of the "original" object. If their names start with the same
# letters, the material is replaced with the original.
# HINT: The script does not update the 3dView!

print("---RUNNING SCRIPT---")
src = c.active_object

for i in c.selected_objects:
    if (i == src):
        continue
    for src_id in range(len(src.data.materials)):
        for replace_id in range(len(i.data.materials)):
            src_name = src.data.materials[src_id].name
            rep_name = i.data.materials[replace_id].name
            
            if (rep_name.startswith(src_name)):
                i.data.materials[replace_id] = src.data.materials[src_id]
                print("Replaced "+rep_name+" by "+src_name)
                break
Source Link
Ectras
  • 391
  • 1
  • 6

This is what I came up with. Select the object with the "wrong" materials first (like 'torso.001',...) and the object with the "original" materials last (like 'torso',...). Then run the script. It compares all material names and if one of the "wrong" materials starts with the name of one of the "right" materials, it will be replaced by this mat. (So the material 'torso.001' would be replaced by the material 'torso').

The script does not update the 3dView, so be sure to do some action (like zoom in, etc.) to update the viewport after you run the script!

Also this might not work correctly, if some names are combined of other names. E.g. if you have two materials 'torsoUpper' and 'torso'. If that is a problem, I can edit the script.

Hope this helps.

import bpy
  
c = bpy.context

# Select multiple objects. 
# The last selected should be the object with the "original" colors
# The script loops through all materials of the other objects and compares
# them to the materials of the "original" object. If their names start with the same
# letters, the material is replaced with the original.
# HINT: The script does not update the 3dView!

print("---RUNNING SCRIPT---")
src = c.selected_objects[-1]

for i in c.selected_objects:
    if (i == src):
        continue
    for src_id in range(len(src.data.materials)):
        for replace_id in range(len(i.data.materials)):
            src_name = src.data.materials[src_id].name
            rep_name = i.data.materials[replace_id].name
            
            if (rep_name.startswith(src_name)):
                i.data.materials[replace_id] = src.data.materials[src_id]
                print("Replaced "+rep_name+" by "+src_name)
                break