How can I get the vertices location connected to animated bones? E.g. the vertices of vertexgroup "foot.L" at keyframe 2?
2 Answers
Copy Location Constraint.
From https://blender.stackexchange.com/a/139758/15543
Can leverage the existing behaviour of the copy location constraint, targeted to a mesh, subtargeted to a vertex group.
Test script, run in object mode with mesh object selected. Will add an empty for each vertex group at the mean location (as determined by constraint) of each vertex group.
import bpy
context = bpy.context
ob = context.object
me = ob.data
for name in ob.vertex_groups.keys():
bpy.ops.object.empty_add(location=(0, 0, 0))
mt = context.object
mt.name = f"{ob.name}_{name}"
cl = mt.constraints.new('COPY_LOCATION')
cl.target = ob
cl.subtarget = name
Thanks, this gives me the center of the vertex group. Is it possible to get the location of all vertices (not only the center)?
Or using another slightly updated example from https://blender.stackexchange.com/a/139758/15543
this one adds an empty parented to each vertex in the vertex group.
For vertex group named "Group" containing vertex index 3, there will be an empty object mt
named "Group_3" parented to vertex 3. At any frame the global location of vertex 3 will be mt.matrix_world.translation
.
import bpy
from collections import defaultdict
context = bpy.context
vgs = defaultdict(list)
ob = context.object
me = ob.data
for v in me.vertices:
for g in v.groups:
vgs[ob.vertex_groups[g.group].name].append(v.index)
for name, vg in vgs.items():
for i in vg:
bpy.ops.object.empty_add(location=(0, 0, 0))
mt = context.object
mt.parent = ob
# mt.name = "%s_%d" % (name, i) # for python < 3.6
mt.name = f"{name}_{i}"
mt.parent_type = 'VERTEX'
mt.parent_vertices = (i,) * 3
Note would be a good idea here to jam empties into new linked collection, so they can be removed / hidden easily
Another way is to use one of the solutions outlined here
How do I get a mesh data-block with modifiers and shape keys applied in Blender 2.8?
for a modified mesh. However if you have a subsurf, for example, will have a lot of extra generated vertices.
-
$\begingroup$ Thanks, this gives me the center of the vertex group. Is it possible to get the location of all vertices (not only the center)? $\endgroup$– NanKatCommented Jan 7, 2020 at 21:14
I extended your first answer, this does work:
ob = bpy.data.objects['Person:Body']
me = ob.data
for name in ob.vertex_groups.keys():
bpy.ops.object.empty_add(location=(0, 0, 0))
mt = context.object
mt.name = f"empty_{ob.name}_{name}"
cl = mt.constraints.new('COPY_LOCATION')
cl.target = ob
cl.subtarget = name
mt.matrix_world = mt.matrix_world.copy()
mt.constraints.clear()
So, I am able to get the location of the vertex group per frame.
However, I can not get the location of the vertices. I tried out your second suggestion. Obviously, they are not at the same place as the animated Person is located.
Image shows the location of one vertice of the thumb (created empty), which is in the left corner instead of on the person.
-
$\begingroup$ actually I need the position of the linked parent vertex, but can't figure out how. So the location at the end of the black line $\endgroup$– NanKatCommented Jan 12, 2020 at 17:04
-
$\begingroup$ I'm so sorry, didn't have time to deeper look into this. Thanks so much for your support @batFINGER. I will check this weekend :) $\endgroup$– NanKatCommented Jan 15, 2020 at 12:33