I would like to have a list in the preferences of my Add-On where the content of the list is read from a file. (And therefore the length of the list is not known.) In this list I would like to have a check mark icon for each list item and only one should be selected. Both the list as well as the selected item of the list should be saved with the Add-On's preferences.
Normally such a list would be done with a PropertyGroup and a UIList. But I don't know how to do it in the Preferences as it is a class of it's own. Can someone help me to show how to achieve this?
PropertyGroup for storing the data from file:
class Library_List(bpy.types.PropertyGroup):
lib_name: bpy.props.StringProperty(name ='lib_name')
lib_last_mod: bpy.props.IntProperty(name ='lib_last_mod')
Class for reading the file:
class ReScan_Root_Library(bpy.types.Operator):
bl_idname = "panel.rescan_root_library"
bl_label = "Rescan Library"
def execute(self, context):
...
LibList = open(LibList_path, "r")
for x in LibList:
if not re.match(r'#', x) and not re.match(r'\n', x):
in_temp = x.split()
Lib_In = bpy.context.scene.lib_list.add()
Lib_In.lib_name = in_temp[0]
Lib_In.lib_last_mod = int(in_temp[1])
return {'FINISHED'}
I defined a UIList:
class CUSTOM_UL_items(UIList):
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index):
split = layout.split(factor=0.3)
split.label(text=item.lib_name)
split.label(text=str(item.lib_last_mod))
def invoke(self, context, event):
pass
And trying to add it to the preferences interface:
scn = bpy.context.preferences.addons['My_AddOn'].preferences
row.template_list("CUSTOM_UL_items", "", scn, "lib_list", scn, "", rows=2)
I try to define lib_list in the init as:
bpy.types.Preferences.lib_list = bpy.props.CollectionProperty(type=Library_List)
but this is obviously wrong as I get the message:
ui_template_list_data_retrieve: Property not found: PREF_PT_Preferences.lib_list
How do I define lib_list? If I define it as bpy.types.scene.lib_list I also get an error message. I have CollectionProperties in my scene and they work fine. I just don't understand the way the Preferences are working and also never worked with UiLists.