# Approaching vertex groups with Python

I want to approach two vertex groups belonging to two different objects, making sure that these objects are close together. How can I do that scripting?

Thank you so much!

Given an object ob, you can access its mesh at ob.data, and its vertices at ob.data.vertices. The vertex groups are defined on the object as well, at ob.vertex_groups. Each vertex group has a weight() function you can call to get the weight of a vertex.

vg0 = ob.vertex_groups[0]
for index, vertex in enumerate(ob.data.vertices):
try:
weight = vg0.weight(index)
except RuntimeError:
# Raised when the vertex is not part of the group
weight = 0.0

print('Vertex #%d at %s has weight %f' % (index, vertex.co, weight))


For efficient distance queries (from a point in space to a mesh) you can use the BVHTree module. This may work faster than trying to compute the distance between every point of one mesh to every other point on the other.

• Good catch, I updated the code. – dr. Sybren Jun 27 '18 at 9:52
• Lightspeed updating! – aliasguru Jun 27 '18 at 9:53
• uhmmm, sorry, i want to put very close my objects basing on their 2 vertex groups... why do we consider the weight? – Christian Marcias Jun 27 '18 at 11:19
• The code can be used to detect whether a vertex is in the vertex group (use the exception) or you can use the weight itself somehow. The exact use is up to you. – dr. Sybren Jun 27 '18 at 14:52