Move CENTER (not pivot) to SELECTED array of (edges, vertex, faces) I was reading this and this about switching the pivot point.

And I managed to reproduce its 4 steps:

bpy.ops.mesh.primitive_cube_add(radius=1, view_align=False, enter_editmode=False, location=(0, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='VERT')
bpy.ops.view3d.snap_cursor_to_selected()
bpy.ops.object.editmode_toggle()
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')


So the logical question is: Can we get it down to 1 "keybind"?

The usual workflow should be:


I change from object to edit mode. 
I select "VERTS" to grab different points between what I need to place the CENTER of the object (the purpose of this is that every time I want to manipulate/rotate/translate) this object, those selected vertices will have the TRUE center of the object in LOCAL mode.
I choose "CENTER to SELECTED" (from somewhere/my function/addon, etc..)
(internally blender moves the pivot to selected, changes (snaps) the CENTER to pivot, and then returns the PIVOT to the center of the world. The idea is that the PIVOT will not get lost or in the way once it has completed its task).
The center of the object gets placed between the selected vertices (or edges/faces).


Is it possible to do this on a code or addon?
Thanks.
Bmesh version

The origin is local coordinate (0, 0, 0)

In local (vertex) space the selected location vector o is the average of selected vert coordinates   Make that the origin point by subtracting vector o from all verts' coordinates.

Then move the object to counter the shift in local coords.  The vector t  is the equivalent global space translation, of local space translation o.

Test script. Edit mode. Select verts, run script.

import bpy
import bmesh
from mathutils import Vector, Matrix
context = bpy.context
ob = context.object
me = ob.data

bm = bmesh.from_edit_mesh(me)
selected_verts = [v.co for v in bm.verts if v.select]
# average of selected verts
o = sum(selected_verts, Vector()) / len(selected_verts)
# make this the new origin
for v in bm.verts:
    v.co -= o
# move the object

mw = ob.matrix_world
t = mw * o - mw * Vector()
mw.translation += t

bmesh.update_edit_mesh(me)
me.update()