images doublons dans Blender

python - 2017-08-23 14:23:13
  
import bpy
 
def get_original_images(image):
 
    if not "." in image.name:
        return image
 
    base, extension = image.name.rsplit(".", 1)
 
    if not extension.isdigit():
        return image
 
    listIndex = {'index': [], 'imgData': []}
 
    for name, imgData in bpy.data.images.items():
        if name == base:
            return imgData
        if base in name:
            listIndex['index'].append(int(name.split(".")[-1]))
            listIndex['imgData'].append(imgData)
 
 
    minIndex = min(listIndex['index'])
 
    idx = listIndex['index'].index(minIndex)
 
    return listIndex['imgData'][idx]
 
 
imageToClean = []
 
for obj in bpy.context.scene.objects:
    if obj.material_slots:
        for mat in obj.material_slots:
            for node in mat.material.node_tree.nodes:
                if node.type == 'TEX_IMAGE':
                    image = get_original_images(node.image)
                    if image != node.image:
                        imageToClean.append(node.image)
                        node.image = image
 
for img in imageToClean:
    bpy.data.images.remove(img, do_unlink = True)