So I need to allow the user of my script to choose a directory that contains images, and then run my script on each image. I already have my script made here. I looked at this question already, but that is about exporting, not importing.

It should look like this:

![image.. yep](https://db.tt/PTsp3q6C)

And should allow the user to choose the directory.



1 Answer 1


For directories or file paths you can use a StringProperty. In order to get a File Dialog instead of a Folder Dialog, simply set its subtype from DIR_PATH to FILE_PATH.

path: bpy.props.StringProperty(
        name = "Custom Path",
        description="Choose a directory:",

Following example adds a Folder Dialog to the Tool Shelf and prints the path to the console.

enter image description here

import bpy

from bpy.props import (StringProperty,
from bpy.types import (Panel,

# ------------------------------------------------------------------------
#    Scene Properties
# ------------------------------------------------------------------------

class MyProperties(PropertyGroup):

    path: StringProperty(
        description="Path to Directory",

# ------------------------------------------------------------------------
#    Panel in Object Mode
# ------------------------------------------------------------------------

class OBJECT_PT_CustomPanel(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_tool, "path", text="")

        # print the path to the console
        print (scn.my_tool.path)

# ------------------------------------------------------------------------
#    Registration
# ------------------------------------------------------------------------
classes = (

def register():
    from bpy.utils import register_class
    for cls in classes:

    bpy.types.Scene.my_tool = PointerProperty(type=MyProperties)

def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
    del bpy.types.Scene.my_tool

if __name__ == "__main__":

Further reading: How to create a custom UI?

To collect all images in a folder, you can use e.g. os.listdir() to return a list of the files in the folder and make sure that the file type is correct:

import os

# Path to the folder
path = '/home/user/Desktop/'

# Collect all OpenEXR files within the folder
exr_list = [f for f in os.listdir(path) if f.endswith('.exr')] 

# Iterate through the list
for i in exr_list:

Further reading: How do I list all files of a directory?

See the revisions of this answer for Blender prior to 2.8.

  • $\begingroup$ Thanks! But how do I access the files in the directory? $\endgroup$
    – doomslug
    Commented Mar 11, 2015 at 0:08
  • 1
    $\begingroup$ I've updated the answer, but as you can see that's not really blender specific. $\endgroup$
    – p2or
    Commented Mar 11, 2015 at 10:53
  • $\begingroup$ This example does add a folder dialog to the tool shelf and prints the path to the console, but why does it print the path to the console random numerous times? Sometimes 6, sometimes 11, 9, etc. But never just once. I'd like to use it in an add-on but I have a feeling it won't work if I can't get it to do what it's supposed to do just once. $\endgroup$ Commented Apr 26, 2020 at 3:23
  • $\begingroup$ As print() is part of the draw() method, it's going to be re-drawn by Blender... Don't worry, just use it @GregMathieson $\endgroup$
    – p2or
    Commented Apr 27, 2020 at 17:41

You must log in to answer this question.

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