In Blender, any object can be removed from a collection, as well as from all collections to which this object has been added, using the Python API.
To remove the object from the collection with the desired name, we need to use the unlink() method on the objects list in this collection.
For example, let’s remove the currently active object from the collection named “Collection 2”:
1 2 |
obj = bpy.context.object bpy.data.collections['Collection 2'].objects.unlink(obj) |
To remove the active object from all collections, we need to loop through each collection and remove the object:
1 2 3 4 5 |
obj = bpy.context.object for col in bpy.data.collections: if bpy.context.object in col.objects[:]: col.objects.unlink(obj) |
Please note that the unlink() command does not check for the existence of an object in the collection, so to avoid an error:
RuntimeError: Error: Object ‘Suzanne’ not in collection ‘Collection 2’
Before deleting, we need to check if the object exists in the collection.