script help retrieving data users from images?

Just trying to write a simple script to find all the duplicate textures that usually end in " .001 etc" and set their user count to zero so that on saving the file is nice and clean with only one of each texture left.

The code below seems to work great but the only problem is that all the materials/nodes that referenced the old duplicate names now are unlinked and have to me manually updated… I found how to access the user count but not to find the actual datablock they are linked to.

Anyone know how to find the users of the image textures so they can then be relinked to the correct names in the script?

import bpy
print ("start")
ilist=[]
ilist_dup=[]

for i in bpy.data.images:
    
    iname=i.name
    iusr=i.users
    print (iname)
    ilist.append(iname) 
    if iname[-4] == "." and iname[0:-4] in ilist:
        ilist_dup.append(iname)
        i.user_clear()
    #print (iname) 
print (ilist_dup)

If you want to find every reference, you need to iterate through all datablocks and clear the references. There’s no direct way to retrieve the users.

OK that’s what I found and managed to scrap together a small script by using other peoples similar code and the blender python api which, frankly - is not very helpful or clear in explaining exactly what data you are retrieving and how to modify it with the various methods etc listed …

Feel free to use the script below. It simply takes all the duplicate images - eg… ending in .001 usually- in a blend file and sets the user count to 0 for them so that on a save and reload they are gone YAYY!.
It also searches the materials in the file and every time it finds an ImageTexture node in a material it relinks the correct image back so that after deleting the duplicates you aren’t left with broken materials!

Todo: for someone with more blender2.6+ python experience you could also add a new loop to the code that would loop through the materials that don’t use nodes such as the blender internal render mats and find images/environment textures and fix the broken names as well. Right now it only does this for cycles mats that use nodes…

import bpy
print ("start")
ilist=[]
ilist_dup=[]
ilist_new=[]

for i in bpy.data.images:
    
    iname=i.name
    iusr=i.users
    print (iname)
    ilist.append(iname)
    if iname[-4] == "." and iname[0:-4] in ilist:
        ilist_dup.append(iname)
        i.user_clear()
    else:
        ilist_new.append(iname) 
    #print (iname) 
#print (ilist_new,ilist_dup)
#ilist=ilist-ilist_dup
for mat in bpy.data.materials:
    for node in mat.node_tree.nodes:
        #print("node" , node.bl_idname)
        if node.bl_idname == "ShaderNodeTexImage":
            if node.image :
                #print(node.image.name)
                image=node.image
                if  node.image.name[-4] == "." and node.image.name[0:-4] in ilist:#and node.image.name in ilist_dup:
                    #print(node.image.name[-1])
                    new_i=bpy.data.images[image.name[0:-4]]#node.image.name[0:-4]
                    print("node" , node.bl_idname , "replacing",node.image.name,"with",node.image.name[0:-4])
                    node.image=new_i
                    
   
        
            

Anyone know how to find the users of the image textures so they can then be relinked to the correct names in the script?