# Renaming bones with Python

I often use Blender to port models between game engines for personal-use game mods, which means I frequently have to rename the bones in accordance with the rules of different game engines.

I already have a script that works perfectly for renaming vertex groups (should I import a mesh without its skeleton)...

import bpy

name_list = [
# old name - new name
['Bip01 Neck1','Bip01_Neck1'],
## Rest of the list
## Each version of the script has a different list, specific to the game engines I'm porting between.
['Bip01 Finger41.R', 'Bip01_R_Finger41'],
['Bip01 Finger42.R', 'Bip01_R_Finger42']
]

v_groups = bpy.context.active_object.vertex_groups
for n in name_list:
if n[0] in v_groups:
v_groups[n[0]].name = n[1]


... select desired object, run script, all recognised vertex groups get renamed.

However, my attempts to adapt a version that will rename bones instead...

bone_list = bpy.context.visible_bones
for n in name_list:
if n[0] in bone_list:
bone_list[n[0]].name = n[1]


... simply has no effect. (I have also tried with the bpy.context.editable_bones, bpy.context.selected_bones and bpy.context.selected_editable_bones contexts).

There's no errors on the console (although I can definitely provoke errors - by trying to run the script in the wrong 3D view mode, misspelling commands, etc - so it's not that the script isn't even trying to run), and the bone names in the armature definitely match the names in the script's list (I have now copy-pasted some names to check, but the Vertex group version of the script definitely works to rename the matched groups on the associated meshes).

I'm out of my element when it comes to Python in Blender, so I'm really quite stuck as to why this is simply failing to work. Words of wisdom would be appreciated.

Probably easiest to use the armature objects pose bone collection.

import bpy
context = bpy.context
obj = context.object

namelist = [("Bone", "Head")]

for name, newname in namelist:
# get the pose bone with name
pb = obj.pose.bones.get(name)
# continue if no bone of that name
if pb is None:
continue
# rename
pb.name = newname

• Sorry for being slightly slow getting back to you, but thanks - that's worked a charm. That should save me a heck of a lot of effort (and a great many errors) from having to do the whole thing manually every time. – RagnarokEOTW Dec 21 '16 at 20:02