2
$\begingroup$

I have tried in several ways and the only one that worked for me was through a modal that extends from ImportHelper, however I would like to do everything from the main panel:

enter image description here

import bpy 
from bpy.props import StringProperty

class MainPanel(bpy.types.Panel):
    bl_idname = "Test"
    bl_label = "Test File"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = 'View'
 
    def draw(self, context):
        layout = self.layout
        scene = context.scene

        layout.prop(scene, 'file_')
        
def showPath(self, context):
    print("filepath: ", bpy.context.scene.file_)# return only //x_bot.json 
    print("self.filepath", self.filepath)# return error
    
def register():
    bpy.utils.register_class(MainPanel)
    bpy.types.Scene.file_ = bpy.props.StringProperty(
        name= "File", 
        subtype='FILE_PATH', 
        update=showPath
    )

def unregister():
    bpy.utils.unregister_class(MainPanel)

    
if __name__ == "__main__":
    register()
$\endgroup$

1 Answer 1

2
$\begingroup$

bpy.path.abspath()

Returns the absolute path relative to the current blend file using the “//” prefix.

TLDR; Just call bpy.path.abspath(your_path) to get the absolute path representation.


There is no reason to display the absolute path nor convert it because the user can set it to absolute or relative in the File Browser (N):

enter image description here

Also notice: there are several naming conventions like adding a separator to the name of your panel class or using lowercase names for functions and variables (PEP8):

import bpy

class CATEGORY_PT_CustomPanel(bpy.types.Panel):
    bl_idname = "OBJECT_PT_my_panel"
    bl_label = "My Panel"
    bl_space_type = "VIEW_3D"   
    bl_region_type = "UI"
    bl_category = "Tools"
    bl_context = "objectmode"

    def draw(self, context):
        layout = self.layout
        scn = context.scene

        col = layout.column(align=True)
        col.prop(scn, "my_path", text="")

        # Print the absolute and relative path to the console
        print ("REL:", scn.my_path)
        print ("ABS:", bpy.path.abspath(scn.my_path))


def register():
    bpy.utils.register_class(CATEGORY_PT_CustomPanel)
    bpy.types.Scene.my_path = bpy.props.StringProperty(name="File", subtype='FILE_PATH')

def unregister():
    bpy.utils.unregister_class(CATEGORY_PT_CustomPanel)
    del bpy.types.Scene.my_path


if __name__ == "__main__":
    register()

Related

$\endgroup$

You must log in to answer this question.

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