Fix broken links throught script


(cymps) #1

I’ve reorganized a project’s folder and naming convention and now I have about 80 blend files with broken links to fix. every file has about 5-20 links that needs relocating.

is it possible to do that through scripting?
I would like to write a scripts that checks the scene for broken links, then replaces a wrong path with a correct one.

I’m very new to python, so I’m not sure how do I address links and edit properties like their path.

thanks in advance :slight_smile:


(mavek) #2

what type of links? files I imagine, the path is no longer correct? Probably due to some explicit file naming convention. But the path is present in a relative location.

I had to do something recently, but it was at the addon level, i don’t think there is a refind paths button


(cymps) #3
a friend helped me solved it. it was rather simple, I just needed to know how to access the library path, 
the code I ended up with was something like this:

import bpy
import os

def fixLink(prev_name, new_name):
#goes over all libraries in current blend file and replaces paths containing "prev_name" with "new_name"
    for lib in bpy.data.libraries:
       path = lib.filepath
       if prev_name in path:
           endChar = lib.filepath.rfind('\\', 0, len(lib.filepath))
           lib.filepath = lib.filepath[0:endChar+1]+new_name+".blend" #concats path with new file name  
           print("Path %s was changed to %s \n" % (path, lib.filepath))


prev_name_list = []
new_name_list = []

for i in range(len(new_name_list)):
        fixLink(prev_name_list[i], new_name_list[i])

bpy.ops.wm.save_as_mainfile(filepath=bpy.data.filepath)
bpy.ops.wm.revert_mainfile()`Preformatted text`