I have a script that takes in positional data and creates a cube based on their positions. Each time I run the script, I've noticed that the old cubes are still showing up in the User Perspective. It's a pain to go through each cube and manually delete them.
I've googled for a while to find a solution to no avail. I know that
for ob in bpy.data.objects:
Allows me to iterate through the objects in the scene. I get
Camera
Cube
Cube.001
Cube.002
...
but I don't know how to use the API to select only cubes to remove and I don't know how to remove them. Is unlinking the same thing as removing the cubes from the user perspective?
ob.name.startswith("Cube")is more efficient self-explaining, and the call to delete() can be moved outside the loop - all selected objects are deleted by a single call. Note that the delete operator will fail if in editmode! Other than that, it's the optimal solution,bpy.data.objects.remove()would cause trouble 'cause of users (scenes and mesh datablocks). – CodeManX Apr 21 '14 at 23:47elseclause to deselect all objects that could be selected but don't match the condition? – CodeManX Apr 21 '14 at 23:53ob.select = ob.type == 'MESH' and ob.name.startswith("Cube"), although it's a bit less clear in the visual appearance. – CodeManX Apr 22 '14 at 00:02