I have a mesh with armature modifier. Is there any efficient way to read its vertices coordinates with modifiers accounted in Blender 2.80?
So far the only way I found is to duplicate the object, apply modifier, read vertices and remove duplicated object:
def extract_vertices(my_object):
# Duplicate object
for obj in bpy.data.objects: obj.select_set(False)
my_object.select_set(True)
bpy.ops.object.duplicate_move()
# Apply modificators
duplicated_object = bpy.context.scene.objects[-1]
bpy.context.view_layer.objects.active = duplicated_object
duplicated_object.select_set(True)
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="RIG")
# Read vertices
vertices = [v.co for v in duplicated_object.data.vertices]
# Remove duplicated object
my_object.select_set(False)
bpy.ops.object.delete()
return vertices
It works, but very slow. I need to run this thing thousands of times in the loop, adjusting modifier every iteration.
It seems that Object.to_mesh()
described in answer here could be exactly what I need, but it's signature is changed in Blender2.80 and there's no apply_modifiers
parameter anymore.
bm.verts.ensure_lookup_table()
andbm.free()
, which I wouldn't know is needed. $\endgroup$bm.free()
(you won't create a memory leak if you don't use it), but it is more efficient because it deallocates the memory right away and not just when there is no reference anymore. It's always a good idea to check the Blender Python API docs (docs.blender.org/api/current/bmesh.html) $\endgroup$