To do this with bpy.ops.wm.link
, you need to rely on the function itself to store the path in your blend file as a relative path. Fortunately it does that by default because it has a keyword argument relative_path
that defaults to True
.
First, you don't need the filepath
keyword argument.
Second, you need to create a directory entry string. You can do this by starting with a relative path like //relative_path_to_file.blend
.
Finally you need to add a "directory" to this string. This is the section of the file that you want to take your link from. In my example, I'm going to link a material, so I need to add /Material
.
I can write this code to get a path relative name:
directory = "//../stuff/src.blend/Material"
but bpy.ops.wm.link
needs an absolute path, so I use bpy.path.abspath()
to convert it. The call to bpy.ops.wm.link
looks like this:
bpy.ops.wm.link(directory=bpy.path.abspath(directory), filename="ShinyRed")
where "ShinyRed"
is the name of the material I want to link to.
putting it all together gives:
directory = "//../stuff/src.blend/Material"
bpy.ops.wm.link(directory=bpy.path.abspath(directory), filename="ShinyRed")
and when I check Libraries in the outliner under Blender File display mode, I see that I am given a relative path name.
Since you have absolute paths, I should assure you that you'll still end up with a relative path if you do this:
bpy.ops.wm.link(directory='Z:\\SuperRally\\assets\\characters\\alex\\alex_rig.blend\\Collection',
filename='Alex_main')
and, again, you don't need the filepath
argument.