I am trying to use the Vertex Proximity Modifier to update vertex weights and hoping to be able to read the same with Python.

I have a plane and a vertex group called "all" (which includes all the vertices and default weight 0.0) and the proximity object is set to a sphere. The modifier behaves as expected. But when I try the following code:

import bpy

obj = bpy.context.active_object # select the plane

for i in range(0, 24): # I know there are 25 vertices
    print (obj.vertex_groups['all'].weight(i))

I get 0.0 printed out for each vertex. Can I get the individual weight of each vertex with the modifier in action but not applied? (If I apply the modifier, then I get the intended results.)


Use bpy.types.Object.to_mesh:

to_mesh(scene, apply_modifiers, settings, calc_tessface=True, calc_undeformed=False)

Create a Mesh datablock with modifiers applied

This leaves your object unchanged and gives access to the modified vertex groups, e.g.

import bpy

mesh = bpy.context.object.to_mesh(bpy.context.scene, True, "PREVIEW")
for vertex in mesh.vertices:
    for group in vertex.groups:
        print(vertex, group, group.weight)
  • $\begingroup$ Works great! I was using bmesh - so I had to write it back to a mesh before this produced the expected results. Sorry I had missed giving the information about the bmesh earlier. Thanks a ton! $\endgroup$
    – user1020
    Jul 31 '13 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.