How do I change the active collection?

I have two collections and want to change the active collections and do some operations on that collection, then change to the other collection.

Hi, can you please post a screenshot of your ui.


This is how my outliner looks. I want to do operations like add objects and stuff in the ‘Collection’ collection but it just ends up doing those in the ‘Scene Collection’ collection

The scene collection is where the collections are you just add to it.

You don’t need to change the active collection for moving an object into the collection…

What you need to do is to unlink the object from the ‘Scene Collection’ and link it to the child ‘Collection’. Or use the bpy.ops.object.move_to_collection operator.

As an example if I add a cube with the scene collection selected it adds the cube to the scene collection. I want to be able to select the ‘Collection’ collection and then add the cube in that. The issue with linking is I need to know where it is adding the cube (‘Collection’ collection or the scene collection). Maybe the move_to_collection might work. Let me try

If you select the collection then add object it will go into the selected collection.
you can also drag and drop in the outliner.

Hehe. I know that. I did that up till now but I want to be able to directly change it without manually selecting the collection. There might be a case with many collections so I just need to select the object visually.

1 Like

Yes in that case selecting the object and move to collection will be the way to go.
I also just noticed you have it in Python support not my filed sorry.

This doesn’t work for my case as I’ll need to know where the mesh is being added as a prerequisite right? Or else I’ll have to loop through each collection find it then link it. Which seems way more steps than just changing the active collection right? Maybe if there is a way to make the selected objects collection be the active collection?

The object datablock is allways stored at bpy.data.objects, so you can find it there.
Then you can retrieve to which collections the object is linked with:

import bpy

obj = bpy.data.objects["Cube"] # or the object you got from the 'add object' op
collections = obj.users_collection

#remove the object from all collection:
for coll in collections:
   coll.objects.unlink(obj)

The object will still remain in the bpy.data.objects collection, but it won’t belong to any scene collection (and its children).

It’s even possible to add the object only to the bpy.data.objects without adding the object to a collection. The UI operator for adding objects does this with the active collection automatically, but if you use the API directly, you can skip that.

1 Like

Okay so the way I’m doing it uses the users_collection mentioned by @Secrop and this stack exchange page which shows how to change active collection

I get the active objects collection name with this

sel_obj = bpy.context.active_object
curr_col_name = sel_obj.users_collection[0].name

And then I change active collection to that collection using

act_col = bpy.context.view_layer.layer_collection.children[curr_col_name]
bpy.context.view_layer.active_layer_collection = act_col

Okay to add to this, this may not work for collections with collections with nested collections as show here

image

image

To fix this you can refer to this post

You can use something like this

sel_obj = bpy.context.active_object
user_col = sel_obj.users_collection[0]

def rec_collec_finder(layCol, colName):
found = None
if (layCol.name == colName):
return layCol
for layer in layCol.children:
found = rec_collec_finder(layer, colName)
if found:
return found

lay_col = bpy.context.view_layer.layer_collection
bpy.context.view_layer.active_layer_collection = rec_collec_finder(lay_col, user_col.name)