3
$\begingroup$

I am trying to figure out why this does not update the mesh's data.

The goal of this is to ensure that boundary edges of meshes imported from SVG are never longer than a certain amount. This is followed by code that triangulates the mesh.

if obj.mode != 'EDIT': bpy.ops.object.mode_set(mode='EDIT')

bm_s = bmesh.from_edit_mesh(obj.data)
bm_s.edges.ensure_lookup_table()
print("Vertices before subdiv: {}".format(len(obj.data.vertices)))
print("Vertices in bmesh before subdiv: {}".format(len(bm_s.verts)))
 
edges_outside = [ e for e in bm_s.edges if len(e.link_faces) <= 1]

for e in [ e for e in edges_outside if e.calc_length() >= 2 * avg_length_inside ]:
    d = int(e.calc_length() / avg_length_inside)
    bmesh.ops.subdivide_edges(bm_s, edges=[e], cuts=d, use_grid_fill=True)

print("Vertices in bmesh after subdiv: {}".format(len(bm_s.verts)))
bmesh.update_edit_mesh(obj.data)
print("Vertices after subdiv: {}".format(len(obj.data.vertices)))
poly = triangulate.obj_to_poly(bm_s)
bm_s.free()        

This prints - for example:

Vertices before subdiv: 100
Vertices in bmesh before subdiv: 100
Vertices in bmesh after subdiv: 148
Vertices after subdiv: 100

The funny thing is: if I run this code and it is not followed by anything, it updates the mesh exactly like I want, but of code follows right after, the object's data will not be updated.

Why is that?

$\endgroup$

1 Answer 1

2
$\begingroup$

This is because it is temporary data in edit mode. When exiting edit mode, the obj data will be updated.

import bpy, bmesh

obj = bpy.data.objects["Cube"]
bm_s = bmesh.from_edit_mesh(obj.data)
bm_s.edges.ensure_lookup_table()
print("Vertices before subdiv: {}".format(len(obj.data.vertices)))
print("Vertices in bmesh before subdiv: {}".format(len(bm_s.verts)))
 
edges = [e for e in bm_s.edges]

for e in edges:
    bmesh.ops.subdivide_edges(bm_s, edges=[e], cuts=2, use_grid_fill=True)

print("Vertices in bmesh after subdiv: {}".format(len(bm_s.verts)))
bmesh.update_edit_mesh(obj.data)

bpy.ops.object.mode_set(mode='OBJECT')

print("Vertices after subdiv: {}".format(len(obj.data.vertices)))
Vertices before subdiv: 8
Vertices in bmesh before subdiv: 8
Vertices in bmesh after subdiv: 32
Vertices after subdiv: 32

You also can use obj.update_from_editmode() to update to object

$\endgroup$
2
  • $\begingroup$ great, thanks! and is there a way to update it back without exiting edit mode? i guess it's obj.update_from_editmode() - Would you mind if I added that to the answer? $\endgroup$
    – simone
    Commented Jul 30 at 7:45
  • $\begingroup$ obj.update_from_editmode() $\endgroup$
    – X Y
    Commented Jul 30 at 8:05

You must log in to answer this question.

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