ui_list_simple.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import bpy
  2. class MATERIAL_UL_matslots_example(bpy.types.UIList):
  3. # The draw_item function is called for each item of the collection that is visible in the list.
  4. # data is the RNA object containing the collection,
  5. # item is the current drawn item of the collection,
  6. # icon is the "computed" icon for the item (as an integer, because some objects like materials or textures
  7. # have custom icons ID, which are not available as enum items).
  8. # active_data is the RNA object containing the active property for the collection (i.e. integer pointing to the
  9. # active item of the collection).
  10. # active_propname is the name of the active property (use 'getattr(active_data, active_propname)').
  11. # index is index of the current item in the collection.
  12. # flt_flag is the result of the filtering process for this item.
  13. # Note: as index and flt_flag are optional arguments, you do not have to use/declare them here if you don't
  14. # need them.
  15. def draw_item(self, context, layout, data, item, icon, active_data, active_propname):
  16. ob = data
  17. slot = item
  18. ma = slot.material
  19. # draw_item must handle the three layout types... Usually 'DEFAULT' and 'COMPACT' can share the same code.
  20. if self.layout_type in {'DEFAULT', 'COMPACT'}:
  21. # You should always start your row layout by a label (icon + text), or a non-embossed text field,
  22. # this will also make the row easily selectable in the list! The later also enables ctrl-click rename.
  23. # We use icon_value of label, as our given icon is an integer value, not an enum ID.
  24. # Note "data" names should never be translated!
  25. if ma:
  26. layout.prop(ma, "name", text="", emboss=False, icon_value=icon)
  27. else:
  28. layout.label(text="", translate=False, icon_value=icon)
  29. # 'GRID' layout type should be as compact as possible (typically a single icon!).
  30. elif self.layout_type in {'GRID'}:
  31. layout.alignment = 'CENTER'
  32. layout.label(text="", icon_value=icon)
  33. # And now we can use this list everywhere in Blender. Here is a small example panel.
  34. class UIListPanelExample(bpy.types.Panel):
  35. """Creates a Panel in the Object properties window"""
  36. bl_label = "UIList Panel"
  37. bl_idname = "OBJECT_PT_ui_list_example"
  38. bl_space_type = 'PROPERTIES'
  39. bl_region_type = 'WINDOW'
  40. bl_context = "object"
  41. def draw(self, context):
  42. layout = self.layout
  43. obj = context.object
  44. # template_list now takes two new args.
  45. # The first one is the identifier of the registered UIList to use (if you want only the default list,
  46. # with no custom draw code, use "UI_UL_list").
  47. layout.template_list("MATERIAL_UL_matslots_example", "", obj, "material_slots", obj, "active_material_index")
  48. # The second one can usually be left as an empty string.
  49. # It's an additional ID used to distinguish lists in case you
  50. # use the same list several times in a given area.
  51. layout.template_list("MATERIAL_UL_matslots_example", "compact", obj, "material_slots",
  52. obj, "active_material_index", type='COMPACT')
  53. def register():
  54. bpy.utils.register_class(MATERIAL_UL_matslots_example)
  55. bpy.utils.register_class(UIListPanelExample)
  56. def unregister():
  57. bpy.utils.unregister_class(MATERIAL_UL_matslots_example)
  58. bpy.utils.unregister_class(UIListPanelExample)
  59. if __name__ == "__main__":
  60. register()