1
$\begingroup$

I've written an operator to check all vertices' bone weights and change them according to some rules, in Edit Mode. If I check the new bone weights while still in Edit Mode, they have the correct value. If I switch to Object Mode and then back to Edit Mode, however, all the new bone weights are lost and the vertices have the old ones. Why is this happening?

This is an example of what I'm doing to change the bone weights:

for vertex in context.edit_object.data.vertices:
    for gr in vertex.groups:
        gr.weight = 1

EDIT: Doing context.edit_object.data.update() after chanigng the bone weights doesn't fix the problem.

Thanks in advance!

$\endgroup$

1 Answer 1

1
$\begingroup$

One way is to set up a dictionary with key groupname and a list of vertex indices and add them in object mode using vertex_group.add(..)

import bpy
context = bpy.context
ob = context.object

dic = {}
for g in ob.vertex_groups:
    dic[g.name] = []

for vertex in ob.data.vertices:
    for gr in vertex.groups:
        dic[ob.vertex_groups[gr.group].name].append(vertex.index)

# has to be in object mode for vertex_group.add
bpy.ops.object.mode_set()
w = 1.0
for vgname, verts in dic.items():
    #print(vgname)
    ob.vertex_groups[vgname].add(verts, w, 'REPLACE')
$\endgroup$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .