0
$\begingroup$

I don't know why but I've tried to get the property group to reference through the addon preferences for the list and been at this for four days and cant get anything to work. I've used this link https://blender.stackexchange.com/a/220639/78916 as a base reference for helping to solve the issue but end up going around in circles.

The code listed works fine but will not save any settings when exiting Blender and I'm guessing this is because the list doesn't reference through the Addon Preferences

The reference to the list index(and it's strings) needs to be used/accessed from another class but I can't even get any reference to the property group items through the addon preferences class? Given myself a headache over this one and I've done this for previous addons but never for a list and got myself code blined now and very frustrated.

Any input, advice or point in the right direction would be greatly appreciated but please don't point me to any UI Lists I dont think my eyes could take any more of those!

bl_info = {
    "name" : "Preferences UI list",
    "author" : "Fiddler",
    "description" : "",
    "blender" : (2, 80, 0),
    "version" : (0, 0, 1),
    "location" : "View3D",
    "warning" : "",
    "category" : "User Interface"
}
import os
import bpy

from bpy.props import *
from bpy.props import StringProperty, IntProperty, BoolProperty, CollectionProperty, PointerProperty
from bpy.types import PropertyGroup, Operator, Panel, AddonPreferences, UIList


class CUSTOM_PG_webfilepaths(PropertyGroup): 
   
    name: bpy.props.StringProperty( 
        name="Web Site URL", 
        description="Add A Web SIte URL", 
        default="Web SIte URL"
        ) 
    button_Name: bpy.props.StringProperty( 
        name="Button Name", 
        description="Add A Button Name", 
        default="Button Name"
        )
    display: bpy.props.BoolProperty()


class AddonPreferences(AddonPreferences):
    bl_idname = __name__

    context: EnumProperty(
        name = 'Context',
        description = 'Preferences UI list',
        items = [
            ('WEBSITES', 'Websites', ''),
            #('SCREEN', 'Screen Postion', ''),            
            ('FILEPATHS', 'File Paths', '')],
        default = 'WEBSITES') 

    def draw(self, context):
        layout = self.layout
        column = layout.column(align=True)
        row = column.row(align=True)
        row.prop(self, 'context', expand=True)
        box = column.box()
        getattr(self, self.context.lower())(context, box)

    def websites(self, context, layout):
        layout = self.layout 
        scene = context.scene 
        
        layout.label(text="Enter Your Web Site Favourites:")
        row = layout.row() 
        row.template_list("MY_UL_WSList", "The_List", scene, "my_list", scene, "list_index") 
        row = layout.row() 
        row.operator('my_list.new_item', text='NEW') 
        row.operator('my_list.delete_item', text='REMOVE') 
        row.operator('my_list.move_item', text='UP').direction = 'UP' 
        row.operator('my_list.move_item', text='DOWN').direction = 'DOWN' 
        if scene.list_index >= 0 and scene.my_list: 
            item = scene.my_list[scene.list_index] 
            row = layout.row() 
            row.prop(item, "name") 
            row.prop(item, "button_Name")
            
    #def screen(self, context, layout):
        #layout = self.layout
        #layout.label(text='HELLO WORLD')

    def filepaths(self, context, layout):
        layout = self.layout
        layout.label(text='HELLO WORLD')


class MY_UL_WSList(UIList): 
    """Demo UIList.""" 
    def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index): 
   
        #index = bpy.types.Scene.list_index 
        
        if self.layout_type in {'DEFAULT', 'COMPACT'}:
            split = layout.split(factor=0.1 )
            split.label(text="%d" % (index))
            split.label(text=item.name, icon = 'WORLD')
            split.label(text=item.button_Name, icon = 'VIEW_PAN')  
        
        elif self.layout_type in {'GRID'}: 
            layout.alignment = 'CENTER' 
            layout.label(text="", icon = '')


class LIST_OT_NewItem(Operator): 
    """Add a new item to the list.""" 
    bl_idname = "my_list.new_item" 
    bl_label = "Add a new item"

    def execute(self, context):
        context.scene.my_list.add()
        return{'FINISHED'}


class LIST_OT_DeleteItem(Operator): 
    """Delete the selected item from the list.""" 
    bl_idname = "my_list.delete_item" 
    bl_label = "Deletes an item"

    @classmethod 
    def poll(cls, context): 
        return context.scene.my_list 
    def execute(self, context): 
        my_list = context.scene.my_list 
        index = context.scene.list_index 
        my_list.remove(index) 
        context.scene.list_index = min(max(0, index - 1), len(my_list) - 1) 
        return{'FINISHED'} 


class LIST_OT_MoveItem(Operator): 
    """Move an item in the list.""" 
    bl_idname = "my_list.move_item" 
    bl_label = "Move an item in the list" 

    direction: bpy.props.EnumProperty(
        items=(('UP', 'Up', ""), 
        ('DOWN', 'Down', ""),))

    @classmethod 
    def poll(cls, context): 
        return context.scene.my_list 

    def move_index(self): 
        """ Move index of an item render queue while clamping it. """ 
        index = bpy.context.scene.list_index 
        list_length = len(bpy.context.scene.my_list) - 1 
        # (index starts at 0) 
        new_index = index + (-1 if self.direction == 'UP' else 1) 
        bpy.context.scene.list_index = max(0, min(new_index, list_length)) 

    def execute(self, context): 
        my_list = context.scene.my_list 
        index = context.scene.list_index 
        neighbor = index + (-1 if self.direction == 'UP' else 1) 
        my_list.move(neighbor, index) 
        self.move_index() 
        return{'FINISHED'}


def register():
    bpy.utils.register_class(CUSTOM_PG_webfilepaths) 
    bpy.utils.register_class(AddonPreferences)  
    bpy.utils.register_class(MY_UL_WSList) 
    bpy.utils.register_class(LIST_OT_NewItem) 
    bpy.utils.register_class(LIST_OT_DeleteItem) 
    bpy.utils.register_class(LIST_OT_MoveItem)
   
   
    bpy.types.Scene.my_list = CollectionProperty(type = CUSTOM_PG_webfilepaths) 
    bpy.types.Scene.list_index = IntProperty(name = "Index for my_list", default = 0)


def unregister(): 
    del bpy.types.Scene.my_list 
    del bpy.types.Scene.list_index


    bpy.utils.unregister_class(AddonPreferences) 
    bpy.utils.unregister_class(CUSTOM_PG_webfilepaths) 
    bpy.utils.unregister_class(MY_UL_WSList) 
    bpy.utils.unregister_class(LIST_OT_NewItem) 
    bpy.utils.unregister_class(LIST_OT_DeleteItem) 
    bpy.utils.unregister_class(LIST_OT_MoveItem)
$\endgroup$
3
  • $\begingroup$ Possibly same as issue with pointerproperties, btw which version? See if method employed in question blender.stackexchange.com/questions/157677/… re setting autosave. One workaround is to create "addon presets" capability.. $\endgroup$
    – batFINGER
    Jun 3 at 12:56
  • $\begingroup$ ... see point re collection is a scene property. Have you tried annotating the prefernces class with collection property? if so add that code instead. $\endgroup$
    – batFINGER
    Jun 3 at 13:07
  • $\begingroup$ I'm using Blender 3.0 via VS Code, but have tried on stable Blender releases with a zip file addon load and still the same problem, I'm wondering if a 'preferences' json file could be the route to go with this. $\endgroup$
    – Vin
    Jun 3 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.