# Iterating vertex groups in Python

I'm having trouble with a script. I'm trying to duplicate a selected object and copy them into an array configuration. Afterwards i'd like to select the same vertex group of each of the duplicated objects and move them.

For some reason each loop on moving the vertex group only moves the first duplicated object... I feel like I must have a fundamental misunderstanding of how edit mode works in scripting. I've tried multiple versions, but always have the same result.

The script I have is below:

import bpy
import random as random
import time

dup = bpy.context.selected_objects[0]

list = []

for i in range(0,4):
bpy.ops.object.select_all(action='DESELECT')
dup.select_set(True)

bpy.ops.object.duplicate()

obj1 = bpy.context.selected_objects[0]

obj1.location[0] = i*4
obj1.location[1] = 0
obj1.location[2] = 0

list.append(obj1)

for obj in list:
bpy.ops.object.select_all(action='DESELECT')
obj.select_set(True)

bpy.ops.object.editmode_toggle()
obj.vertex_groups.active = obj.vertex_groups[0]
bpy.ops.mesh.select_all( action = 'DESELECT' )
bpy.ops.object.vertex_group_select()
bpy.ops.transform.translate(value=(0, 0, 4.32424), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(False, False, True), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)
obj.vertex_groups.active = None
bpy.ops.object.editmode_toggle()


And this is the result, object i'm duplicating is upper left and all the vertex group moves are only going to the first object

Ugh, I figured it out, didn't realize that 'active' was different than 'selected' in the context of edit mode.

Here's the updated script:

import bpy
import random as random
import time

dup = bpy.context.selected_objects[0]

list = []

for i in range(0,4):
bpy.ops.object.select_all(action='DESELECT')
dup.select_set(True)

bpy.ops.object.duplicate()

obj1 = bpy.context.selected_objects[0]

obj1.location[0] = i*4
obj1.location[1] = 0
obj1.location[2] = 0

list.append(obj1)

for obj in list:
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = obj # Set active object
obj.select_set(True)

bpy.ops.object.editmode_toggle()
obj.vertex_groups.active = obj.vertex_groups[0]
bpy.ops.mesh.select_all( action = 'DESELECT' )
bpy.ops.object.vertex_group_select()
bpy.ops.transform.translate(value=(0, 0, 4.32424), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(False, False, True), mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)
obj.vertex_groups.active = None
bpy.ops.object.editmode_toggle()