GUI: template_lists

I’m not getting my head around lists!

How do I initialise a template_list from an array in the given ‘init_list()’ function?


import bpy

class myList(bpy.types.PropertyGroup):
    pass

bpy.utils.register_class(myList)

## create CollectionProperty and link it to the property class
bpy.types.Object.theList = bpy.props.CollectionProperty(type=myList)
bpy.types.Object.theList_index = bpy.props.IntProperty(min=-1, default=-1)

def init_list(obj):
    l=["one","two","three"]
    #   HOW TO INITIALISE THE LIST FROM l ?
    #obj.theList.add()
    return

class OBJECT_PT_list(bpy.types.Panel):
    bl_label = "List Tests"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'

    def draw(self, context):
        layout=self.layout
        obj=context.object
        
        init_list(obj)
        
        layout.template_list(obj, "theList", obj, "theList_index",3)

bpy.utils.register_class(OBJECT_PT_list)

Cheers!

Any solutions?

Further…

This works:


import bpy

class myList(bpy.types.PropertyGroup):
    name = bpy.props.StringProperty(name="Test Prop", default="Unknown")
    value = bpy.props.IntProperty(name="Test Prop", default=22)

bpy.utils.register_class(myList)

bpy.types.Scene.theList = bpy.props.CollectionProperty(type=myList)
bpy.types.Scene.theList_index = bpy.props.IntProperty(min=-1, default=-1)

class OBJECT_PT_list(bpy.types.Panel):
    bl_label = "List Tests"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'

    def draw(self, context):
        layout=self.layout
        obj=context.object
        
my_item = bpy.context.scene.theList.add()
my_item.name = "Spam"
my_item.value = 1000

my_item = bpy.context.scene.theList.add()
my_item.name = "Eggs"
my_item.value = 30

for my_item in bpy.context.scene.theList:
    print(my_item.name, my_item.value)

bpy.utils.register_class(OBJECT_PT_list)

not that it does much…

However just moving it gives errors!


import bpy

class myList(bpy.types.PropertyGroup):
    name = bpy.props.StringProperty(name="Test Prop", default="Unknown")
    value = bpy.props.IntProperty(name="Test Prop", default=22)

bpy.utils.register_class(myList)

bpy.types.Scene.theList = bpy.props.CollectionProperty(type=myList)
bpy.types.Scene.theList_index = bpy.props.IntProperty(min=-1, default=-1)


def init_list(obj):
    l=["one","two","three"]
    
    for e in l:
        my_item = bpy.context.scene.theList.add()
        my_item.name = e
        my_item.value = 1000
    
    for my_item in bpy.context.scene.theList:
        print(my_item.name, my_item.value)
    
    return

class OBJECT_PT_list(bpy.types.Panel):
    bl_label = "List Tests"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'

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

        obj=context.object
        
        #o = bpy.context.scene
        init_list()
        #layout.template_list(o, "theList", o, "theList_index",3)

bpy.utils.register_class(OBJECT_PT_list)

No matter where i’ve tried to put it I still get the same error:


AttributeError: Writing to ID classes in this context is not allowed

Help… :eek:

Ok, it’s still crude but…


import bpy

class myList(bpy.types.PropertyGroup):
    name = bpy.props.StringProperty(name="Test Prop", default="Unknown")
    value = bpy.props.IntProperty(name="Test Prop", default=22)


bpy.utils.register_class(myList)

bpy.types.Scene.theList = bpy.props.CollectionProperty(type=myList)
bpy.types.Scene.theList_index = bpy.props.IntProperty(min=-1, default=-1)


def init_list():
    l=["one","two","three"]
    for e in l:
        my_item = bpy.context.scene.theList.add()
        my_item.name = e
        my_item.value = 1000
    
    for my_item in bpy.context.scene.theList:
        print(my_item.name, my_item.value)
    
    return

class MY_LIST_OT_add(bpy.types.Operator):
        bl_idname      = 'my_list.add'
        bl_label       = "Add list item"
        bl_description = "Add list item"
        def invoke(self, context, event):
                my_item = bpy.context.scene.theList.add()
                my_item.name = "NEW"
                my_item.value = 1000
                
                return{'FINISHED'}

