Script to change path's for all images

Hi,

excuse me if there is still such a script here around, but I didn’t found one in a short. I had the problem that the path to my document folder changed when I migrated to a new operating system this year. I store all the textures for a project into this project folder, but when the path changed for the reason I mentioned above, I had to reload every image manually. But this could be really annoying if there are a lot of them. So I decided to write a script which is able to change parts of the path’s automatically for all images. So if you have the same problem, you can use the script by copy it to the text editor window, changing the two variables ‘oldpath’ and ‘newpath’ the way I explain it below and run the script.


import Blender

from Blender import Texture,Image,Material

# insert the path which should be replaced
oldpath = "C:/Dokumente und Einstellungen/31/Eigene Dateien"
    
# insert the new path which replaces the old one
newpath = "C:/Users/31/sub"
    
image_count = 0

for tex in Blender.Texture.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

So here it goes:

For example your old directory structure looked like this:

c:\folder
ame\blender\project1 extures\bump.png

But the new path your ‘bump.png’ and all other textures are inside is:

c:\users est\blender\project1 extures\bump.png

Then you should give the variables the following values:


# insert the path which should be replaced
oldpath = "c:/folder/name"

# insert the new path which replaces the old one
newpath = "c:/users/test"

Now click ‘Text’ in the menu and ‘Run Python Script’. Another thing is that you have to use ‘/’ instead of ‘’ to seperate directories.

Hope that could be helpful…
cheers

I just have a few questions:

Is Material needed? I don’t see it used anywhere.

In this line:

if image.getFilename()[i:i+1] == “\” :

[i:i + 1] is only a slice of length 1, but you’re checking if it is equal to a string of length 2. How is this condition made true?

Also, couldn’t you fullfill the slash requirement by just saying:
oldpath = oldpath.replace("", “/”) ?

And going back, (if you like), by saying:
newpath = newpath.replace("/", “”) ?


Anyway, this seems pretty useful to me. I actually could have used it a couple times before. I don’t know why it never occured to me to make a script out of it…

No Material is not needed because the images are independed classes in python.

if image.getFilename()[i:i+1] == “\” :

[i:i + 1] is only a slice of length 1, but you’re checking if it is equal to a string of length 2. How is this condition made true?

This string “\” has only the length of one because the first '' is an escape sequence and tells with the second '' that I want a ''. If you do it like this:

if image.getFilename()[i:i+1] == "" :

than you dont search for the character '' but only for the escape sequence \

Also, couldn’t you fullfill the slash requirement by just saying:
oldpath = oldpath.replace("", “/”) ?

Yep thanks, I didn’t now that there is still a replace method for string’s thanks :slight_smile:

“This string “\” has only the length of one because the first ‘’ is an escape sequence and tells with the second ‘’ that I want a ‘’. If you do it like this:”

Oh, thanks for clearing that up. I didn’t know that, thanks for explaining!