I have imported a lot of STL files with many redundant faces and vertices.

As the title says, I imported 80 STL meshes and I need to clean them up a bit as I get constant z fighting. It seems to be a soup of triangles that often overlap with opposite normals. I need to remove the double faces, recompute the normals so that they are all pointing outwards and maybe add a small smoothing algorithm to the mesh to remove the irregularities. I googled it some and I managed to find some hints and tips.

I found a post that told me to do this:

  • select it (hint: blender default is with the right mouse button) and press TAB to go into edit mode
  • type A (maybe repeatedly) to make sure all vertices are selected
  • type W and select ‘remove doubles’ from the pop-up menu (I think the importer does this automatically, but just to make sure)
  • type ALT-J to join triangles into quads as far as possible
  • press TAB to exit edit mode

I want to do this in python instead of doing it manually.

I have found out that you can do this:
bpy.ops.object.mode_set(mode=‘EDIT’)bpy.ops.mesh.remove_doubles(threshold=0.01, use_unselected=False)

However, I have no idea how I can put this into the loop? As it stands I have no idea. I tried looping over the objects, but the remove_doubles function was not available for objects, nor was the function mode_set.

This is my code as far as I was able to make it:

objects = bpy.data.objectsfor object in objects:
    object.mode_set(mode='EDIT')
    object.data.remove_doubles(threshold=0.01, use_unselected=False)

Additionally, what is use_unselected? It didnt say in the documentation.

Thank you for your help.

Yours,
Noobs.

For selected objects:


import bpy


for obj in bpy.context.selected_objects:
    bpy.context.scene.objects.active = obj
    bpy.ops.object.mode_set(mode = 'EDIT')
    bpy.ops.mesh.select_all(action='SELECT')
    bpy.ops.mesh.remove_doubles(threshold=0.01, use_unselected=False)
    bpy.ops.mesh.tris_convert_to_quads(face_threshold=3.14159, shape_threshold=3.14159)
    bpy.ops.object.mode_set(mode = 'OBJECT')