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
1 Answer
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.