3
$\begingroup$

I found those script i Think it was for Blender 2.49 to change all imagePath at once,
but I cant get it to work in Blender 2.8...

Can some one help , it would be very handie for flies with lots of images?

import bpy  #Original import Blender

from bpy import context, data, ops
#from ORIGINAL from Blender import Texture, Image, Material

#insert the path which should be replaced
oldpath = "C:\\OLDpath\\"

#insert the new path which replaces the old one
newpath = "D:\\NewPth\\"

image_count = 0

for tex in bpy.data.textures.get():

    image = tex.getImage()
    #print image.name

    filename = ""


    if (tex.getType() == "Image"):

        if (len(image.getFilename()) > 0):

            i=0
            while i < len(image.getFilename()) :

                if image.getFilename()[i:i+1] == "\\" :
                    filename += "/"
                else :
                    filename += image.getFilename()[i:i+1]
        
                i+=1

        if (oldpath == filename[0:len(oldpath)]):

            temp = filename[len(oldpath):len(filename)]
            temp = newpath + temp
    
            image.filename = temp
            image.reload()
        
            image_count += 1
        
print ("image path's changed:")
print (image_count)

I did a few changes, but cant find the rest od my errors. I thing some stuff is now depricated but I cant find the 2.8 Version

here is the Path to the ORIGINAL Script https://blenderartists.org/t/script-to-change-paths-for-all-images/460733

$\endgroup$
3
$\begingroup$

You are right, this script is severely outdated (2009 !) This should do :

import bpy

# The path which should be replaced
oldpath = "C:\\OLDpath\\"

# The new path which replaces the old one
newpath = "D:\\NewPth\\"

image_count = 0

for image in bpy.data.images:
    if not image.filepath.startswith(oldpath):
        continue
    image.filepath = image.filepath.replace(oldpath, newpath)
    image.reload()
    image_count += 1

print(f"Image paths changed: {image_count}")

We take advantage of the python string method string.replace(old_string, new_string) to swap the paths. Note that they must both be written the same way, like in your example.

$\endgroup$
5
  • 2
    $\begingroup$ Or could for image in bpy.data.images: to change all image filepaths rather than only those assoc with an image texture. Q suggests this a bit, old script goes over textures? Wonder if it's worth mentioning relative paths, in that, for example: as long as a folder named images is in same folder as blend file then //images/foo.png will be path to image relative to blend file. Was on BA answering Qs back then. 2009 would have been a 2.49 hanger on... the change from 2.7 to 2.8 was nothing like the upheaval of 49b to 50 $\endgroup$
    – batFINGER
    Jun 17 '20 at 11:01
  • $\begingroup$ @batFINGER Thanks for the heads-up :) I agree it would be better to use relative paths in the case you describe, but if the image library is located somewhere else or on a different drive, you would still have to use absolute path, right ? $\endgroup$
    – Gorgious
    Jun 17 '20 at 11:16
  • 2
    $\begingroup$ for sure more in line with handy to use relative paths if you are moving folders around to avoid need to re-path. Another small thing consider avoiding reloading images with unchanged paths... filepath.startswith(oldpath) and report updated 3 of 10 (eg). $\endgroup$
    – batFINGER
    Jun 17 '20 at 11:25
  • $\begingroup$ @batFINGER Good call ! Thanks :) $\endgroup$
    – Gorgious
    Jun 17 '20 at 11:33
  • 2
    $\begingroup$ Thanx, works like a charm. And to the discurs about relatives and absolutes - I think relatives are always better in production, but sometimes you get FBX files from Evermotion and all textures are stored absolute. So you create blend files out of them and reorganize everything. Then this script is top notch.. afterwards we can do the abolute to relativ conversion and everything is back in line again. $\endgroup$ Jun 17 '20 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.