class MY_LIST_OT_del(bpy.types.Operator):
        bl_idname      = 'my_list.remove'
        bl_label       = "Remove list item"
        bl_description = "Remove list item"
        def invoke(self, context, event):
                sce= context.scene
                my_list= sce.theList
                if sce.theList_index >= 0:
                   my_list.remove(sce.theList_index)
                   sce.theList_index-= 1
                return{'FINISHED'}

class MY_LIST_OT_clear(bpy.types.Operator):
        bl_idname      = 'my_list.clear'
        bl_label       = "Empty list"
        bl_description = "Empty list"
        def invoke(self, context, event):
                sce= context.scene
                my_list= sce.theList
                print("Clear")
                n=len(my_list)
                for i in range(0,n+1):
                    my_list.remove(n-i)
                return{'FINISHED'}

class MY_LIST_OT_dump(bpy.types.Operator):
        bl_idname      = 'my_list.dump'
        bl_label       = "Dump list items"
        bl_description = "Dump list items"
        def invoke(self, context, event):
                sce= context.scene
                my_list= sce.theList
                print("Dumping List (",len(my_list),")")
                for e in my_list:
                    print(e.name)
                return{'FINISHED'}
            
class OBJECT_PT_list(bpy.types.Panel):
    bl_label = "List Tests"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'

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

        o = bpy.context.scene
        layout.template_list(o, "theList", o, "theList_index",3)
        
        layout.operator('my_list.add', text="", icon="ZOOMIN")
        layout.operator('my_list.remove', text="", icon="ZOOMOUT")
        layout.operator('my_list.dump', text="Dump")
        layout.operator('my_list.clear', text="Clear")


init_list()

bpy.utils.register_class(MY_LIST_OT_add)
bpy.utils.register_class(MY_LIST_OT_del)
bpy.utils.register_class(MY_LIST_OT_dump)
bpy.utils.register_class(MY_LIST_OT_clear)

bpy.utils.register_class(OBJECT_PT_list)

Cheers Ricky that’ll be useful too!

P.S. I did reply yesterday but your inbox was full…

EDIT: And still is full!

looks like they reduce the qty of messages you can get
i’ll clear it now
thanks for reminding me

happy 2.5

Hi I was able to init some values but it does not work like it should.




# ------ ------
bl_info = {
    'name': 'test',
    'author': '',
    'version': (0, 0, 0),
    'blender': (2, 5, 7),
    'api': 36812,
    'location': '',
    'description': '',
    'warning': '',
    'wiki_url': '',
    'tracker_url': '',
    'category': 'Mesh' }



# ------ ------
import bpy
from bpy.props import *



# ------ ------
class p_group_0_(bpy.types.PropertyGroup):

    name = bpy.props.StringProperty()



# ------ ------
class my_list(bpy.types.PropertyGroup):

    list_ = CollectionProperty( type = p_group_0_ )
    test_ = IntProperty( min = -1,
                         default = 0)



# ------ init ------
def init():

    bpy.context.scene.my_custom_props.test_ = 0

    
    my_item = bpy.context.scene.my_custom_props.list_.add()
    my_item.name = "Eggs"
    
    
    
    for my_item in bpy.context.scene.my_custom_props.list_:
    
    
        print(my_item.name)






# ------ panel 0 ------
class p_0_(bpy.types.Panel):

    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'

    bl_idname = 'p_0_id'      # <--- id
    
    bl_label = ''
    bl_context = 'mesh_edit'

    def draw(self, context):
        my_props = context.scene.my_custom_props
        
        layout = self.layout
        layout.template_list(my_props, 'list_', my_props, 'test_', 3 )



# ------ classes to register ------
class_list = [ p_0_,
               p_group_0_,
               my_list ]



# ------ register ------
def register():
    for c in class_list:
        bpy.utils.register_class(c)

    bpy.types.Scene.my_custom_props = PointerProperty( type = my_list )
    
    init()      # <----- init


# ------ unregister ------
def unregister():
    for c in class_list:
        bpy.utils.unregister_class(c)

    del bpy.context.scene['my_custom_props']

# ------ ------
if __name__ == "__main__":
    register()