# mesh.edges.foreach_set() doesn't update

Function to create a mesh from a numpy array.
Object gets created, but edges and verts are only shown when I toggle to edit mode and back to object mode.

Small example, run in object mode with Plane selected.

 def write_obj(verts, edges):

me = bpy.data.meshes.new("new_mesh")
me.vertices.foreach_set("co", verts.ravel())
me.update()
me.edges.foreach_set("vertices", edges.ravel())

obj = bpy.data.objects.new('new_obj', me)

mverts_co = np.zeros((len(mesh.vertices) * 3), dtype=np.float)
mesh.vertices.foreach_get("co", mverts_co)
return np.reshape(mverts_co, (len(mesh.vertices), 3))

fastedges = np.zeros((len(mesh.edges)*2), dtype=np.int) # [0.0, 0.0] * len(mesh.edges)
mesh.edges.foreach_get("vertices", fastedges)
return np.reshape(fastedges, (len(mesh.edges), 2))

active_obj = bpy.context.active_object

write_obj(verts, edges)


What kind of update() am I missing in my function?
mesh.update() doesn't work.

// Okay, foreach_set doesn't seem to have a way to update.
Except toggling between object and edit mode, but that made the script really slow. Originally I wanted to run a time comparison against from_pydata

def write_obj_from_pydata(verts, edges=None):
if edges is None:
# join vertices into one uninterrupted chain of edges.
edges = [[i, i+1] for i in range(len(verts)-1)]
me = bpy.data.meshes.new("new_mesh")
me.from_pydata(verts, edges, [])

obj = bpy.data.objects.new('new_obj', me)

• Cheers. IIRC this has come up before and something about not being the intended use rings a bell.. The me.from_pydata method is the intended way to go ... with the bonus of being able to also add faces. Aug 10, 2020 at 19:15
Use me.update(calc_edges_loose=True). Make sure you do it after you set the edges though, not before where you currently have me.update().