0

I've seen a number of answers on here and other places that if I have a file path in Blender, I can use bpy.path.abspath(file_path) and get the absolute path to a file or directory. My problem is that I have a StringProperty with subtype = 'DIR_PATH' set and I'm having problems with what it displays:

enter image description here

The path is set to /User/<my_name>/Documents/Dev/3DGraphics/3DExperiments, however notice only two slashes show. The StringProperty is showing a path relative to the current file in Blender. When I select another directory, it still gives me a relative path.

I'm using this structure:

class BluePrinterData(bpy.types.PropertyGroup):
    status: bpy.props.StringProperty(
        name="status",
        default="Waiting for Cameras to be Added"
        )
    project_name: bpy.props.StringProperty(
        name="project_name"
        )
    save_directory: bpy.props.StringProperty(
        name = "",
        description = "Output Directory",
        default = '',
        maxlen = 1024,
        subtype = 'DIR_PATH'
        )
    file_format: bpy.props.EnumProperty(
        name = 'File Output Format',
        description = 'File format for saving images',
        items = {('png', 'PNG', 'Save as PNG'),
                 ('pdf', 'PDF', 'Save as PDF'),
                 ('bmp', 'BMP', 'Save as BMP'),
                 ('tiff', 'TIFF', 'Save as TIFF')},
        default = 'png'
        )

Note the 3rd property down, save_directory, is the one I'm talking about and I've set the subtype as needed.

I've tried adding an if statement to change the path to absolute if it isn't, but that doesn't make a difference.

The one thing I'm trying to do here is make sure that instead of seeing the relative path in the StringProperty, I see the absolute path.

I've looked over API docs, but I can't find what I have to change to display an absolute path.

(Again, please note I know when I need that absolute path in Python, I can convert - but I can't find a way to convert what the StringProperty with the subtype = 'DIR_PATH' to show the absolute path.)

Tango
  • 511
  • 2
  • 22
  • TLDR; In the file dialog hit N and untick Relative (allows the user to decide). Exactly the same question: How can I get the full path of the selected file from the file explorer with python, Also related: https://blender.stackexchange.com/questions/12152/absolute-path-of-files-in-blender-with-python – brockmann Mar 28 '21 at 08:42
  • @brockmann: I was so busy going over the code in that answer that I didn't see the part about using 'N' while using the file browser. Also, I played around and changed default=' to default='//' and that changed it to display the absolute path, but I don't know why. CodeManX describes that, but I still don't get why the double-slash overrides and starts with an absolute path. – Tango Mar 28 '21 at 18:12
  • 1
    "//" prefix is a Blender specific identifier for the current blend file.: https://blender.stackexchange.com/a/6846/31447 You do not have to set any default path, just let the user decide whether the path should be absolute or relative as explained in my answer (second comment). – brockmann Mar 28 '21 at 18:20

1 Answers1

1

There is no reason to display the absolute path nor convert it or set some default path because the user can set it to absolute or relative in the File Browser (N) which is actually a nice feature. If you really need an absolute path representation at some point, just call bpy.path.abspath(path) as explained in How can I get the full path of the selected file from the file explorer?.

enter image description here

Further reading

brockmann
  • 12,613
  • 4
  • 50
  • 93