# How to make changes to my vertex groups with python

I need to mirror my weight paint from one side to the other. The mirror modifier isn't working for me because I have existing weight paint and groups for both sides. Unfortunately it's not feasible for me to manually copy and mirror each group like what has been done in a way suggested by another answer.

But it did inspire me to put myself into the position of not yet having the other side of groups yet. I tested this theory out by deleting my 'b__L' groups then using this script:

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'))


I added the modifier and this worked! So is there a way for the script above to be modified to:

• DELETE all starts with 'b__L' groups
• Add suffix '.R' to groups starting with 'b__R'

THEN

• Duplicate the remaining 'b__R' groups (ignore others)
• Replace title text of duplicates ('b__R' to 'b__L')
• Change the suffix of duplicates, '.R' to '.L.'

THEN something to simply remove the suffixes finally for import to game.

I'm aware this means I will have to remember to always delete the left hand side of my model to use mirror w this. I have no experience with Python and I’ve tried making some changes myself and it’s just returning me errors. Appreciate any help.

Ultimately what was missing here was another look at the core issue. Only toggling a suffix on and off was not the answer. My groups had names that differed by one character to their counterpart and regardless of having the correct .L/.R suffix, meant they could not link with the mirror modifier. I did not need to delete the vertex groups relevant to the mesh side I had deleted as previously thought.

That being said, this is the code you're after if you need to achieve something similar.

In my case, remove the prefix that caused the group names to no longer be linked to their counterparts [4:]. Add the correct suffixes. Revert both of these changes when the script is run again for import back to game.

import bpy
active_object = bpy.context.active_object

vertex_groups = active_object.vertex_groups[:]

for vertex_group in vertex_groups:
if vertex_group.name.endswith('.L'):
vertex_group.name = 'b__L' + vertex_group.name.replace('.L', '')
continue
if vertex_group.name.endswith('.R'):
vertex_group.name = 'b__R' + vertex_group.name.replace('.R', '')
continue
if (vertex_group.name.startswith('b__L') | vertex_group.name.startswith('b__R')):
suffix = '.L' if vertex_group.name.startswith('b__L') else '.R'
vertex_group.name = vertex_group.name[4:] + suffix


Thanks to my lovely sister for this script. Hope it can help someone else out there!