0
$\begingroup$

How can i get the name of the collection where an object belongs and store the name of that collection into a variable so i can use it later to unlink the object from that collection

$\endgroup$
1
  • $\begingroup$ Objects don't "belong" to a single collection, they may be linked in several collections simultaneously. They may even be in different collections in do scenes at the same time $\endgroup$ Commented Oct 8, 2022 at 17:27

1 Answer 1

1
$\begingroup$

Use bpy.types.Object.users_collection attribute to retrieve the collections it is linked in. It's a sequence of collections which can contain 0 or more items, since an object can be part of 0 or more collections.

I'll assume your object is part of at least one collection since it's the general case.

obj = bpy.data.objects["Cube"]  # Replace with your object name

col = obj.users_collection[0]  # This stores a reference to the (first) collection
col_name = col.name

If your object is linked in several collections, you'll have to add custom logic since AFAIK the order in which they are stored in users_collection is not deterministic.

$\endgroup$

You must log in to answer this question.

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