1
$\begingroup$

I have been referred to a link which has already answered this, however the script in the linked page removes all of my collections. My collection is called "Animation". I am not currently working in this collection. So I am wanting to select this collection and delete it. The script i was referred to deletes everything. Thanks for the help...

import bpy
context = bpy.context
scene = context.scene

for c in scene.collection.children:
    scene.collection.children.unlink(c)
$\endgroup$
1
  • $\begingroup$ Please don't re-ask the same question multiple times, edit your original post and explain why the linked dupe doesn't solve your problem to re-open it. I think your other one was closed instantly because of its poor quality, a one-liner without showing any effort. Please take your time and read the help files, in this case: blender.stackexchange.com/help/closed-questions so we don't have to explain to you how this site works again and again. Thanks $\endgroup$
    – brockmann
    Commented Apr 10, 2020 at 14:56

2 Answers 2

4
$\begingroup$

Use Context

So I am wanting to select this collection and delete it.

Further to Blender 2.80: Delete Collection or clear the intial scene in scripting mode

As is the case with most of blender making active in the UI gives it context.

The context collection can be the scene collection so don't want to delete that.

import bpy
from bpy import context

remove_collection_objects = True

coll = context.collection
scene = context.scene

if coll:
    if remove_collection_objects:
        obs = [o for o in coll.objects if o.users == 1]
        while obs:
            bpy.data.objects.remove(obs.pop())

    if coll is not scene.collection:
        bpy.data.collections.remove(coll)

Remove named collection and its unique objects

Script below will remove the default scene collection "Collection 1" and all objects within it.

Change name to collection to remove and set the remove_collection_objects toggle.

import bpy
#from bpy import context


name = "Collection 1"
remove_collection_objects = True

#coll = context.collection # 
coll = bpy.data.collections.get(name)

if coll:
    if remove_collection_objects:
        obs = [o for o in coll.objects if o.users == 1]
        while obs:
            bpy.data.objects.remove(obs.pop())

    bpy.data.collections.remove(coll)
$\endgroup$
2
$\begingroup$

You can access the collection directly with :

bpy.data.collections['Animation']

So you just have to :

import bpy

col_name = 'Animation'

try:
    bpy.data.collections.remove(bpy.data.collections[col_name])
except KeyError:  # Prevent failure if the collection doesn't exist
    print(f"The collection {col_name} doesn't exist")

Source

$\endgroup$
3
  • $\begingroup$ Agreed. I added a solution with a try/except for the sake of variety $\endgroup$
    – Gorgious
    Commented Apr 10, 2020 at 9:49
  • $\begingroup$ AKA known as very pythonic cant UV again.. See docs.blender.org/api/current/… Have found attachment to using foo = collection["Bar"] one of the commonest reasons for confusing scripts given the nature of blenders naming system. Without a good remove regime, the object you think is "Cube" or modifier "Array" or .., may not be if another already has same name. , IMO good habit to not use addressing by name (Apologies for being a stickler,) $\endgroup$
    – batFINGER
    Commented Apr 10, 2020 at 10:37
  • $\begingroup$ @batFINGER Again, agreed, and as a matter of fact I prefer your proposal, I merely wanted to offer variety without too much complexity. However I think this kind of code optimisation is a bit out of context in this question since it is supposed to be a one-time operation. Cheers $\endgroup$
    – Gorgious
    Commented Apr 10, 2020 at 11:18

You must log in to answer this question.

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