PyAPI: extend Menu.path_menu
- Add optional 'display_name' callback so callers can construct own names. - Add optional 'prop_filepath' argument (for operators that don't use "filepath"). - Add doc-string. - Use keyword only arguments.
This commit is contained in:
@@ -725,11 +725,30 @@ class Header(StructRNA, _GenericUI, metaclass=RNAMeta):
|
|||||||
class Menu(StructRNA, _GenericUI, metaclass=RNAMeta):
|
class Menu(StructRNA, _GenericUI, metaclass=RNAMeta):
|
||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
|
|
||||||
def path_menu(self, searchpaths, operator,
|
def path_menu(self, searchpaths, operator, *,
|
||||||
props_default=None, filter_ext=None):
|
props_default=None, prop_filepath="filepath",
|
||||||
|
filter_ext=None, display_name=None):
|
||||||
|
"""
|
||||||
|
Populate a menu from a list of paths.
|
||||||
|
|
||||||
|
:arg searchpaths: Paths to scan.
|
||||||
|
:type searchpaths: sequence of strings.
|
||||||
|
:arg operator: The operator id to use with each file.
|
||||||
|
:type operator: string
|
||||||
|
:arg prop_filepath: Optional operator filepath property (defaults to "filepath").
|
||||||
|
:type prop_filepath: string
|
||||||
|
:arg props_default: Properties to assign to each operator.
|
||||||
|
:type props_default: dict
|
||||||
|
:arg filter_ext: Optional callback that takes the file extensions.
|
||||||
|
|
||||||
|
Returning false excludes the file from the list.
|
||||||
|
|
||||||
|
:type filter_ext: Callable that takes a string and returns a bool.
|
||||||
|
:arg display_name: Optional callback that takes the full path, returns the name to display.
|
||||||
|
:type display_name: Callable that takes a string and returns a string.
|
||||||
|
"""
|
||||||
|
|
||||||
layout = self.layout
|
layout = self.layout
|
||||||
# hard coded to set the operators 'filepath' to the filename.
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import bpy.utils
|
import bpy.utils
|
||||||
@@ -752,15 +771,19 @@ class Menu(StructRNA, _GenericUI, metaclass=RNAMeta):
|
|||||||
files.sort()
|
files.sort()
|
||||||
|
|
||||||
for f, filepath in files:
|
for f, filepath in files:
|
||||||
props = layout.operator(operator,
|
# Intentionally pass the full path to 'display_name' callback,
|
||||||
text=bpy.path.display_name(f),
|
# since the callback may want to use part a directory in the name.
|
||||||
translate=False)
|
props = layout.operator(
|
||||||
|
operator,
|
||||||
|
text=display_name(filepath) if display_name else bpy.path.display_name(f),
|
||||||
|
translate=False,
|
||||||
|
)
|
||||||
|
|
||||||
if props_default is not None:
|
if props_default is not None:
|
||||||
for attr, value in props_default.items():
|
for attr, value in props_default.items():
|
||||||
setattr(props, attr, value)
|
setattr(props, attr, value)
|
||||||
|
|
||||||
props.filepath = filepath
|
setattr(props, prop_filepath, filepath)
|
||||||
if operator == "script.execute_preset":
|
if operator == "script.execute_preset":
|
||||||
props.menu_idname = self.bl_idname
|
props.menu_idname = self.bl_idname
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user