I'm trying to add a folder dialog box in the add-on I'm working on. I managed to add a button that opens a folder dialog and setup the path, but I'd like to be able to see that path rather than just printing it in the console.

Something like that :

Folder Dialog Box

I'm currently using an operator and I found out that I need to use a Property, here's what I tried :

class project_path(bpy.types.PropertyGroup):
path: bpy.props.StringProperty(subtype="DIR_PATH")

and then tried to call it in my Panel with :

class PT_Panel(Panel):
    bl_space_type = "VIEW_3D"
    bl_region_type ="UI"
    bl_label = "Operations"
    bl_category = "Tools"

    def draw(self, context):

        layout = self.layout

        sce = context.scene

        box = layout.box()
        box.label( text = "Project Path")

        row = box.row()
        row.prop(sce.project_path,"path",text='Project Path')

But then I got that error :

AttributeError: 'Scene' object has no attribute 'project_path'

Can't find any help on that, I guess I'm not using the correct attribute, but I've tried a bunch of different things without success, am I not supposed to use the class as an attribute ?

I found this question that is relevant, but it's also not explaining where the attribute is taken from.


This is how you get it to work

Import :

from bpy.props import PointerProperty

In your PT_Panel class:

ProjPath = context.scene.proj_path
row.prop(ProjPath, "path", text='Project Path')

While registering:

classes = [project_path, PT_Panel]

def register():

    for cls in classes :
        bpy.types.Scene.proj_path= PointerProperty(type= project_path)

def unregister():

    for cls in classes :
        del bpy.types.Scene.proj_path

if __name__ == "__main__":

If you want to save it permanently then use AddonPrefrences

And try to improve your naming skills :

  1. Darkfall - https://www.youtube.com/watch?v=i12oOBI3E28
  2. Red-K Production- https://www.youtube.com/watch?v=V1gU50MKulw
  • $\begingroup$ Thanks for the help on that, being using python for a week or so and trying to do an add-on for like 2 days so still a lot to learn. Your solution works, I had to modify the identation a bit for the pointerproperty registration and unregister but it works ! I will definitely get a look at the naming convention videos you gave me. Thank you so much for your help ! – $\endgroup$
    – CédricF
    Jan 20 at 17:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.