We can delete collections in Blender through the “collections” list in “bpy.data”.
To remove a collection we need to use the “remove” method from the collections list, specifying the collection to be removed in the parameter:
1 2 3 |
collection = bpy.data.collections.get('collection_to_del') bpy.data.collections.remove(collection) |
When we delete a collection in this way, all objects from the collection will be unlinked from the scene, but will not be deleted as objects and remain in the blend-file.
To completely remove a collection and all objects in it, we must first delete the objects from the “objects” list, and then delete the collection itself.
This code removes the collection and all nested objects:
1 2 3 4 5 6 7 8 |
import bpy collection = bpy.data.collections.get('collection_to_del') for obj in collection.objects: bpy.data.objects.remove(obj, do_unlink=True) bpy.data.collections.remove(collection) |
should also remove all orphan data after deleting
Yes, you can. When we set do_unlink=True, all data links are removed, so the object will be removed from orphan next save.
WHy is there no code for moving an collection?
I can seem to find any code for that, do you know ant perhaps
I think you can try to unlink and then link the collection to another collection you need.