How to append .L and .R to my vertex groups with script - AND maybe how to duplicate I'm trying to create a simple script that appends '.L' and '.R' to my existing vertex groups respectively. They already have an existing naming convention that begins with 'b__R' and 'b__L' and I don't want to change that, you can see that's actually an important part of my 'if'. 

The mirror modifier does not recognise the L and R in its current format, so I just want to append the .L and .R then be able to remove it when I'm ready to import to my game. I want it to ignore central groups I have that do not have b__R/b__L in the name so that those just mirrors themselves into their existing groups.

Here's what I currently have, and it's not working:

import bpy
obj = bpy.context.active_object

vg = obj.vertex_groups[:]

for g in vg:
    if g.name.beginswith(‘b__R’):
        g.name = g.name+’.R’
    if g.name.beginswith(‘b__L’):
        g.name = g.name+’.L’


To reverse:

import bpy
obj = bpy.context.active_object

vg = obj.vertex_groups[:]

for g in vg:
    if g.name.beginswith(‘b__R’):
        g.name = g.name.replace(’.R’,’’)
    if g.name.beginswith(‘b__L’):
        g.name = g.name.replace(’.L’,’’)


I am COMPLETELY unfamiliar with Python just keep that in mind, I'm just taking guesses here. I'm really just using Python as a means to an end and copied and edited the script from an answer to a similar question on here (linked below).

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

Also, importantly - your opinion. Is this the best way to do it? I already have an L and R with assigned weights and groups BEFORE deleting half of my mesh (in order to use the modifier). I'm working with a pre existing mesh here. Or should I delete the groups associated with the side of vertices I have deleted to use the modifier, then use the script to also CREATE the other side's groups again? I ask this because I'm not really sure how the data transfer works in the former case and whether those groups will be holding onto information from the deleted vertices. 

If it turned out that the latter is the better way to do it (make the script create the other side's groups again), how would I do this?

Sorry for the load of questions, I've avoided this aspect of mirroring the weights in the past because I'm not too clear on its effects in my situation. I'm more than happy to answer any questions for clarification.

Here's the error I'm getting when I Run Script in Blender.
There are three problems currently. 


You seem to have pasted the code into a text-editing programm with has encoded the script, then opened it up in Blender. Most likely the ending of your file was .rtf. As you can see in the text editor of Blender, this introduced numerous characters which we don't want. Right now your script starts with {\rtf\ansi ...
You have to simply open the text editor in Blender, paste the script directly, then press Run Script.
If you really want to edit your script externally, use the ending .txt or better .py (the python extension).
You have replaced the command startswith with beginswith (accidentally probably).
You are not using single quotes ', but other special characters ‘ and ’. The were probably generated by your text-editor as well. Use a simple texteditor instead, such as Blenders builtin text editor.


Here is the corrected script.

import bpy
obj = bpy.context.active_object

vg = obj.vertex_groups[:]
for g in vg:
    if g.name.startswith('b__R'):
        g.name = g.name+'.R'
    if g.name.startswith('b__L'):
        g.name = g.name+'.L'


However, I would suggest more changes.


Using the same script to toggle the ending on and off.
Comparing parts of the name with the slice notation.
E.g. to compare the first four characters: my_name[:4]
Replacing the ending letter with the fourth (fixed) L or R character.


These are a lot of obstacles for a beginner, here is the improved (changed) script, the benefit beeing to only need one.

import bpy

obj = bpy.context.active_object
for g in obj.vertex_groups:
    if g.name[:4] in ['b__R', 'b__L']:
        if g.name[-2:] in ['.R', '.L']:
            g.name = g.name[:-2]
        else:
            g.name = g.name + '.' + g.name[3:4]