Register collectionproperty to object instead scene

Hi, I just play around with python and now I want to register a list to Objects instead of Scene.

When I do this:

bpy.types.Scene.my_list = bpy.props.CollectionProperty(type = ListItem)

I can draw the list with this code

        scene = context.scene
        row = layout.row()
        row.template_list("MY_UL_List", "The_List", scene,
                          "my_list", scene, "list_index")

This works fine.

As I understand the list is now registered to the sole scene.
However, I want it to be the list of a single object, so every object can have its own scene.

So I thought it would go like this:

bpy.types.Object.my_list = bpy.props.CollectionProperty(type = ListItem)

and

        scene = context.active_object
        row = layout.row()
        row.template_list("MY_UL_List", "The_List", scene,
                          "my_list", scene, "list_index")

But now this list isn’t showing in my panel.

Whats my misconception here?

Yeh, never mind… it was so easy.

What did you do to fix this problem? This information will be helpful to anyone who comes across this thread in the future :slight_smile:

Ok,

so this line was correct, its how you do it:

bpy.types.Object.my_list = bpy.props.CollectionProperty(type = ListItem)

However, I changed the second codeblock to this.
Instead of scene = context.active_object I now use

obj = context.object

And also didnt use the scene as variable name.

        obj = context.object
        row = layout.row()
        row.template_list("MY_UL_List", "The_List", obj,
                          "my_list", obj, "list_index")

So I will find the list in context.object and not in context.active_object …

I hope I understood this right, however… it works!

1 Like

AFAIK context.object and context.active_object point to the same object (or to None if there is none) under the hood so I think your error was located elsewhere. And according to the devs (can’t find the source) the latter is the preferred syntax. Food for thought.

2 Likes

Hmm. Indeed… Mind Food… What else have I changed. I will think about it…
Changed so much recently