How to register a function/property to the base of a collection property rather than to each of it's items

I have a collection property defined as:

bpy.types.Scene.my_list = CollectionProperty(type=MyListClass)

and I know that I can add a function to every item in that list by defining it inside MyListClass, but instead, I want to define a function that can be accessed from the base of the property (sorry if that’s incorrect, I’m not sure what the correct terminology is).

AKA, I want to be able to access my function from:

bpy.context.scene.my_list.my_function()

rather than:

bpy.context.my_list["my_item"].my_function()

Is this possible? The same question also applies to custom properties.

You can add a PointerProperty to the scenes which will contain both methods and list, e.g.:

class MyList(PropertyGroup):
    pass

class Container(PropertyGroup):
    list : CollectionProperty(type = MyList)

    def my_function(self):
        pass

bpy.utils.register_class(MyList)
bpy.utils.register_class(Container)
bpy.types.Scene.container = PointerProperty(type=Container)

Now you can do scene.container.my_function() and scene.container.list['item']. It is generally a good idea to have a large PointerProperty container in order not to pollute built-in blender properties of scenes, objects, etc.

Note the order of the registration: as Container uses MyList class the later should be registered first.

Would advise not using list as attribute name since it’s a keyword in python

Yes, sure, but it’s just a mockup. Although, to be honest, name list in current situation will not compete with the python’s keyword

Ah great, thanks for the answer, that’s really useful to know!

1 Like