4
class MATERIAL_UL_matslots_example(bpy.types.UIList):
5
# The draw_item function is called for each item of the collection that is visible in the list.
6
# data is the RNA object containing the collection,
7
# item is the current drawn item of the collection,
8
# icon is the "computed" icon for the item (as an integer, because some objects like materials or textures
9
# have custom icons ID, which are not available as enum items).
10
# active_data is the RNA object containing the active property for the collection (i.e. integer pointing to the
11
# active item of the collection).
12
# active_propname is the name of the active property (use 'getattr(active_data, active_propname)').
13
# index is index of the current item in the collection.
14
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
18
# draw_item must handle the three layout types... Usually 'DEFAULT' and 'COMPACT' can share the same code.
4
class MESH_UL_mylist(bpy.types.UIList):
6
# Be careful not to shadow FILTER_ITEM (i.e. UIList().bitflag_filter_item)!
7
# E.g. VGROUP_EMPTY = 1 << 0
9
# Custom properties, saved with .blend file. E.g.
10
# use_filter_empty = bpy.props.BoolProperty(name="Filter Empty", default=False, options=set(),
11
# description="Whether to filter empty vertex groups")
13
# Called for each drawn item.
14
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index, flt_flag):
15
# 'DEFAULT' and 'COMPACT' layout types should usually use the same draw code.
19
16
if self.layout_type in {'DEFAULT', 'COMPACT'}:
20
# You should always start your row layout by a label (icon + text), this will also make the row easily
21
# selectable in the list!
22
# We use icon_value of label, as our given icon is an integer value, not an enum ID.
23
# Note "data" names should never be translated!
24
layout.label(text=ma.name if ma else "", translate=False, icon_value=icon)
25
# And now we can add other UI stuff...
26
# Here, we add nodes info if this material uses (old!) shading nodes.
27
if ma and not context.scene.render.use_shading_nodes:
28
manode = ma.active_node_material
30
# The static method UILayout.icon returns the integer value of the icon ID "computed" for the given
32
layout.label(text="Node %s" % manode.name, translate=False, icon_value=layout.icon(manode))
34
layout.label(text="Node <none>", translate=False)
37
18
# 'GRID' layout type should be as compact as possible (typically a single icon!).
38
19
elif self.layout_type in {'GRID'}:
39
layout.alignment = 'CENTER'
40
layout.label(text="", icon_value=icon)
43
# And now we can use this list everywhere in Blender. Here is a small example panel.
44
class UIListPanelExample(bpy.types.Panel):
45
"""Creates a Panel in the Object properties window"""
46
bl_label = "UIList Panel"
47
bl_idname = "OBJECT_PT_ui_list_example"
48
bl_space_type = 'PROPERTIES'
49
bl_region_type = 'WINDOW'
52
def draw(self, context):
57
# template_list now takes two new args.
58
# The first one is the identifier of the registered UIList to use (if you want only the default list,
59
# with no custom draw code, use "UI_UL_list").
60
layout.template_list("MATERIAL_UL_matslots_example", "", obj, "material_slots", obj, "active_material_index")
62
# The second one can usually be left as an empty string. It's an additional ID used to distinguish lists in case you
63
# use the same list several times in a given area.
64
layout.template_list("MATERIAL_UL_matslots_example", "compact", obj, "material_slots",
65
obj, "active_material_index", type='COMPACT')
69
bpy.utils.register_class(MATERIAL_UL_matslots_example)
70
bpy.utils.register_class(UIListPanelExample)
74
bpy.utils.unregister_class(MATERIAL_UL_matslots_example)
75
bpy.utils.unregister_class(UIListPanelExample)
78
if __name__ == "__main__":
b'\\ No newline at end of file'
22
# Called once to draw filtering/reordering options.
23
def draw_filter(self, context, layout):
24
# Nothing much to say here, it's usual UI code...
27
# Called once to filter/reorder items.
28
def filter_items(self, context, data, propname):
29
# This function gets the collection property (as the usual tuple (data, propname)), and must return two lists:
30
# * The first one is for filtering, it must contain 32bit integers were self.bitflag_filter_item marks the
31
# matching item as filtered (i.e. to be shown), and 31 other bits are free for custom needs. Here we use the
32
# first one to mark VGROUP_EMPTY.
33
# * The second one is for reordering, it must return a list containing the new indices of the items (which
34
# gives us a mapping org_idx -> new_idx).
35
# Please note that the default UI_UL_list defines helper functions for common tasks (see its doc for more info).
36
# If you do not make filtering and/or ordering, return empty list(s) (this will be more efficient than
37
# returning full lists doing nothing!).
39
# Default return values.
43
# Do filtering/reordering here...
45
return flt_flags, flt_neworder