PyAPI: updates to bl_ui.generic_ui_list which didn't follow conventions

- Replace type annotations with doc-strings, the current conventions is
  not to use type annotations in startup scripts.
- Replace abbreviation "idx" with "index" in public arguments/properties.
- Replace `len(..) > 0` with boolean checks.
- Add `__all__` to list public members.
- Use `arg` instead of `param` for doc-strings.
- Locate the doc-string so it shows as `__doc__`.
This commit is contained in:
Campbell Barton 2023-02-03 14:52:16 +11:00
parent 266d8de687
commit c468aeefb5
2 changed files with 72 additions and 67 deletions

View File

@ -1,7 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-or-later
import bpy
from bpy.types import Operator, UILayout, Context
from bpy.props import EnumProperty, StringProperty
"""
This module (in particular the draw_ui_list function) lets you draw the commonly
@ -15,54 +12,67 @@ You can get an example of how to use this via the Blender Text Editor->
Templates->Ui List Generic.
"""
import bpy
from bpy.types import Operator
from bpy.props import (
EnumProperty,
StringProperty,
)
__all__ = (
"draw_ui_list",
)
def draw_ui_list(
layout: UILayout,
context: Context,
layout,
context,
class_name="UI_UL_list",
*,
unique_id="",
list_path: str,
active_idx_path: str,
list_path,
active_index_path,
insertion_operators=True,
move_operators=True,
menu_class_name="",
**kwargs) -> UILayout:
**kwargs,
):
"""
Draw a UIList with Add/Remove/Move buttons and a menu.
:param layout:
UILayout to draw the list in.
:param context:
Blender context to get the list data from.
:param class_name:
Name of the UIList class to draw. The default is the
UIList class that ships with Blender.
:param unique_id:
Optional identifier, in case wanting to draw multiple unique copies of a list.
:arg layout: UILayout to draw the list in.
:type layout: :class:`UILayout`
:arg context: Blender context to get the list data from.
:type context: :class:`Context`
:arg class_name: Name of the UIList class to draw. The default is the UIList class that ships with Blender.
:type class_name: str
:arg unique_id: Optional identifier, in case wanting to draw multiple unique copies of a list.
:type unique_id: str
:arg list_path: Data path of the list relative to context, eg. "object.vertex_groups".
:type list_path: str
:arg active_index_path: Data path of the list active index integer relative to context,
eg. "object.vertex_groups.active_index".
:type active_index_path: str
:arg insertion_operators: Whether to draw Add/Remove buttons.
:type insertion_operators: bool
:arg move_operators: Whether to draw Move Up/Down buttons.
:type move_operators: str
:arg menu_class_name: Identifier of a Menu that should be drawn as a drop-down.
:type menu_class_name: str
:param list_path:
Data path of the list relative to context, eg. "object.vertex_groups".
:param active_idx_path:
Data path of the list active index integer relative to context,
eg. "object.vertex_groups.active_index".
:returns: The right side column.
:rtype: :class:`UILayout`.
:param insertion_operators:
Whether to draw Add/Remove buttons.
:param move_operators:
Whether to draw Move Up/Down buttons.
:param menu_class_name:
Name of a Menu that should be drawn as a drop-down.
Additional keyword arguments are passed to template_list().
Additional keyword arguments are passed to :class:`UIList.template_list`.
"""
row = layout.row()
list_owner_path, list_prop_name = list_path.rsplit('.', 1)
list_owner = _get_context_attr(context, list_owner_path)
idx_owner_path, idx_prop_name = active_idx_path.rsplit('.', 1)
idx_owner = _get_context_attr(context, idx_owner_path)
index_owner_path, index_prop_name = active_index_path.rsplit('.', 1)
index_owner = _get_context_attr(context, index_owner_path)
list_to_draw = _get_context_attr(context, list_path)
@ -70,8 +80,8 @@ def draw_ui_list(
class_name,
unique_id,
list_owner, list_prop_name,
idx_owner, idx_prop_name,
rows=4 if len(list_to_draw) > 0 else 1,
index_owner, index_prop_name,
rows=4 if list_to_draw else 1,
**kwargs
)
@ -81,7 +91,7 @@ def draw_ui_list(
_draw_add_remove_buttons(
layout=col,
list_path=list_path,
active_idx_path=active_idx_path,
active_index_path=active_index_path,
list_length=len(list_to_draw)
)
layout.separator()
@ -90,11 +100,11 @@ def draw_ui_list(
col.menu(menu_class_name, icon='DOWNARROW_HLT', text="")
col.separator()
if move_operators and len(list_to_draw) > 0:
if move_operators and list_to_draw:
_draw_move_buttons(
layout=col,
list_path=list_path,
active_idx_path=active_idx_path,
active_index_path=active_index_path,
list_length=len(list_to_draw)
)
@ -104,50 +114,50 @@ def draw_ui_list(
def _draw_add_remove_buttons(
*,
layout: UILayout,
list_path: str,
active_idx_path: str,
list_length: int
) -> None:
layout,
list_path,
active_index_path,
list_length,
):
"""Draw the +/- buttons to add and remove list entries."""
add_op = layout.operator(UILIST_OT_entry_add.bl_idname, text="", icon='ADD')
add_op.list_path = list_path
add_op.active_idx_path = active_idx_path
add_op.active_index_path = active_index_path
row = layout.row()
row.enabled = list_length > 0
remove_op = row.operator(UILIST_OT_entry_remove.bl_idname, text="", icon='REMOVE')
remove_op.list_path = list_path
remove_op.active_idx_path = active_idx_path
remove_op.active_index_path = active_index_path
def _draw_move_buttons(
*,
layout: UILayout,
list_path: str,
active_idx_path: str,
list_length: int
) -> None:
layout,
list_path,
active_index_path,
list_length,
):
"""Draw the up/down arrows to move elements in the list."""
col = layout.column()
col.enabled = list_length > 1
move_up_op = layout.operator(UILIST_OT_entry_move.bl_idname, text="", icon='TRIA_UP')
move_up_op.direction = 'UP'
move_up_op.list_path = list_path
move_up_op.active_idx_path = active_idx_path
move_up_op.active_index_path = active_index_path
move_down_op = layout.operator(UILIST_OT_entry_move.bl_idname, text="", icon='TRIA_DOWN')
move_down_op.direction = 'DOWN'
move_down_op.list_path = list_path
move_down_op.active_idx_path = active_idx_path
move_down_op.active_index_path = active_index_path
def _get_context_attr(context: Context, data_path: str) -> object:
def _get_context_attr(context, data_path):
"""Return the value of a context member based on its data path."""
return context.path_resolve(data_path)
def _set_context_attr(context: Context, data_path: str, value: object) -> None:
def _set_context_attr(context, data_path, value) -> None:
"""Set the value of a context member based on its data path."""
owner_path, attr_name = data_path.rsplit('.', 1)
owner = context.path_resolve(owner_path)
@ -160,19 +170,18 @@ class GenericUIListOperator:
bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
list_path: StringProperty()
active_idx_path: StringProperty()
active_index_path: StringProperty()
def get_list(self, context) -> str:
return _get_context_attr(context, self.list_path)
def get_active_index(self, context) -> str:
return _get_context_attr(context, self.active_idx_path)
return _get_context_attr(context, self.active_index_path)
def set_active_index(self, context, index):
_set_context_attr(context, self.active_idx_path, index)
_set_context_attr(context, self.active_index_path, index)
# noinspection PyPep8Naming
class UILIST_OT_entry_remove(GenericUIListOperator, Operator):
"""Remove the selected entry from the list"""
@ -190,7 +199,6 @@ class UILIST_OT_entry_remove(GenericUIListOperator, Operator):
return {'FINISHED'}
# noinspection PyPep8Naming
class UILIST_OT_entry_add(GenericUIListOperator, Operator):
"""Add an entry to the list after the current active item"""
@ -210,7 +218,6 @@ class UILIST_OT_entry_add(GenericUIListOperator, Operator):
return {'FINISHED'}
# noinspection PyPep8Naming
class UILIST_OT_entry_move(GenericUIListOperator, Operator):
"""Move an entry in the list up or down"""
@ -219,8 +226,8 @@ class UILIST_OT_entry_move(GenericUIListOperator, Operator):
direction: EnumProperty(
name="Direction",
items=[('UP', 'UP', 'UP'),
('DOWN', 'DOWN', 'DOWN')],
items=(('UP', 'UP', 'UP'),
('DOWN', 'DOWN', 'DOWN')),
default='UP'
)
@ -241,9 +248,7 @@ class UILIST_OT_entry_move(GenericUIListOperator, Operator):
return {'FINISHED'}
# =============================================
# Registration
# Registration.
classes = (
UILIST_OT_entry_remove,
UILIST_OT_entry_add,

View File

@ -19,7 +19,7 @@ class MyPanel(bpy.types.Panel):
layout,
context,
list_context_path="scene.my_list",
active_idx_context_path="scene.my_list_active_idx"
active_index_context_path="scene.my_list_active_index"
)
@ -34,13 +34,13 @@ class_register, class_unregister = bpy.utils.register_classes_factory(classes)
def register():
class_register()
bpy.types.Scene.my_list = bpy.props.CollectionProperty(type=MyPropGroup)
bpy.types.Scene.my_list_active_idx = bpy.props.IntProperty()
bpy.types.Scene.my_list_active_index = bpy.props.IntProperty()
def unregister():
class_unregister()
del bpy.types.Scene.my_list
del bpy.types.Scene.my_list_active_idx
del bpy.types.Scene.my_list_active_index
register()