15

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.

Thanks!

brockmann
  • 12,613
  • 4
  • 50
  • 93
doomslug
  • 1,809
  • 1
  • 18
  • 31

1 Answers1

26

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:",
        subtype='DIR_PATH')

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,
                       PointerProperty,
                       )

from bpy.types import (Panel,
                       PropertyGroup,
                       )


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

class MyProperties(PropertyGroup):

    path: StringProperty(
        name="",
        description="Path to Directory",
        default="",
        maxlen=1024,
        subtype='DIR_PATH')

# ------------------------------------------------------------------------
#    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 = (
    MyProperties,
    OBJECT_PT_CustomPanel
)

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

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

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


if __name__ == "__main__":
    register()

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: print(os.path.join(path,i))

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


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

p2or
  • 15,860
  • 10
  • 83
  • 143
  • Thanks! But how do I access the files in the directory? – doomslug Mar 11 '15 at 00:08
  • 1
    I've updated the answer, but as you can see that's not really blender specific. – p2or Mar 11 '15 at 10:53
  • 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. – Greg Mathieson Apr 26 '20 at 03:23
  • As print() is part of the draw() method, it's going to be re-drawn by Blender... Don't worry, just use it @GregMathieson – p2or Apr 27 '20 at 17:41