Developer Forum

A question about path manipulation

Blender itself seems to do something rather helpful/clever when a file using absolute paths from Windows (e.g. d:\blender\myfolder\myimage.png) is loaded on Linux. It goes looking in /d/blender/myfolder/myimage.png. This makes things nicely predictable.

I ran into an issue with DECALmachine where it stores a path as-is in the blender preferences, and then uses that value as-is whereever those preferences are loaded. On Linux, this means that it ends up creating ‘d:\blender\assets’ as the base folder name, rather than looking for /d/blender/assets. I was trying to find out how/where blender adjusts its path as described at the beginning, in order to provide a way to mirror this helpful blender behavior within the addon.

Is this done within core blender code, or via the Python API?

You can easily convert it on loading time through an handler.

This is an example of python functions for converting path linux/windows: path script

And then, on loading the file on another platform (example I use for VSE):

@persistent
def load_post_handler(dummy):
    for scene in bpy.data.scenes:
        if not scene.sequence_editor:
            continue
        sequences = scene.sequence_editor.sequences_all
        for strip in sequences:
            # Adapt path for linux or windows
            match strip.type:
                case 'MOVIE':
                    strip.filepath = convert_os_path(strip.filepath)
                case 'IMAGE':
                    strip.directory = convert_os_path(strip.directory)

bpy.app.handlers.load_post.append(load_post_handler)