6
$\begingroup$

When is it appropriate to use one over the other?

In example, I have the following:

obj = bpy.data.objects[name]
bpy.context.scene.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
dmesh = obj.data
mesh = bmesh.from_edit_mesh(dmesh)
# do stuff with mesh
#
# call one or the other:
bmesh.update_edit_mesh(dmesh)
dmesh.update()

Which call is correct in the end?

In my tests they seem to do the same.

$\endgroup$
2
  • 2
    $\begingroup$ The example is slightly misleading in that dmesh may not necessarily be context.object.data, that said I'd say the rule of thumb is use bmesh.update_edit_mesh() when you are using bmesh and mesh.update() when you are not. $\endgroup$
    – batFINGER
    Jun 9, 2016 at 7:25
  • $\begingroup$ @batFINGER: thanks, I have updated the code. $\endgroup$
    – shinjin
    Jun 9, 2016 at 8:06

1 Answer 1

10
$\begingroup$

The mesh data used by an object is stored within object.data, the most obvious data being stored within the vertices, edges and polygons properties.

When you add or alter edges or polygons you need to use object.data.update() to see the changes in the 3dview.

To see this working, start with a default cube in wireframe shading and enter the following into a python console.

odata = bpy.data.objects['Cube'].data
odata.edges[8].vertices[0] = 3
odata.edges[8].vertices[1] = 5

You shouldn't see any difference yet, now enter

odata.update()

and you will see one of the edges is now across the top face and not on the side.

Instead of directly editing the mesh data, it is preferred to use the extra functionality provided in the bmesh module. The function bmesh.from_edit_mesh() makes a copy of the mesh data that you can make changes to while the object is in edit mode, the changes you make remain in this copy until you call bmesh.update_edit_mesh(), this lets you add 10,000 faces and then do one refresh to reduce the amount of drawing/copying needed. If you directly alter the object data while the object is in edit mode then those changes will be lost as the bmesh edit_mesh will overwrite any changes upon exiting edit mode.

Similar to bmesh.from_edit_mesh() and bmesh.update_edit_mesh() we also have bm.from_mesh() and bm.to_mesh() which allow us to edit the mesh data while the object remains in object mode. (these are available after bm = bmesh.new())

$\endgroup$
2
  • $\begingroup$ So, does bmesh.update_edit_mesh(odata) implies an odata.update()? $\endgroup$
    – shinjin
    Jun 11, 2016 at 5:02
  • 1
    $\begingroup$ No, the edit mesh is separate from odata, but exiting edit mode transfers the edit mesh to odata, then does odata.update(). The update_edit_mesh() is just to update the edit mode viewport display. If you use from_mesh() it doesn't alter odata until you use to_mesh() which then also updates. $\endgroup$
    – sambler
    Jun 11, 2016 at 5:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .