Search/Replace all image paths

I’d like to offer the standard preemptive apology if this is in the wrong place. This is my first post, I usually just scour the existing posts for answers.

I also want to begin by saying that for the purpose of this question, let’s pretend I have absolutely no programming or scripting experience, because I certainly have no relevant experience. Also, my knowledge of blender itself is… er, patchy.

Now for the question…
I have a file, using Blender Cycles, called ColorPro.blend.
In it, I have 72 objects, each with its own material.
Each of the 72 materials contains an Image Texture node. The image paths are something like the following:
“/Links/Color Pro/ColorPro_Green.png”
“/Links/Color Pro/ColorPro_Red.png”
“/Links/Color Pro/ColorPro_Blue.png”

Now, I wish to copy/edit this file to a nearly identical file, but these links would instead need look like the following:
“/Links/Color Studio/ColorStudio_Green.png”
“/Links/Color Studio/ColorStudio_Red.png”
“/Links/Color Studio/ColorStudio_Blue.png”

As you can see this is a bit different than re-linking to a different drive (which is mostly what I found in my search), since all the file names are also slightly changed.

So, essentially what I think would be the easiest way to do this would be if I could search all image paths within the file for the string “Color Pro/ColorPro” and replace all instances with “Color Studio/ColorStudio”.

I did find one script here which seemed like it would be what I’m looking for but I’m getting an error when I try to run it.

Please forgive my ignorance, I would not ordinarily ask strangers for help with something like this, but it’s for work and time is a factor. And I’m going to need to do this several more times than in the files mentioned above.
Also, if it’s matters, I am using a MacBook Pro… I know. It’s not ideal, but it’s a work-issued laptop for a project that is a bit outside our ordinary scope.
Blender Version 2.79b

Let me know if I left anything out or was unclear with anything.

Thanks for any advice on this.

Hi, welcome!

This script assumes you’ll be replacing: Color Pro with Color Studio and file prefix ColorPro with ColorStudio. You can run it in the text editor.

It should go through every cycles material and image node, and replace the old path and prefix with the new ones, if it finds them. The total count of replaced paths will be output in the console.

Note, there’s no undo for this. If some error occurs, it would be best to do File > Revert.
If you’re using linked libraries, I would strongly recommend isolating or making copies of them before trying to run this, since all changes are passed to the source.

import bpy

old_path = "Color Pro"
old_file_prefix = "ColorPro"

old = old_path, old_file_prefix

new_path = "Color Studio"
new_file_prefix = "ColorStudio"

new = new_path, new_file_prefix

def replace():
    count = 0
    for mat in
        for node in mat.node_tree.nodes:
            if node.type == 'TEX_IMAGE':
                path = node.image.filepath

                if all(s in path for s in old):
                    count += 1
                    for a, b in zip(old, new):
                        path = path.replace(a, b)

                    node.image.filepath = path
           = path.split("/")[-1]
    return count

if __name__ == '__main__':
    count = replace()
    if count:
#        print(f"replaced {count} paths")
        print("replaced %s paths" % (count))

        print("nothing to replace")
1 Like

Thank you so much for your time and response. I’m getting the attached error message when I try to run that script. Am I perhaps doing something wrong?

I’ve tried pasting it in the Text Editor and hitting Run Script, as well as saving it as a py file and opening it.

Oh right. I wrote that on 2.79.7 which has a higher python version installed. Edited the post!

Thank you, you beautiful genius! That works like a dream. Thanks so much, you’ve saved me potentially hours of tedious weekend work!

1 Like