Skip to content

Instantly share code, notes, and snippets.

@MichaelGatesDev
Created August 10, 2021 01:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelGatesDev/0d3f075c5512bec9faa9be9ed033ff39 to your computer and use it in GitHub Desktop.
Save MichaelGatesDev/0d3f075c5512bec9faa9be9ed033ff39 to your computer and use it in GitHub Desktop.
Remap & Remove Duplicate Materials + Textures in Blender
import bpy
# credit to https://blender.stackexchange.com/questions/75790/how-to-merge-around-300-duplicate-materials/229803#229803
# remove duplicate materials
def remove_duped_materials():
mats = bpy.data.materials
for mat in mats:
(original, _, ext) = mat.name.rpartition(".")
if ext.isnumeric() and mats.find(original) != -1:
print("%s -> %s" %(mat.name, original))
mat.user_remap(mats[original])
mats.remove(mat)
# remove duplicate textures
def remove_duped_textures():
mats = bpy.data.materials
for mat in mats:
if mat.node_tree:
for n in mat.node_tree.nodes:
if n.type == 'TEX_IMAGE':
if n.image is None:
print(mat.name,'has an image node with no image')
elif n.image.name[-3:].isdigit():
name = n.image.name[:-4]
exists = False
for img in bpy.data.images:
if img.name in name:
exists = True
if exists:
old_name = n.image.name
n.image = bpy.data.images[name]
print("%s -> %s" %(old_name, n.image.name))
else:
n.image.name = name
remove_duped_materials()
remove_duped_textures()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment