Trying to fix image name (script inside)

Hello,
I imported a model in 3DS format.

I noticed in the uv editor, when selecting model faces, that images are like myimage.bmp.001, myimage.bmp.002, etc… on different objects instead of myimage.bmp for all objects using the same image.

I’m trying to write a script to fix this.

However, when using bpy.data.images.load, the file name is also suffixed by a number. If the last file was, for example, myimage.bmp.234, when using the load instruction, the file name becomes myimage.bmp.235 (and it increased for each face!) instead of myimage.bmp.

Thanks in advance for your help.

import bpy
import os
from bpy.props import StringProperty

model_directory = os.path.dirname(bpy.data.filepath)

#for obj in bpy.context.scene.objects:
for obj in bpy.context.selected_objects:
    if obj.type == "MESH":
        for fuv_num, fuv in enumerate(obj.data.uv_textures):
            for f in fuv.data:
                if f.image is not None:
                    print(" >>> ", f.image.name)
                    str_index = f.image.name.find('BMP.')
                    if str_index != -1:
                        new_image_name = f.image.name[:str_index+3] # Remove .XXX after .BMP
                        new_file_name = model_directory + '\\' + new_image_name
                        img = bpy.data.images.load(filepath=new_file_name)
                        f.image = img
                        print(f.image.name, new_image_name)


Hello,

Here is an example of the problem: on the right, the texture file name in the material is correct (no .001 extension) but in the uv editor, I’ve many references to the same file but with a suffix .001, .002, etc…

Hi. Something like?

This will make all reference one image “somename.BMP” if their name begins with “somename.BMP”.


import bpy
import os
from bpy.props import StringProperty


model_directory = os.path.dirname(bpy.data.filepath)
base_img = bpy.data.images.get("somename.BMP")


#for obj in bpy.context.scene.objects:


if base_img is not None:
    for obj in bpy.context.selected_objects:
        if obj.type == "MESH":
            for fuv_num, fuv in enumerate(obj.data.uv_textures):
                for f in fuv.data:
                    if f.image is not None:
                        print(" >>> ", f.image.name)
                        if image.name.startswith(base_image.name):
                             f.image = base_img