1
$\begingroup$

I want to clear a scene's master collection in order to ensure I am working with an empty collection before I run my script which will build the scene for me.

I search for answers on how to achieve that and they suggest selecting all desired objects and triggering the deletion operation with that selection. I attempted to adapt that code, but found that the object of type FONT actually lacks the select property/attribute and as a result I can't include my Texts in the selection to clear them.

# Clear all objects in the master collection to reset to a blank scene
for object in bpy.data.scenes[0].collection.objects:
    print(object.type)
    if hasattr(object, 'select'):
        object.select = True

bpy.ops.object.delete()

Is there an alternative way of selecting Texts? Or even better, can I clear the collection completely without "macroing" it by creating a selection and calling the delete operation?

$\endgroup$

1 Answer 1

1
$\begingroup$

Upon inspecting what sort of options there are using the Autocomplete function, I found unlink:

# Clear all objects in the master collection to reset to a blank scene
for object in bpy.data.scenes[0].collection.objects:
    bpy.data.scenes[0].collection.objects.unlink(object)

This clears all the objects from the collection, Text or not.

$\endgroup$

You must log in to answer this question.

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