1
$\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$
  • $\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 Jan 28 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$ – SnoopyBrown Jan 28 at 14:11
3
$\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.

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)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.