1
$\begingroup$

When separating a mesh in edit mode using P, bpy.ops.mesh.separate(type='SELECTED'), it will select your vertices and make a new object. You are left with two objects.

However, if you select all of the mesh of one object and use it, you will be left with two objects, of which one of has no vertices in it. I need to select this object "if no vertices are present" and delete it.

Here is why I need to do this. I have one object, say a sphere and then using Vertex Weight Proximity and Vertex Weight Edit on other objects (which are joined with one name), I am able to detect all objects intersecting my sphere. These will leave a vertex group, which I can then link up ⌃ Ctrl + L and separate the objects.

However, if all the objects are selected, then I am faced with the problem of having one object with no vertices. I do hope I am clear enough in my question. I need some help with the python script of deleting the object if there are no vertices.

Here is my initial script to separate the objects.

bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.vertex_group_set_active(group='V_Group')
bpy.ops.object.vertex_group_select()
bpy.ops.mesh.select_linked(delimit={'SEAM'})
bpy.ops.mesh.separate(type='SELECTED')
bpy.ops.object.editmode_toggle()
$\endgroup$

1 Answer 1

2
$\begingroup$

Remove all zero vert mesh objects from scene.

Script below will remove all zero vert mesh objects from the scene. For your use case could instead look only over context.selected_objects.

import bpy
scene = bpy.context.scene

empty_meshobs = [o for o in scene.objects
                 if o.type == 'MESH'
                 and not o.data.vertices]
                 
while empty_meshobs:
    bpy.data.objects.remove(empty_meshobs.pop()) 

Another alternative is to not split if the selected vertices are all of total.

sel_verts, _, _ =   context.object.data.count_selected_items()
if sel_verts < len(context.object.data.vertices):
    bpy.ops.mesh.separate(type='SELECTED')
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .