5
$\begingroup$

When modifying/accessing a mesh in Python, changes made while in edit-mode by the user are not available to Python.

bpy.context.object.data.vertices[x].select

For example, doesn't change for any vertex until I (for example) switch from edit to object mode.

Is there a way a way to access the current state of the mesh?

$\endgroup$
4
$\begingroup$

In edit-mode, the mesh as defined by bpy.types.Mesh is not used, there is in fact a different mesh-representation being used (bmesh.types.BMesh), which supports editing operations and modifications.

So changes to bpy.context.object.data are not displayed, and they will be overwritten when exiting editmode.

Instead, you have to access the BMesh.

import bpy
import bmesh

obj = bpy.context.edit_object
me = obj.data

# Get a BMesh representation
bm = bmesh.from_edit_mesh(me)

Once you have a bmesh from the edit-mesh, you are directly accessing the same data which the user is manipulating.

For the full example, see:

Text Editor -> Templates -> Python -> BMesh Simple Editmode

See BMesh API docs: http://www.blender.org/api/blender_python_api_2_72_release/bmesh.html

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.