0
$\begingroup$

I have two meshes: (A and B) that are partly overlapping and have some vertices with different indices but the exact same global location. I would like to look at the vertex locations of mesh A and select the vertices with the same location within mesh B.

My attempt modifying some code snippets:

vertices = []
bpy.context.view_layer.objects.active = bpy.data.objects['A']
bpy.ops.object.editmode_toggle()
obj=bpy.context.object
if obj.mode == 'EDIT':
    bm=bmesh.from_edit_mesh(obj.data)
    for v in bm.verts:
        vertices.append(obj.matrix_world @ v.co)

else:
    print("Object is not in edit mode.")

bpy.ops.object.editmode_toggle()    
#print(vertices)

bpy.context.view_layer.objects.active = bpy.data.objects['B']
bpy.ops.object.editmode_toggle()

if obj.mode == 'EDIT':
    bm=bmesh.from_edit_mesh(obj.data)
    for v in bm.verts:
        if v in vertices:
            v.select

I highly appreciate your help!

$\endgroup$
4
  • $\begingroup$ Hello, the code above might work, but it will stall indefinitely if you have a very high number of verts. In this case you'll need to explore more sophisticated methods. On average how many verts are you comparing between the two meshes ? Also if the transform of your objects is different, you'll need to use their local matrix to transform the vertex coordinates from local to global coordinates (obj.matrix_world @ v.co) $\endgroup$
    – Gorgious
    Jan 10, 2023 at 20:15
  • $\begingroup$ Mesh A = 7 000 verts; Mesh B is 19 000 verts. The code above runs without errors but in its current state doesn't select any vertices from mesh B. I edited the code above accordingly to your suggestion. With print statements I noticed that the second: for v in bm.verts: never runs. The vertices array is not empty however and has the expected len of around 7 000 $\endgroup$
    – Alex22
    Jan 10, 2023 at 21:41
  • $\begingroup$ if v in vertices is always False. Because it means BMVert in list of Vectors. $\endgroup$
    – tetii
    Jan 11, 2023 at 15:53
  • $\begingroup$ yep use if v.co in vertices btw the error would have been spotted more easily if you used more descriptive names eg vertex_coordinates instead of vertices $\endgroup$
    – Gorgious
    Jan 12, 2023 at 6:59

1 Answer 1

0
$\begingroup$

Thank you for your suggestions. I now finally got it working with the code below. The last thing I changed before it started working was replacing all instances of "obj" with "bpy.context.object". Not sure why that solved the issue. The code would run but would not select any vertices of object B before that.

vertex_coordinates = []
bpy.context.view_layer.objects.active = None
bpy.context.view_layer.objects.active = bpy.data.objects['A']
bpy.ops.object.mode_set(mode ='EDIT')

if bpy.context.object.mode == 'EDIT':
    bm = bmesh.from_edit_mesh(bpy.context.object.data)
    for v in bm.verts:
        vertex_coordinates.append(bpy.context.object.matrix_world @ v.co)

bpy.ops.object.mode_set(mode='OBJECT')   

bpy.context.view_layer.objects.active = None
bpy.context.view_layer.objects.active = bpy.data.objects['B']
bpy.ops.object.mode_set(mode ='EDIT')

if bpy.context.object.mode == 'EDIT':
    bm = bmesh.from_edit_mesh(bpy.context.object.data)
    for v in bm.verts:
        if (bpy.context.object.matrix_world @ v.co) in vertex_coordinates:
            v.select = True
$\endgroup$

You must log in to answer this question.

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