3
$\begingroup$

I'm trying to replicate the functionality of right-clicking on a scene collection, and selecting 'DELETE', which removes the collection but does not remove any of the objects.

However, all functions and answers I found (unlink, collection.remove, etc) always remove all the objects that are part of the collection, which I'm trying to avoid.

$\endgroup$
0

1 Answer 1

2
$\begingroup$

You need to relink the objects to a new collection. Here's an example that removes all the objects from a collection called "monkeys" and moves them to the default collection while removing "monkeys". Change the names to protect your innocent:

import bpy

collection_to_remove = bpy.data.collections.get('monkeys')

objects_from_collection = [object for object in collection_to_remove.objects]
bpy.data.collections.remove(collection_to_remove)

collection_to_link_to = bpy.data.collections.get('Collection')
for object in objects_from_collection:
    collection_to_link_to.objects.link(object)
$\endgroup$

You must log in to answer this question.

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