Created
August 10, 2021 01:55
-
-
Save MichaelGatesDev/0d3f075c5512bec9faa9be9ed033ff39 to your computer and use it in GitHub Desktop.
Remap & Remove Duplicate Materials + Textures in Blender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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