Skip to content

Layout Template List

BleuRaven edited this page Nov 6, 2024 · 6 revisions
  1. Import and register bbpl.
from . import bbpl
def register():
    bbpl.register()

def unregister():
    bbpl.unregister()
  1. Create a child class of BBPL_UI_TemplateItem, BBPL_UL_TemplateItemDraw and BBPL_UI_TemplateList from bbpl.blender_layout.layout_template_list.
    - BBPL_UI_TemplateList manage the list and actions.
    - BBPL_UI_TemplateItem manage the item and content.
    - BBPL_UL_TemplateItemDraw manage how the item should draw. In your TemplateList class you need set your Item Class and Draw Item Class.
BBPL_UI_TemplateItem = bbpl.blender_layout.layout_template_list.types.create_template_item_class()
BBPL_UL_TemplateItemDraw = bbpl.blender_layout.layout_template_list.types.create_template_item_draw_class()
BBPL_UI_TemplateList = bbpl.blender_layout.layout_template_list.types.create_template_list_class(BBPL_UI_TemplateItem, BBPL_UL_TemplateItemDraw)


class MyAddon_UI_TemplateItem(BBPL_UI_TemplateItem): # Item class (bpy.types.PropertyGroup)
    pass

class MyAddon_UI_TemplateItemDraw(BBPL_UL_TemplateItemDraw): # Draw Item class (bpy.types.UIList)
    pass

class MyAddon_UI_TemplateList(BBPL_UI_TemplateList): # Template List class
    template_collection: bpy.props.CollectionProperty(type=MyAddon_UI_TemplateItem)
    template_collection_uilist_class_name = "MyAddon_UI_TemplateItemDraw"
  1. register the childs class and create a pointer of your TemplateList class.
bpy.utils.register_class(MyAddon_UI_TemplateItem)
bpy.utils.register_class(MyAddon_UI_TemplateItemDraw)
bpy.utils.register_class(MyAddon_UI_TemplateList) # Need be register after Item and Draw Item.

bpy.types.Scene.my_properties_list = bpy.props.PointerProperty(type=MyAddon_UI_TemplateList)

You can now use draw(layout: bpy.types.UILayout) on your list for draw in the UI

my_properties_list.draw(layout)

Here the result:
Layout Template List

  1. Edit your Item Class like an bpy.types.PropertyGroup for add the content and property you need.
# Example
class MyAddon_UI_TemplateItem(BBPL_UI_TemplateItem): # Your Item class
    use: bpy.props.BoolProperty(
        name="Use",
        default=True
        )

    name: bpy.props.StringProperty(
        name="Bone groups name",
        description="Your bone group",
        default="MyGroup",
        )
  1. Edit your Draw Item Class following bpy.types.UIList API. Basic UIList Example
# Example
class MyAddon_UI_TemplateItemDraw(BBPL_UL_TemplateItemDraw): # Your Draw Item class
    def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):

        prop_line = layout

        indexText = layout.row()
        indexText.alignment = 'LEFT'
        indexText.scale_x = 1
        indexText.label(text=str(index))

        prop_use = prop_line.row()
        prop_use.alignment = 'LEFT'
        prop_use.prop(item, "use", text="")

        #icon = bbpl.ui_utils.getIconByGroupTheme(item.theme)
        icon = "NONE"

        prop_data = prop_line.row()
        prop_data.alignment = 'EXPAND'
        prop_data.prop(item, "name", text="")
        prop_data.enabled = item.use
  1. API
    • draw(layout: bpy.types.UILayout) for draw the template.
    • get_template_collection() for get the list collection. Now you can also use len()
    • get_active_index() Get the active index
    • get_active_item() Get the active item
    • clear()

BBPL_UI_TemplateList can be used with the native list fonctions like len(), iter()...

Clone this wiki locally