# How to filter multiple file-extensions to limit which files display in an operator's file-selector?

I'm creating an image file importer based off the operator_file_import template. I want to set script so that it recognizes image files, because right now it's only recognizing text. How do I go about defining a list of extensions that show up in the file browser when I launch the script? Right now, based on the starting template, the only thing I can import is a .txt file.

Here is the code - The generic comments are still there and anything that I changed is indicated by a comment/CHANGED tag.

import bpy
from bpy.types import Operator
import collections
import os
from bpy.props import (
StringProperty,
BoolProperty,
EnumProperty,
IntProperty,
FloatProperty,
CollectionProperty,
)
from bpy_extras.io_utils import ImportHelper
from bpy.types import Operator

#CHANGED read definition to class name
f = open(filepath, 'r', encoding='utf-8')
f.close()

# would normally load the data here
print(data)

return {'FINISHED'}

#CHANGED class name
class ImportPbrTextures(Operator, ImportHelper):
"""This appears in the tooltip of the operator and in the generated docs"""
bl_idname = "import_test.pbr_textures"  # important since its how bpy.ops.import_test.some_data is constructed
bl_label = "Import PBR Textures"

#CHANGED filename_ext to png
# ImportHelper mixin class uses this
filename_ext = "*.png"

filter_glob = StringProperty(
default="*.png",
options={'HIDDEN'},
maxlen=255,  # Max internal buffer length, longer would be clamped.
)

# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
use_setting = BoolProperty(
name="Example Boolean",
description="Example Tooltip",
default=True,
)

type = EnumProperty(
name="Example Enum",
description="Choose between two items",
items=(('OPT_A', "First Option", "Description one"),
('OPT_B', "Second Option", "Description two")),
default='OPT_A',
)

def execute(self, context):

# Only needed if you want to add into a dynamic menu
self.layout.operator(ImportPbrTextures.bl_idname, text="Text Import Operator")

#CHANGED class registration to ImportPbrTextures class
def register():
bpy.utils.register_class(ImportPbrTextures)

def unregister():
bpy.utils.unregister_class(ImportPbrTextures)

if __name__ == "__main__":
register()

# test call
bpy.ops.import_test.pbr_textures('INVOKE_DEFAULT')

• This question isn't spesific to image importing, its just about extensions. – ideasman42 Jun 2 '16 at 8:14
• The script doesn't run AttributeError: Calling operator "bpy.ops.import_test.some_data" error, could not be found  – ideasman42 Jun 2 '16 at 8:18
• The code works, ie only shows png files, however you are test calling import_test.some_data rather than import_test.pbr_textures ... perhaps the former still had ".txt" as a glob. – batFINGER Jun 2 '16 at 8:18
• script now works and points to .png files. Problem was as batFINGER said, bpy.ops.import_test.some_data needed to be changed to import_test.pbr_textures – lvxejay Jun 2 '16 at 8:29
• All this code does is point to the image files now. Upon trying to load and read the images, I am met with this error: line 64, in execute NameError: name 'read_some_data' is not defined How do I define the script to read these files? Or more specifically, how can I make this script load the image into a texture node? – lvxejay Jun 2 '16 at 9:02

In the operator's filter_glob property, (which you've already defined), you can define multiple extensions using the ; separator, eg:

filter_glob = StringProperty(
default="*.png;*.jpg;*.jpeg;*.bmp;*.exr",
options={'HIDDEN'},
)


If you want to make a list of all extensions Blender supports you can use. bpy.path.extensions_image eg:

"*" + ";*".join(bpy.path.extensions_image)

• works properly! File importer script now points to multiple extensions – lvxejay Jun 2 '16 at 9:05
• @lvxejay bpy.path.extesions_image is a set of all image formats supported (by extension) – batFINGER Jun 2 '16 at 13:54
• @ideasman42 Where are these operator's properties documented. I cannot to seem to find any description of them besides in Blender source. – Zingam May 2 '18 at 6:24

Here is an example from http://web.purplefrog.com/~thoth/blender/python-cookbook/image-to-material-node.html :

def image_for(fname):
candidates = [ img for img in bpy.data.images if img.filepath == fname]
if len(candidates)<1:
else:
return candidates[0]

def node_of_type(nodes, type):
for node in nodes:
if node.type == type:
return node

def mission1(fname):

mat = bpy.data.materials.get("material")
if mat is None:
mat = bpy.data.materials.new("material")

mat.use_nodes = True
nodes = mat.node_tree.nodes

n1 = node_of_type(nodes, "TEX_IMAGE")
if n1 is None:

n1.location = (-200,0)
n1.image = image_for(fname)

bsdf = node_of_type(nodes, 'BSDF_DIFFUSE')