2
$\begingroup$

I have an absolute path. But I need it to become relative. After reading a few articles and tried to do something myself. But I was powerless. :( It's not the library that writes all the time. I can't figure out how to make a link with a relative path

bpy.ops.wm.link(
            filepath=os.path.join('Z:\\SuperRally\\assets\\characters\\alex\\alex_rig.blend', 'Collection', 'Alex_main'),
            directory=os.path.join('Z:\\SuperRally\\assets\\characters\\alex\\alex_rig.blend', 'Collection'),
            filename='Alex_main'
        )

It is work

directory = "//..\..\..\..\\assets\\characters\\higy\\higy_rig.blend\\Collection" bpy.path.relpath(directory) == directory bpy.ops.wm.link(directory=bpy.path.abspath(directory), filename="Higy_main")
$\endgroup$

3 Answers 3

2
$\begingroup$

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.

  • /blender

  • /blender/work <- the directory my current file is saved in

  • /blender/stuff <- the directory that the file I want to link to is in.

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.

$\endgroup$
2
  • 2
    $\begingroup$ it seems i won directory = "//..\..\..\..\\assets\\characters\\higy\\higy_rig.blend\\Collection" bpy.path.relpath(directory) == directory bpy.ops.wm.link(directory=bpy.path.abspath(directory), filename="Higy_main") $\endgroup$
    – Welet
    Commented Apr 20, 2022 at 10:59
  • 2
    $\begingroup$ I came up with this while analyzing your answer $\endgroup$
    – Welet
    Commented Apr 20, 2022 at 11:14
1
$\begingroup$

Get path of current opened blend file

import bpy
p = bpy.data.filepath

if p == "":
    print("unsaved file")
else:
    print(p)

Go back

bpy.path.abspath("//../hello.blend")
#to go back a directory

Make all paths to external files relative to current .blend

bpy.ops.file.make_paths_relative()

Link .blend file

import bpy

filepath = "//file.blend"

# link all objects
with bpy.data.libraries.load(filepath, link=True, relative=True) as (data_from, data_to):
    data_to.objects = [name for name in data_from.objects]
$\endgroup$
1
  • $\begingroup$ It's not the library that writes all the time. I can't figure out how to make a link with a relative path $\endgroup$
    – Welet
    Commented Apr 19, 2022 at 16:35
1
$\begingroup$

Does this not work? bpy.path.relpath() is builtin, seems one-and-done.

bpy.data.filepath
>>> '/home/jake/Documents/blender/projects/test.blend'

fp = bpy.data.filepath

bpy.path.relpath(fp)
>>> '//test.blend'
$\endgroup$
1
  • $\begingroup$ It is wok. directory = "//..\..\..\..\\assets\\characters\\higy\\higy_rig.blend\\Collection" bpy.path.relpath(directory) == directory bpy.ops.wm.link(directory=bpy.path.abspath(directory), filename="Higy_main") $\endgroup$
    – Welet
    Commented Apr 20, 2022 at 11:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .