I want a simple script to iterate through all objects, and run remove doubles. I only want to run it once so the python console is fine. Unfortunately the following code only sometimes works and only if I select a single object in the 3D view and run it with context.selected_objects. But I want all objects tidied up and don’t want to have to select them manually (there are 861 which are not manifold due to doubles). I presume I’m doing something wrong with the selections, as the remove_doubles just returns “Info: Removed 0 vertices”, but a manual wr works fine. I’m a 10-day old python and blender newbie, so I’ve probably missed something obvious, but would appreciate some help.
As an aside I’d also like to list any objects which are not manifold after removing doubles, so if you know that too…
(my plan was to modify http://yorik.uncreated.net/scripts/blender25/mesh_select_nonmanifold_edges.py, but pointless if I can’t do step 1.)
Cheers D
obj= bpy.data.objects
#obj= bpy.context.selected_objects
for ob in obj:
if ob.type == ‘MESH’:
The edit mode is applied on the active object, not the selected one. So you have to set your object as active in the scene as well, like that :
bpy.context.scene.objects.active = ob
Then it should work.
For the second point, there is an operator in Blender that selects the non-manifold vertices of a mesh :
bpy.ops.mesh.select_non_manifold()
So, an idea is to deselect every vertices of your mesh, apply this operator and see if the current vertex selection is empty. If so, your mesh will be manifold.
#*** Remove doubles from all objects *** #Make all layers visible (edit mode can not be set for objects on hidden layers)
for i in range(1, 20):
bpy.context.scene.layers[i] = True
#Iterate through all objects. If it’s a mesh: activate, set edit mode, select all of mesh, remove doubles, set object mode
obj= bpy.data.objects
for ob in obj:
if ob.type == ‘MESH’:
bpy.context.scene.objects.active = ob
bpy.ops.object.mode_set(mode=‘EDIT’)
bpy.ops.mesh.select_all(action=‘SELECT’)
bpy.ops.mesh.remove_doubles(threshold=0.0001, use_unselected=False)
bpy.ops.object.mode_set(mode=‘OBJECT’)
for i in range(1, 20):
bpy.context.scene.layers[i] = True
obj= bpy.data.objects
for ob in obj:
if ob.type == ‘MESH’:
bpy.context.scene.objects.active = ob
bpy.ops.object.mode_set(mode=‘EDIT’)
bpy.ops.mesh.select_all(action=‘DESELECT’)
bpy.ops.mesh.select_non_manifold()
if ob.data.total_vert_sel>1:
print(ob.name)
bpy.ops.object.mode_set(mode=‘OBJECT’)
*** Select a named object for editing ***
ob = bpy.data.objects[‘OB_NAME_HERE’]
bpy.context.scene.objects.active = ob