# How to remove a common prefix from vertex-group names?

I'm looking to write a script that can remove a prefix from all vertex group names, so that I can use the mirror modifier appropriately. The models I work with have "b_" as a prefix for most group names. I found this script to add the prefix back on after mirroring, but it still takes far too long to process the whole thing.

Updated: Removes & Adds prefix back in

import bpy
obj = bpy.context.active_object
bpy.ops.object.mode_set(mode='OBJECT',toggle=True)
for g in obj.vertex_groups:
if g.name.startswith('b_'):
print(g.name)
g.name = g.name[2:]

else:
print(g.name)
g.name = 'b_' + g.name

• Intrigued, what is the hassle with "b_" prefix and mirror modifier? secondly what takes a long time to process, surely not the posted script? Otherwise this appears to be a python string manipulation question, that can be handled with replace or similar. – batFINGER Mar 10 '16 at 14:21
• When i use the mirror modifier with these vertex group names, the vertex groups are not mirrored, because blender is looking for the Ls and Rs, it takes a few minutes to change all of the vertex groups, which is why i looked for this script, but it only covers putting the prefix back in, and not removing it in the first place – Komodo Mar 10 '16 at 15:40

To remove the "b_" prefix

import bpy
obj = bpy.context.active_object
prefixes = ["b_", "f_", "h_", "x_"]

bpy.ops.object.mode_set(mode='OBJECT',toggle=True)
for g in obj.vertex_groups:
for prefix in prefixes:
if g.name.startswith(prefix):
g.name = g.name[2:]

• Thanks! I altered this to work both ways as you can see in the main post. However, the face groups, hand groups and miscellaneous groups such as tail bones use a different prefix (f_, h_, x_). I'm wondering if there's a way I could adapt the script to check for all four prefixes separately and add the correct prefix back on :S – Komodo Mar 11 '16 at 14:14

The manual page for the mirror modifier explains the vertex groups option as -

Vertex Groups

When enabled, the Mirror modifier will try to mirror existing vertex groups.

A very nice feature, but one that has very specific prerequisites:

• The vertex groups you want to mirror must be named following the usual left/right pattern (i.e. suffixed by something like ”.R”, ”.right”, ”.L”, etc).
• The mirror side vertex group must already exist (it will not be created automatically). It must also be completely empty (no vertices assigned to it).

So you should have no problem leaving your prefix in place, but you want to add a .L suffix to the existing vertex groups and create a matching empty .R group for each existing vertex group. (or add .R and create the .L groups)

import bpy
obj = bpy.context.active_object

vg = obj.vertex_groups[:]

for g in vg:
if not g.name.endswith('.L'):
obj.vertex_groups.new(g.name+'.R')
g.name = g.name+'.L'
else:
obj.vertex_groups.new(g.name.replace('.L', '.R'))

• That's very interesting, it works well for custom models, but a lot of the models I work with in this scenario have to be named as they were to function properly in game, so I can't change the names to append a suffix, only remove and replace the prefix. Thanks though, one step closer to becoming a python master :) – Komodo Mar 11 '16 at 14:04
• The suffix makes the mirror vertex groups work. Before exporting for the game engine you can apply modifiers, then run a script that has if g.name.endswith('.R): g.name = 'b_'+g.name.replace('.R','') – sambler Mar 11 '16 at 14:19