How to make a script where every objects gets it own collection?

how to make a script where every objects gets it own collection?
(I have a lot of objects and I want each object to be in its own collection)

I have a similar code fragment in my add-on, that bridges blender to the natron compositor. Before blender had light groups, it moved each light into its own collection, that could then be driven on a view layer. Here’s the fragment moving each light into its own collection:

It’s not only needed to create the new collection, but also to remove the object from the collections it’s currently inside. Hope that helps.

1 Like

Before

After

Code Snippet

import bpy

for o in bpy.data.objects:
    if o.type == "MESH":
        new_c = bpy.data.collections.new(o.name)
        bpy.context.scene.collection.children.link(new_c)
        for parent_cols in o.users_collection:
            parent_cols.objects.unlink(o)
        new_c.objects.link(o)
2 Likes

Thank you very much!

thank you very much!