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)