dont display any file as a preset in the menu (limit to *.py, *.xml)

backup files like .py~ for eg had their own menu entries which got annoying.

added optional filter_ext function callback argument to Menu.path_menu() to avoid displaying invalid types.
This commit is contained in:
Campbell Barton 2012-04-07 02:19:11 +00:00
parent 628b183d98
commit b9f9aa947a

@ -671,7 +671,7 @@ class Header(StructRNA, _GenericUI, metaclass=RNAMeta):
class Menu(StructRNA, _GenericUI, metaclass=RNAMeta):
__slots__ = ()
def path_menu(self, searchpaths, operator, props_default={}):
def path_menu(self, searchpaths, operator, props_default={}, filter_ext=None):
layout = self.layout
# hard coded to set the operators 'filepath' to the filename.
@ -687,17 +687,16 @@ class Menu(StructRNA, _GenericUI, metaclass=RNAMeta):
files = []
for directory in searchpaths:
files.extend([(f, os.path.join(directory, f))
for f in os.listdir(directory)])
for f in os.listdir(directory)
if (not f.startswith("."))
if ((filter_ext is None) or
(filter_ext(os.path.splitext(f)[1])))
])
files.sort()
for f, filepath in files:
if f.startswith("."):
continue
preset_name = bpy.path.display_name(f)
props = layout.operator(operator, text=preset_name)
props = layout.operator(operator, text=bpy.path.display_name(f))
for attr, value in props_default.items():
setattr(props, attr, value)
@ -714,4 +713,5 @@ class Menu(StructRNA, _GenericUI, metaclass=RNAMeta):
"""
import bpy
self.path_menu(bpy.utils.preset_paths(self.preset_subdir),
self.preset_operator)
self.preset_operator,
filter_ext=lambda ext: ext.lower() in {".py", ".xml"})