1
$\begingroup$

I need to merge 2 vertices into one with bpy.ops.mesh.merge(type='LAST'). I select 2 vertices, however there is no saved select order to them so I can't use merge with parameter 'LAST'.

import bpy, bmesh

obj = bpy.context.scene.objects.active
objData = obj.data
bpy.ops.object.mode_set(mode='EDIT')
meshData = bmesh.from_edit_mesh(objData)
vertices = meshData.verts
#select 2 testing vertices
vertices[0].select = True
vertices[1].select = True
bpy.ops.mesh.merge(type='LAST')

I get this error:

TypeError: Converting py args to operator properties:  enum "LAST" not found in ('CENTER', 'CURSOR', 'COLLAPSE')
Error: Python script fail, look in the console for now...

So instead I tried to snap the 3D cursor to the first selected vertex to be able to use merge with parameter 'CURSOR':

#select 2 testing vertices
vertices[0].select = True
bpy.ops.view3d.snap_cursor_to_selected()
vertices[1].select = True
bpy.ops.mesh.merge(type='CURSOR')

And I get this error:

RuntimeError: Operator bpy.ops.view3d.snap_cursor_to_selected.poll() failed, context is incorrect
Error: Python script fail, look in the console for now...

So my questions are:

  1. How do I change context to View 3D (if only 'OBJECT', 'EDIT', 'SCULPT', 'VERTEX_PAINT', 'WEIGHT_PAINT', 'TEXTURE_PAINT' are available) or am I missing something? And how come I am able to manually do it on the object in EDIT mode?

  2. Is there any way to edit the selection order of vertices so I can use merge with parameter 'LAST' as I originally intended?

$\endgroup$

1 Answer 1

2
$\begingroup$

This can be done by manipulating the mesh itself using the Bmesh module:

Would it be possible to get list of index of components BY SELECTION ORDER in Edit Mode?

You can access the selection history and proceed with the bmesh.ops calls for merging vertices:

bmesh.ops.pointmerge(bm, verts, merge_co)

Or do it entirely manually by modifying the mesh data itself.

If you have any UV's or vertex colors, you will have to take care of them too.

$\endgroup$
1
  • $\begingroup$ bm.select_history seems to work only in manual selection, it doesn't store anything that was selected using vertices[0].select = True -- bmesh.ops.pointmerge(bm, verts, merge_co) is working for me and does exactly what I want however it crashes, but after adding bmesh.update_edit_mesh(objData) it works just fine. Thank you very much $\endgroup$
    – mireille
    Nov 17, 2017 at 20:10

You must log in to answer this question.

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