# Cannot set vertex group to active with python while in background mode?

I was trying to run blender in the background and run the python file to select a group and change it's size

here is the shell command I ran:

blender -b filename.blend -P pythonname.py


And inside pythonname.py I have this:

bpy.ops.object.vertex_group_set_active(group='Group')
bpy.ops.object.vertex_group_select()
bpy.ops.transform.resize(value=(float(val1),float(val2),float(val3)))
bpy.ops.export_mesh.stl(filepath="/var/www/html/kiosk/stlv1.0/cylinder_savedcopy.stl")
bpy.ops.wm.quit_blender()


I was stopped at the active part.

• What's the error message? The operator fails if there's either no vertex group at all, or if none of the given name was found. – CodeManX Jun 27 '14 at 23:08

For me it's not the operator that sets the active vertex group that fails, but the vertex group select. The reason is that the object needs to be in edit mode in order to run it:

import bpy
vgroups = bpy.context.object.vertex_groups
vgroups.active_index = vgroups["Group"].index
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.object.vertex_group_select()


You may also select the relevant vertices without operator:

import bpy

ob = bpy.context.object

vi = ob.vertex_groups.find("Group")

for v in ob.data.vertices:
for g in v.groups:
if g.group == vi:
v.select = True


... but it only works in object mode. To transform the selection, you need to switch to edit mode or write a low-level replacement (requires object mode):

import bpy
from mathutils import Vector

ob = bpy.context.object
me = ob.data
vi = ob.vertex_groups.find("Group")

verts = [v for v in me.vertices for g in v.groups if g.group == vi]
center = sum(map(lambda v: v.co, verts), Vector()) / len(verts)

for v in verts:
v.co = (v.co - center) * 2 + center