# Replacing all mesh elements in a Mesh object

I'm working on the Animation Nodes Addon and want to create some new nodes which can work with mesh data like polygons. To output the created mesh I have to put it onto an object like this:

mesh = bpy.data.meshes.new(name = "mesh")
mesh.from_pydata(vert_data, edge_data, face_data)
object.data = mesh


This works all fine but the problem is that this generates a new mesh object on each call, which means about 30 times per second.

I also tried to call .from_data on an existing Mesh but this just appends the new mesh data. Is there a clean and fast way to remove all verts, so that I can append the new data to an empty Mesh?

bm = bmesh.new()
bm.to_mesh(object.data)
object.data.from_pydata(vert_data, edge_data, face_data)


This seams to work but throws an error I don't understand (or more a warning). This warning looks like here: https://developer.blender.org/T40571

Can someone explain this message or tell me a way to work around that?

## 1 Answer

The functions for the removal of mesh elements for Mesh(ID) are turned off in the source code.

#if 0 /* BMESH_TODO Remove until BMesh merge */
func = RNA_def_function(srna, "remove", "ED_mesh_vertices_remove");
RNA_def_function_flag(func, FUNC_USE_REPORTS);
RNA_def_int(func, "count", 0, 0, INT_MAX, "Count", "Number of vertices to remove", 0, INT_MAX);
#endif
}


from_pydata is a utility function written in python. You could use bmesh but it might be slower.

I.e. (see verts.new, edges.new, faces.new)

import bmesh
bm = bmesh.new()
for co in vert_data:
bm.verts.new(co)

#bm.verts.ensure_lookup_table()

for edge_indices in edge_data:
bm.edges.new((
bm.verts[edge_indices[0]],
bm.verts[edge_indices[1]]
))
for face_indices in face_data:
bm.faces.new(tuple(
bm.verts[index] for index in face_indices
))

bm.to_mesh(obj.data)
bm.free()


Btw: I was able to crash blender using this simple script to test from_pydata for existing geometry.

• ok thanks, is there a similiar bmesh function for from_pydata? – Jacques Lucke Dec 27 '14 at 12:53
• I am not aware of any function resembling from_pydata for bmesh. – pink vertex Dec 27 '14 at 13:04
• then I have to write this myself... :/ this will be quite slow – Jacques Lucke Dec 27 '14 at 13:05
• added a short section. – pink vertex Dec 27 '14 at 13:15
• i see, I will try this out now. Thanks a lot – Jacques Lucke Dec 27 '14 at 13:15