I have written a script that deletes all vertices but the ones with lowest Z for selected objects in object mode but it does only work for a single object due to the delete function and I wonder if someone could help me to make it work for more then one selected object. The error is RuntimeError: Operator bpy.ops.object.mode_set.poll() Context missing active object
C = bpy.context
objs = C.selected_objects
for ob in objs:
loc_vertex_coordinates = [ v.co for v in ob.data.vertices ] # local coordinates of vertices
# Find the lowest Z value amongst the object's verts
minZ = min( [ co.z for co in loc_vertex_coordinates ] )
maxZ = max( [ co.z for co in loc_vertex_coordinates ] )
# Delete all vertices below maxZ (or above low)
#Start to deselect all vertices. To deselect vertices we need to deselect faces(polygons) and edges at first
for v in ob.data.polygons:
v.select=False
for v in ob.data.edges:
v.select=False
for v in ob.data.vertices:
v.select = False
v.select = v.co.z > minZ
# enter edit mode and delete selected vertices
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.delete(type='VERT')
bpy.ops.mesh.remove_doubles()
bpy.ops.mesh.dissolve_degenerate() #Clean som more
bpy.ops.mesh.edge_face_add() #Merge
bpy.ops.object.mode_set(mode='OBJECT')