Blender Asked by Tortenrandband on January 6, 2021
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.add(count=len(verts))
me.vertices.foreach_set("co", verts.ravel())
me.update()
me.edges.add(count=len(verts))
me.edges.foreach_set("vertices", edges.ravel())
obj = bpy.data.objects.new('new_obj', me)
bpy.context.scene.collection.objects.link(obj)
def read_verts(mesh): #return np.array
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))
def read_edges(mesh): #return np.array
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
verts = read_verts(active_obj.data)
edges = read_edges(active_obj.data)
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)
bpy.context.scene.collection.objects.link(obj)
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()
.
Correct answer by scurest on January 6, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP