7
$\begingroup$

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?

$\endgroup$

2 Answers 2

15
$\begingroup$

Object.users_collection property returns a tuple containing all linked collections:

>>> obj = bpy.context.object
>>> obj.users_collection
(bpy.data.collections['Collection'],)
$\endgroup$
4
  • 2
    $\begingroup$ Many thanks! I also needed to get the name of the collection and was able to do so like this: ob.users_collection[0].name $\endgroup$
    – coCoKNIght
    Mar 21, 2019 at 14:59
  • 1
    $\begingroup$ @brockmann :: Is the result always by order? e.g. [0] = direct parent? The official document in your link lacks detail. $\endgroup$ Jul 18, 2020 at 4:10
  • 2
    $\begingroup$ Not sure, just test it. If not the case, you can sort it: blender.stackexchange.com/questions/146685/… @cppBeginner $\endgroup$
    – brockmann
    Jul 18, 2020 at 18:16
  • $\begingroup$ This doesn't seem to work in drivers. $\endgroup$
    – Will Chen
    Apr 18, 2021 at 22:26
0
$\begingroup$

Select this object by name and it should take you to the right collection. Selection by name is described in this section.

$\endgroup$
1

You must log in to answer this question.

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