Merge 2 vertices at LAST and at CURSOR in Python 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:


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?
Is there any way to edit the selection order of vertices so I can use merge with parameter 'LAST' as I originally intended?
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.