Skip to main content
1 of 3
scurest
  • 10.4k
  • 13
  • 31

Try this. Save beforehand in case something goes wrong.

import bpy

for ob in bpy.data.objects:
    for slot in ob.material_slots:
        try:
            name = slot.material.node_tree.nodes['Image Texture'].image.name
        except Exception:
            continue

        if name not in bpy.data.materials:
            slot.material.name = name
        else:
            slot.material = bpy.data.materials[name]

Afterwards, you can use File > Clean Up > Unused Data Blocks to remove the extra unused materials.

scurest
  • 10.4k
  • 13
  • 31