Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

March 26, 2012

Deleting Objects from a scene

There might be quicker ways of doing this, but this describes the process.
# do not run this on a scene with more than 100 objects... it will be a waste of time. (printing is slow)
for item in bpy.data.objects:
... print(item.name, item.type)
... 

# objects of type mesh are containers for meshes.
# multiple differently named objects can share the same base mesh.
# objects are in that sense 'users' of mesh data.
for item in bpy.data.objects:
... if item.type == "MESH":
...     print(item.name)
... 
Mesh
Mesh.001

for mesh in bpy.data.meshes:
... print(mesh.name)
... 
Cube
Cube.001
Cube.002

# maybe use some blender forensics to consider the two printed sequences above. 
# what must I have done to have 3 mesh structures, but only 2 objects of type 'MESH'? 
we can't delete mesh data if an object uses it. And simply deleting an object doesn't flush the mesh it used (altho I do recall seeing a method that does exactly that) or if you can do a broad stroke:

3 comments:

  1. thanks you very much. It was helpfull

    ReplyDelete
  2. I am doing like this
    bpy.ops.object.mode_set(mode='OBJECT')
    bpy.ops.object.select_by_type(type = 'MESH')
    bpy.ops.object.delete(use_global=False)
    for item in bpy.data.meshes:
    bpy.data.meshes.remove(item)

    ReplyDelete
    Replies
    1. i'm pretty sure select_by_type was added back after I wrote this original post - I will update

      Delete

Please use Blender.StackExchange.com for python scripting questions unrelated to this post.