# How to add a "select path" input in a UI addon script?

I have to allow users of my addon to select a path via the directory browser window of blender.

The path is already set in an external ".conf" file, but I need it to be customizable via blender GUI, to avoid users to edit the config file manually.

I tried many things, (note that I'm a novice with the blender API), for example :

#(...)
bpy.ops.buttons.directory_browse()
# or
col = layout.column()
col.operator("file.directory")
# or
col.operator("buttons.directory_browse")
# or
col.prop(path, 'choose a folder')
#(...)


But nothing works. And can't find a way to do this after a day of seeking.

What I want to achieve is something like this :
Note that the input could be pre-filled with the config file's default value.

• I'm completely new to all this. I wanted to know if theres a way of storing a path enter by the user in the select path box into a variable PATHVar in order to use it later.. bpy.ops.export_scene.fbx( filepath=PATHVar + name + ".fbx") I know that I should be using the solution mentioned by Polosson but conf_path would simply batch export to the blender directory and not a custom one. I only ask here because it seems not to be much information around addon development really. Thanks in advance :)
– Clss
Jan 26, 2019 at 15:09

Well, I finally found a solution:

First, I had to define a custom property in the register() function:

def register():
bpy.utils.register_module(__name__)
bpy.types.Scene.conf_path = bpy.props.StringProperty \
(
name = "Root Path",
default = "",
description = "Define the root path of the project",
subtype = 'DIR_PATH'
)

def unregister():
bpy.utils.unregister_module(__name__)
del bpy.types.Scene.conf_path


Then in the draw() method of the panel class, I had to call the property this way:

def draw(self, context):
layout = self.layout
col = layout.column()
col.prop(context.scene, 'conf_path')


And... it worked!

Now, I wanted to display the default value (which is stored in a separate ".conf" file), in the "file input" when Blender starts (or the add-on is enabled).
To do so, I just had to call a function which get this value, and put it in the "default" parameter of the property definition:

def register():
default_path = get_path_from_config_file()
bpy.utils.register_module(__name__)
bpy.types.Scene.conf_path = bpy.props.StringProperty \
(
name = "Root Path",
default = default_path,
description = "Define the root path of the project",
subtype = 'DIR_PATH'
)


And that's it!

Use the layout.template_image command:

template_image(data, property, image_user, compact=False)


Item(s). User interface for selecting images and their source paths

Parameters:

• data (AnyType, (never None)) – Data from which to take property
• property (string) – Identifier of property in data
• compact (boolean, (optional)) – Use more compact layout

Here's a link to the relevant documentation for bpy.types.UILayout.

• Well, I tried this too, but can't get it to work. Anyway, in my script the path won't be used for images. However, I think I finally found a solution, so I'll answer my own question. Thanks ! Aug 14, 2013 at 15:50