In order to move mesh vertices using the Blender Python API without leaving EDIT mode we need to use bmesh structure. With it, we can conveniently manipulate all the geometry of the object.

Add a cube to the scene (if you removed it earlier) Shift + a – Mesh – Cube, and switch to Edit mode by pressing the Tab key. Select all the points (a) and move them somewhere by pressing the g key.

Now let’s return the geometry to the origin using the Python API.

We can get a pointer to an object’s data structure from its “data” property. For the current active object:

1 2 3 |
obj_data = bpy.context.object.data # bpy.data.meshes['Cube'] |

Let’s create an object of the “bmesh” type, passing in the parameters a pointer to the object’s data.

1 2 3 4 5 |
import bmesh bm = bmesh.from_edit_mesh(obj_data) # <BMesh(0x000001E6167F9D08), totvert=8, totedge=12, totface=6, totloop=24> |

We can access the cube vertices through the “bmesh.verts” property. This is a list where each element is an object corresponding to one vertex. We can access the coordinates of each vertex through it.

1 2 3 |
bm.verts[0] # <BMVert(0x000001E607C01250), index=0> |

To move vertices, we just need to set the desired coordinates for each of it.

For example, let’s move the entire geometry so that the first vertex is at the origin. The local coordinates of the origin point are (0.0, 0.0, 0.0).

Geometry first point coordinates:

1 2 3 |
bm.verts[0].co # Vector((5.0934367179870605, 4.548335552215576, 3.2297282218933105)) |

To move any point while maintaining its offset relative to the first point, we need to subtract the coordinates of the first point from its current coordinates.

Store a copy of the first vertex coordinates in a separate variable, and loop through all the vertices, subtracting the saved value from their coordinates.

1 2 3 4 |
v_co = bm.verts[0].co.copy() for v in bm.verts: v.co -= v_co |

In order for the changes made to the geometry to take effect and be displayed in the viewport, we need to update the mesh data structure by calling the “update()” method.

1 |
obj_data.update() |

D