8
$\begingroup$

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!

$\endgroup$
14
$\begingroup$

For directories or file paths on any panel it's key using a StringProperty. In order to get a File Dialog instead of a Folder Dialog, set its subtype from DIR_PATH to FILE_PATH.

path = StringProperty(
        name = "",
        description="Choose a directory:",
        default="",
        maxlen=1024,
        subtype='DIR_PATH')

As of Blender 2.8x properties should be assigned using a single colon :

path : StringProperty(
        name = "",
        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

Blender 2.8x Update

import bpy

from bpy.props import (StringProperty,
                       PointerProperty,
                       )

from bpy.types import (Panel,
                       Operator,
                       AddonPreferences,
                       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()

Blender 2.7x

import bpy

from bpy.props import (StringProperty,
                       PointerProperty,
                       )

from bpy.types import (Panel,
                       Operator,
                       AddonPreferences,
                       PropertyGroup,
                       )

# ------------------------------------------------------------------------
#    UI (settings Class, Panel in Object Mode)
# ------------------------------------------------------------------------

class MySettings(PropertyGroup):

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

class OBJECT_PT_my_panel(Panel):
    bl_idname = "OBJECT_PT_my_panel"
    bl_label = "My Tool"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    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
# ------------------------------------------------------------------------

def register():
    bpy.utils.register_module(__name__)
    bpy.types.Scene.my_tool = PointerProperty(type=MySettings)

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

if __name__ == "__main__":
    register()

To collect all images in a folder use os.listdir() to return a list containing the files in the folder and make sure that the file type is correct. Simplest way is using a list comprehension:

import os

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

# collect all OpenExr files
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))

For more details about Property Appearance as well as on how to create Custom Interfaces, have a look into: How to create a custom UI?

$\endgroup$
  • $\begingroup$ Thanks! But how do I access the files in the directory? $\endgroup$ – Nathan Craddock Mar 11 '15 at 0:08
  • 1
    $\begingroup$ I've updated the answer, but as you can see that's not really blender specific. $\endgroup$ – p2or Mar 11 '15 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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