i need to initialize a CollectionProperty with a fixed number of items. This property is used to store slot data, kind of like texture slots (There is 18 slots avaibles, but even if they are empty, they exist in the slot collection). Not sure if i am very clear so here is a bit of code

class MyPanel(bpy.types.Panel):
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "world"
    bl_label = "my panel"
    bl_options = {'DEFAULT_CLOSED'}

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

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

        ob = context.active_object

        # tried to initialize the collection here but i get the error "Writing to ID classes in this context is not allowed"
        """if(len(ob.test.myslots) < 4):
            for i in range(len(ob.test.myslots), 4):
    # MyUIList inherit from bpy.types.UIList
        layout.template_list("MyUIList", "", ob.test, "myslots", ob.test, "myslots_active", rows=4, maxrows=4)

    def poll(cls, context):
        return (context.active_object is not None and context.active_object.data is not None and isinstance(context.active_object.data, bpy.types.Mesh))

class MySlot(bpy.types.PropertyGroup):
    slotName = bpy.props.StringProperty()

class MyProperty(bpy.types.PropertyGroup):
    myslots = bpy.props.CollectionProperty(type=MySlot)
    myslots_active = bpy.props.IntProperty()

bpy.types.Object.test = bpy.props.PointerProperty(type=MyProperty)

How do I inititalize this list so...?

  • $\begingroup$ Can you explain more about how you would want the list initialized? initialized as empty? as entirely filled? $\endgroup$
    – ideasman42
    Apr 10, 2014 at 0:33
  • $\begingroup$ doesn't matter... $\endgroup$
    – user3081
    Apr 10, 2014 at 0:43

1 Answer 1


You can't, you need to have an explicit operation to initialize the list. (a button for example), or when the script executes.

At least you can't do while drawing.

  • $\begingroup$ It is possible using a hack: a scene update handler that removes itself and adds the required items, although there can be issues if you load another .blend file. Another workaround is to use an EnumProperty with items-callback and handle the CollectionProperty there too. $\endgroup$
    – CodeManX
    Apr 18, 2014 at 22:52
  • $\begingroup$ @CodeManX EnumProperty with items-callback doesn't work. It gives error doesn't allowed to write data in this context. $\endgroup$
    – Reigen
    Jul 14, 2021 at 13:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .