0

I would like to add an addon preference setting to the user preferences for my addon. I would like to have a list there where you can add and remove things (at first only Strings).

This sets it up:

class AddonPref(bpy.types.AddonPreferences):
    bl_idname = __package__
FileName : StringProperty()

presets_List = ["hi", "bye"]

addRemove : EnumProperty(
    items = [('one', 'add', ''),
            ('two', 'remove', '')
            ])

def draw(self, context):
    layout = self.layout

    box = layout.box()
    split = box.split()
    col = split.column(align = True)

    col.prop(self, "FileName")
    row = col.row()
    row.prop(self, "addRemove", expand = True)
    col.operator("presets.addremove")

    for x in self.presets_List:
        col.label(text=x)

And now I would like to edit the list with an operator (I already have got the add/Remove Enum and the FileName String properties. Only thing missing: the operator

class PresetsADDREMOVE(bpy.types.Operator):
    bl_idname = "presets.addremove"
    bl_label = "resets func"
    bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
    preferences = context.preferences
    addon_prefs = preferences.addons[__package__].preferences

    presets = addon_prefs.presets_List

    if addon_prefs.addRemove == 'one':
        presets.append(addon_prefs.FileName)
    else:
        presets.remove(addon_prefs.FileName)



    return {'FINISHED'}

This operator does work correctly.Though, I have got one big problem. The list is not the same in other blend files. It does not behave as an addon preference "property".

So what would I have to do to fix this problem? Thanks for the help! :D

BlueEvil
  • 75
  • 7
  • 2
    Unfortunately this kind of lists won't be kept when you save or when you quit and reload your file (or exist on different files at the same time). You can use a Collection Property with a PropertyGroup. It's also easy to add or remove elements with a CollectionProperty. See https://blender.stackexchange.com/questions/16511/how-can-i-store-and-retrieve-a-custom-list-in-a-blend-file – Gorgious Mar 04 '21 at 15:23
  • 2
    another option here would be to make the presets list a string property with comma separated values. – batFINGER Mar 04 '21 at 16:03

0 Answers0