Moving mesh vertices in Edit mode

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:

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

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.

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:

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.

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.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments