I can access objects belonging to a collection like so:
objects_in_collection = bpy.data.collections["My_Collection"].objects
How can I access the collection a specific object belongs to?
I can access objects belonging to a collection like so:
objects_in_collection = bpy.data.collections["My_Collection"].objects
How can I access the collection a specific object belongs to?
Object.users_collection property returns a tuple containing all linked collections:
>>> obj = bpy.context.object
>>> obj.users_collection
(bpy.data.collections['Collection'],)
Select this object by name and it should take you to the right collection. Selection by name is described in this section.