0
$\begingroup$

I have made a script for adding VertexWeightMix modifers to a selected mesh. However, when I run the script, it adds tons of modifers, but only changes the first one it adds. How can I fix this? I don't want to apply them immediatly once a modifier is created.

for vertex_b in namelist:
    vertex_a = "bone_01D2"
    vertexA = bpy.context.object.vertex_groups.get(vertex_a)
    vertexB = bpy.context.object.vertex_groups.get(vertex_b)
    if vertexA is None:
        print("Vertex " + vertex_a + " Does not Exist!")
        continue
    if vertexB is None:
        print("Vertex " + vertex_b + " Does not Exist!")
        continue
    bpy.ops.object.modifier_add(type='VERTEX_WEIGHT_MIX')
    bpy.context.object.modifiers["VertexWeightMix"].vertex_group_a = vertex_a
    bpy.context.object.modifiers["VertexWeightMix"].vertex_group_b = vertex_b
    bpy.context.object.modifiers["VertexWeightMix"].mix_mode = 'ADD'
    bpy.context.object.modifiers["VertexWeightMix"].mix_set = 'B'

Edit: I figured out that if you declare a variable i = 0, then you can convert it to a string, append it to the name, then increase it. However, if I ran it again, it would not work.

$\endgroup$
0
$\begingroup$

I can just grab the length of the number of modifiers and then subtract it by one to get the index.

        bpy.context.object.modifiers[len(bpy.context.object.modifiers)-1].name = name
        bpy.context.object.modifiers[len(bpy.context.object.modifiers)-1].vertex_group_a = vertex_a
        bpy.context.object.modifiers[len(bpy.context.object.modifiers)-1].vertex_group_b = vertex_b
        bpy.context.object.modifiers[len(bpy.context.object.modifiers)-1].mix_mode = 'ADD'
        bpy.context.object.modifiers[len(bpy.context.object.modifiers)-1].mix_set = 'B'
| improve this answer | |
$\endgroup$
  • $\begingroup$ In as much as this "works" I wouldn't do it this way. Can add a modifier to any object ob with m = ob.modifiers.new("name", 'TYPE') set properties on the newly added modifier via m.prop = foo. Please take a look at blender.stackexchange.com/a/132861/15543 for an example. Also the last item of a list can be indexed via foo[-1] $\endgroup$ – batFINGER Nov 12 '19 at 12:51

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