import bpy
[docs]def clear_scene(scene=None, deep=False, remove_collections=False):
"""Unlink all the objects contained in the given scene,
clears scene.collection.
Parameters
----------
scene : bpy.types.Scene (default=None)
Scene to clear. If the argument is not provided or None
bpy.context.scene will be set to scene.
deep : bool (default=False)
Delete the data corresponding to the objects.
remove_collections : bool (default=False)
Remove the collections (and its children) from the scene.
Returns
-------
None
"""
if scene is None:
scene = bpy.context.scene
clear_collections(collections=[scene.collection], deep=deep, remove_collections=remove_collections)
[docs]def clear_collections(collections=None, deep=False, remove_collections=False):
"""Unlink all the objects contained in the given collection.
Parameters
----------
collections : list of bpy.types.Collection (default=None)
Collection(s) to clear. If the argument is not provided or None
bpy.context.scene will be set to scene.
deep : bool (default=False)
Delete the data corresponding to the objects.
remove_collections: bool (default=False)
Deletes the given collections and children.
If bpy.context.scene.collection was given it deletes all
children of this collection (bpy.context.scene.collection can't be removed).
Returns
-------
None
"""
if collections is None:
collections = [bpy.context.scene.collection]
for collection in collections:
clear_objects(objects=collection.all_objects, deep=deep)
if remove_collections:
if collection is bpy.data.scenes['Scene'].collection:
for c in bpy.data.collections:
bpy.data.collections.remove(c)
else:
if collection.children:
clear_collections(collections=collection.children, remove_collections=remove_collections)
bpy.data.collections.remove(collection)
[docs]def clear_objects(objects=None, do_unlink=True, deep=True):
"""Delete all given objects.
Parameters
----------
objects : iterable of bpy.type.Object (default=None)
Objects to be deleted. If the argument is not provided or None
all objects will be deleted.
do_unlink : bool (default=True)
Unlink objects from their collections, if needed, before deleting them.
deep : bool (default=True)
Delete the data corresponding to the object
Returns
-------
None
"""
if objects is None:
objects = bpy.data.objects
objects = set(objects)
if deep:
while objects:
# Intersect with objects in the scene. We have to do this because
# the set might contain invalid objects: This happens when objects
# share data and we remove the data.
objects &= set(bpy.data.objects)
if not objects:
break
obj = objects.pop()
typ = obj.type
if typ == 'EMPTY':
bpy.data.objects.remove(obj, do_unlink=do_unlink)
if typ == 'MESH':
clear_meshes(meshes=[obj.data])
elif typ == 'LIGHT':
clear_lights(lights=[obj.data])
elif typ == 'CURVE':
clear_curves(curves=[obj.data])
elif typ == 'CAMERA':
clear_cameras(cameras=[obj.data])
elif typ == 'LATTICE':
clear_lattices(lattices=[obj.data])
else:
while objects:
bpy.data.objects.remove(objects.pop(), do_unlink=do_unlink)
[docs]def clear_meshes(meshes=None, do_unlink=True, only_unused=False):
"""Delete all given meshes.
Parameters
----------
meshes : iterable of bpy.type.Mesh (default=None)
Meshes to be deleted. If the argument is not provided or None all meshes
will be deleted.
do_unlink : bool (default=True)
Unlink meshes from their objects and scenes, if needed, before
deleting them.
only_unused : bool (default=True)
Removes all unused meshes.
If an iterable of meshes is given as first parameter only those are effected,
otherwise all meshes.
Returns
-------
None
"""
if meshes is None:
meshes = bpy.data.meshes
meshes = list(meshes)
if only_unused:
while meshes:
mesh = meshes.pop()
if mesh.users == 0:
bpy.data.meshes.remove(mesh, do_unlink=do_unlink)
else:
while meshes:
bpy.data.meshes.remove(meshes.pop(), do_unlink=do_unlink)
[docs]def clear_lights(lights=None, do_unlink=True):
"""Delete all given lights.
Parameters
----------
lights : iterable of bpy.type.Light (default=None)
Lights to be deleted. If the argument is not provided or None all lights
will be deleted.
do_unlink : bool (default=True)
Unlink lights from their scenes, if needed, before deleting them.
Returns
-------
None
"""
if lights is None:
lights = bpy.data.lights
lights = list(lights)
while lights:
bpy.data.lights.remove(lights.pop(), do_unlink=do_unlink)
[docs]def clear_lattices(lattices=None, do_unlink=True):
"""Delete all given lattices.
Parameters
----------
lattices : iterable of bpy.type.Lattice (default=None)
Lattices to be deleted. If the argument is not provided or None all lattices
will be deleted.
do_unlink : bool (default=True)
Unlink lattices from their scenes, if needed, before deleting them.
Returns
-------
None
"""
if lattices is None:
lattices = bpy.data.lattices
lattices = list(lattices)
while lattices:
bpy.data.lattices.remove(lattices.pop(), do_unlink=do_unlink)
[docs]def clear_curves(curves=None, do_unlink=True):
"""Delete all given curves.
Parameters
----------
curves : iterable of bpy.type.Curve (default=True)
Curves to be deleted. If argument is not provided or None all curves
will be deleted.
do_unlink : bool (default=True)
Unlink curves from their scenes, if needed, before deleting them.
Returns
-------
None
"""
if curves is None:
curves = bpy.data.curves
curves = list(curves)
while curves:
bpy.data.curves.remove(curves.pop(), do_unlink=do_unlink)
[docs]def clear_cameras(cameras=None, do_unlink=True):
"""Delete all given cameras.
Parameters
----------
cameras : iterable of bpy.type.Camera (default=None)
Cameras to be deleted. If the argument is not provided or None all cameras
will be deleted.
do_unlink : bool (default=True)
Unlink cameras from their scenes, if needed, before deleting them.
Returns
-------
None
"""
if cameras is None:
cameras = bpy.data.cameras
cameras = list(cameras)
while cameras:
bpy.data.cameras.remove(cameras.pop(), do_unlink=do_unlink)
[docs]def clear_material(materials=None, only_unused=False):
"""Delete all given materials.
Parameters
----------
materials : iterable of bpy.types.Material (default=None)
Materials to be deleted. If argument is not provided or None all materials
will be deleted.
only_unused : bool (default=False)
Removes all unused material.
If an iterable of materials is given as first parameter only those are effected,
otherwise all materials.
Returns
-------
None
"""
if materials is None:
materials = bpy.data.materials
materials = list(materials)
if only_unused:
while materials:
material = materials.pop()
if not material.users:
bpy.data.materials.remove(material)
else:
while materials:
bpy.data.materials.remove(materials.pop())
[docs]def clear_empty_objects(do_unlink=True, deep=True):
"""Delete all empty objects.
Parameters
----------
do_unlink : bool (default=True)
Unlink objects from their collections, if needed, before deleting them.
deep : bool (default=True)
Delete the data corresponding to the object.
Returns
-------
None
"""
objects = set([object for object in bpy.data.objects if object.type == 'EMPTY'])
if deep:
while objects:
# Intersect with objects in the scene. We have to do this because
# the set might contain invalid objects: This happens when objects
# share data and we remove the data.
objects &= set(bpy.data.objects)
if not objects:
break
obj = objects.pop()
bpy.data.objects.remove(obj, do_unlink=do_unlink)
else:
while objects:
bpy.data.objects.remove(objects.pop(), do_unlink=do_unlink)
[docs]def clear_all(do_unlink=True, remove_collections=True):
"""Delete all instances in blender data blocks.
Parameters
----------
do_unlink : bool (default=True)
Unlink instances from their scenes, if needed, before deleting them.
remove_collections: bool (default=True)
Deletes all collections.
Returns
-------
None
"""
clear_empty_objects(do_unlink=do_unlink)
clear_cameras(do_unlink=do_unlink)
clear_curves(do_unlink=do_unlink)
clear_meshes(do_unlink=do_unlink)
clear_lights(do_unlink=do_unlink)
clear_objects(do_unlink=do_unlink)
clear_lattices(do_unlink=do_unlink)
clear_scene(remove_collections=remove_collections)
clear_material()