# how to save object data while changing mode (Object mode, Edit mode)?

how can I save the object data (Lists , edges , vertices , ...) if I switch the mode from 'Edit' mode to 'Object' mode ?

I wrote a script that append edges to a list in 'Edit' mode then call a function (from add-on called: Edit tools 2) that do some operations ;

One of the operations is changing the mode from 'Edit' to 'Object' then back to 'Edit' , when I want to use the edges from the list after calling this function I get the error :

"BMesh data of type BMEdge has been removed"

and this error is most likely happening because of switching the mode.

The mesh data stored for an object is different from the bmesh data used while in edit mode, while the object data is static and used for drawing and rendering the edit mesh is optimized for adding and deleting items. When you enter edit mode the mesh data is copied to a bmesh structure which is copied back and destroyed when you go back to object mode. This is why you get the error as the bmesh data that you are using has been destroyed and no longer exists.

I expect you should adjust your code to not toggle in and out of object mode, you should be able to do what you want while the object stays in edit mode or perform the steps after you have finished in edit mode.

One option is to stay in object mode, you don't have to go into edit mode to alter the mesh, unless the user is interactively editing it. You can create the bmesh, edit the mesh and copy it back to the object, using bm.to_mesh() multiple times.

import bpy
import bmesh

me = bpy.context.object.data

bm = bmesh.new()
bm.from_mesh(me)

for v in bm.verts:
v.co.x += 1.0
bm.to_mesh(me)

for v in bm.verts:
v.co.y += 1.0
bm.to_mesh(me)

for v in bm.verts:
v.co.z += 1.0
bm.to_mesh(me)

bm.free()
bpy.context.scene.frame_set(bpy.context.scene.frame_current)

• Thanks for the answer , I found that the function I call switch to 'Object' mode to take the object data : obj = context.active_object ,mesh = obj.data , is there any equivalent for 'context.active_object' in edit mode ? – Dante Jun 16 '17 at 12:59
• The active object is context.active_object and should be the one that is in edit mode. You can change the active object with context.scene.objects.active = obj. You can get the mesh data to any object without it being active bpy.data.objects['Cube'].data or through context.selected_objects – sambler Jun 16 '17 at 17:17