# How do I make a script for adding modifiers?

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.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_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.

        bpy.context.object.modifiers[len(bpy.context.object.modifiers)-1].name = name

• 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] – batFINGER Nov 12 '19 at 12:51