2

In my Panel class I use:

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

I also have:

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


def unregister():
bpy.utils.unregister_class(PBRGROUP)
bpy.utils.unregister_module(__name__)
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.

Mc Gavish
  • 159
  • 1
  • 10

1 Answers1

4

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 )
TLousky
  • 16,043
  • 1
  • 40
  • 72
  • 2
    you can use fp.endswith(('.png', '.jpg', '.tif', '.hdr')) , feed it a tuple. – zeffii Oct 14 '15 at 10:37
  • Nice,didn't know that. – TLousky Oct 14 '15 at 10:40
  • 2
    @TLousky bpy.path.extesions_image is a set of all image formats supported (by extension) – batFINGER Oct 14 '15 at 12:09
  • @batFINGER awesome, will update the script accordingly. – TLousky Oct 14 '15 at 12:12
  • To go OTT maybe path_iterator returned a files collection consistent with multi flie operators. bpy.ops.image.open(directory=fp, files=path_image_files(fp)) – batFINGER Oct 14 '15 at 12:24
  • That could be useful (especially for this specific use), though might be a bit less versatile. No time today for the research involved though (collection format). – TLousky Oct 14 '15 at 12:34
  • My image's name is texnormal.png. I want to import this image by extension (.png) and "normal" text in its name. I wrote this code, but it doesn't work. What's wrong with it? if fp.endswith(supported_file_types) and fp[-10:] in ['normal']: – Mc Gavish Oct 14 '15 at 13:06
  • 1
    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'. – TLousky Oct 14 '15 at 13:11
  • I have one more question. I created a text box: box.prop(context.scene, "normal_map_string") and how to replace 'normal' by user defined text in this line 'normal' in fp[-10:]? – Mc Gavish Oct 15 '15 at 16:05
  • I wrote this: if fp.endswith(supported_file_types) and "context.scene.normal_map_string" in fp[-10:]: but it's not working – Mc Gavish Oct 15 '15 at 17:47
  • I found the solution. (bpy.context.scene.normal_map_string) in fp[-10:]) works perfectly. – Mc Gavish Oct 16 '15 at 14:07