8
$\begingroup$

The Python refernce says: bpy.ops.outliner.collection_delete(hierarchy=True) is the rightway to remove the collection and its contents, which are generated in every new document in Blender v2.80 (Beta). But in Scripting Mode it says: AttributeError: 'Screen' object has no attribute 'scene'

How do I clear the initial scene and jump to the next point in my script?

$\endgroup$
2
  • $\begingroup$ Can you provide a link to the python reference re "right way to remove collections" and also provide the part of script that produces that error message. $\endgroup$
    – batFINGER
    Commented Jan 28, 2019 at 13:54
  • $\begingroup$ There is no link, but if right click on the "Collection" item and hover above "Delete hierarchy" this Python command is shown in the pop up window. bpy.ops.outliner.collection_delete(hierarchy=True) is the part, where the error occurs. But the provided solution to unlink the contents works perfectly. $\endgroup$
    – holthaum
    Commented Jan 28, 2019 at 14:11

2 Answers 2

15
$\begingroup$

Unlink all children from the scene collection

Not a fan of using outliner operators in scripts. Unlinking or removing the collections from a scene will also have the same effect of clearing a scene.

Script to unlink all the child collections from the scene's master collection.

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

for c in scene.collection.children:
    scene.collection.children.unlink(c)

Theoretically could remove the child collections using bpy.data.collections.remove(c) above instead of unlink, however I found this has the tendency to seg fault my build of 2.8._ (See EDIT below)

Running this after removes the orphan collection

import bpy

for c in bpy.data.collections:
    if not c.users:
        bpy.data.collections.remove(c)

Similarly could remove the collections objects from blender file with

for o in c.objects:
    bpy.data.objects.remove(o)

EDIT. Update: removing collections from bpy data

import bpy

name = "Collection 1"
remove_collection_objects = True

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$
5
  • $\begingroup$ Is there a way once I remove the scene collections to also flush everything connected to them. Because I end up with orphan data, unused scenes, layers, objects, etc. which increase the memory usage. $\endgroup$
    – ttsesm
    Commented Sep 28, 2020 at 11:58
  • $\begingroup$ Take out the users test. It's there as a failsafe since the collection is a user of the object. If the object is "used" as say a modifier target or somesuch then AFAIK this is another user and the object is not removed. $\endgroup$
    – batFINGER
    Commented Sep 28, 2020 at 12:02
  • $\begingroup$ I have removed the users test but I still get orphan data for some reason. $\endgroup$
    – ttsesm
    Commented Sep 28, 2020 at 12:12
  • $\begingroup$ I get the error: "AttributeError: 'Context' object has no attribute 'scenes'" .. researching.. the first times I tried this code it worked.. $\endgroup$
    – juggler
    Commented Jan 13, 2021 at 6:28
  • $\begingroup$ The closest above to getting that error is context.scene have you accidentally used context.scenes. $\endgroup$
    – batFINGER
    Commented Jan 13, 2021 at 6:38
0
$\begingroup$

The accepted answer works but doesn't delete the sub collections from the Orphan Data,
Just incase someone wants a recursive solution:

import bpy

def del_collection(coll):
    for c in coll.children:
        del_collection(c)
    bpy.data.collections.remove(coll,do_unlink=True)

del_collection(bpy.data.collections["Collection_name_here"])
$\endgroup$

You must log in to answer this question.

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