In my Panel class I use:

col.prop(context.scene, 'path')

I also have:

def register():
bpy.types.Scene.path = bpy.props.StringProperty \
  name = "Folder with textures",
  default = "",
  description = "Select folder with images to import",
  subtype = 'DIR_PATH'

def unregister():
del bpy.types.Scene.path

The above gives me:

enter image description here

How to add all images from selected folder to my .blend file?

I know how to create a button, but I don't know how to add files from directory.


1 Answer 1


You can add an adapted version of this nice generator to your code to find all images in a given folder (such as the ones you keep in your Scene.path property):

import bpy, os
def path_iterator(folder_path):
    for fp in os.listdir(folder_path):
        if fp.endswith( tuple( bpy.path.extensions_image ) ):
            yield fp

Then you can iterate over the images and do whatever you want with them, for instance to add them to the list of images blender can use for textures and other purposes:

for imgPath in path_iterator( bpy.context.scene.path ):
    fullPath = os.path.join( bpy.context.scene.path, imgPath )
    bpy.ops.image.open( filepath = fullPath )
  • 2
    $\begingroup$ you can use fp.endswith(('.png', '.jpg', '.tif', '.hdr')) , feed it a tuple. $\endgroup$
    – zeffii
    Oct 14, 2015 at 10:37
  • $\begingroup$ Nice,didn't know that. $\endgroup$
    – TLousky
    Oct 14, 2015 at 10:40
  • 2
    $\begingroup$ @TLousky bpy.path.extesions_image is a set of all image formats supported (by extension) $\endgroup$
    – batFINGER
    Oct 14, 2015 at 12:09
  • $\begingroup$ @batFINGER awesome, will update the script accordingly. $\endgroup$
    – TLousky
    Oct 14, 2015 at 12:12
  • 1
    $\begingroup$ if fp = "texnormal.png", then fp[-10:] = "normal.png". So the conditional fp[-10:] in ['normal'] will result with a False value (and the brackets [] shouldn't be there since you're testing for substrings within strings). You can reverse it to 'normal' in fp[-10:] which will work, or to fn[-10:-4] == 'normal'. $\endgroup$
    – TLousky
    Oct 14, 2015 at 13:11

You must log in to answer this question.

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