You can do this:
item='CAMERA'
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_by_type(type=item)
bpy.ops.object.delete()
where item
can take any of the following values according to this documentation:
[‘MESH’, ‘CURVE’, ‘SURFACE’, ‘META’, ‘FONT’, ‘ARMATURE’, ‘LATTICE’, ‘EMPTY’, ‘CAMERA’, ‘LAMP’, ‘SPEAKER’]
The above method works, but is not ideal. The following method works and is ideal. First remove the meshes with the following code:
for obj in bpy.context.scene.objects:
if obj.type == 'MESH':
obj.select = True
else:
obj.select = False
bpy.ops.object.delete()
Then you can execute the following code snippet to remove all the unused blocks. This way you don't have to close/open Blender or save/open a .blend file:
for block in bpy.data.meshes:
if block.users == 0:
bpy.data.meshes.remove(block)
for block in bpy.data.materials:
if block.users == 0:
bpy.data.materials.remove(block)
for block in bpy.data.textures:
if block.users == 0:
bpy.data.textures.remove(block)
for block in bpy.data.images:
if block.users == 0:
bpy.data.images.remove(block)
IMPORTANT NOTE: It looks like that there is some dependencies between some data blocks such as mesh, texture, image and materials. If you do not remove the data blocks on the highest level of the hierarchy, you will not be able to remove other data blocks or you have to take the risk and remove data blocks with users
more than 0
. So make sure you use the code above in the following order to remove data blocks. This way you can remove all unlinked (users == 0
) data blocks:
remove meshes --> remove materials --> remove textures --> remove images
The followings also have data blocks:
bpy.data.curves
bpy.data.lamps
bpy.data.cameras
camera
is actually an object datablock, not a camera. You need to unlink the object from the scene, delete the camera (ob.data
, which would becamera.data
in your example) and finally delete the object. If not possible to remove datablocks if they have users however, and clearing the user count may lead to crashes later on - thus it's advisable to use the delete operator instead. $\endgroup$