0
$\begingroup$

I'm new to scripting in blender and am trying to select a bone by name using python. Eventually I would like to loop through all the bones of an armature and translate them to a new location in edit mode but for now I am literally just trying to select a bone by name. I have been looking through SE as well as the blender docs but I cannot seem to wrap my head around this. I have found some older answers from 2.7x but these do not seem to work in 2.8. I have also found plenty of answers on how to select objects by name..but not bones and my brain is unable to extrapolate.

Lets say I have an armature with one bone in it: "jBone"...how do I select this and make it active using python in blender 2.8?

----here is some of what I've tried, none of it works:

misc attempt 1:

bpy.ops.armature.select_set(name="jBone")

misc attempt 2:

ob = bpy.data.objects["jBone"]      
bpy.ops.object.select_all(action='DESELECT') 
bpy.context.view_layer.objects.active = ob
ob.select_set(True)
$\endgroup$
1
  • 1
    $\begingroup$ Lol why did my post get a downvote!? So many mean folks on SE. I don't get it. $\endgroup$
    – jnse
    Nov 24, 2019 at 20:34

1 Answer 1

2
$\begingroup$

Find the object first (by name if you wish), then access the armature. The armature is the data of the object. It has an edit_bones property, but this property is only available in edit mode. This means we have to switch to edit mode first.

import bpy

ob = bpy.data.objects['Armature']
armature = ob.data

bpy.ops.object.mode_set(mode='EDIT')

# get specific bone name 'Bone'
myBone = armature.edit_bones['Bone']
#print the position of its head
print(myBone.head)

# loop through all bones
for bone in armature.edit_bones:
    # move its head and tail
    bone.head.x += 1.5
    bone.tail.x += 1.5


bpy.ops.object.mode_set(mode='OBJECT')
$\endgroup$
1
  • $\begingroup$ Thank you so much for your detailed response! This gives me a lot to go on! $\endgroup$
    – jnse
    Nov 24, 2019 at 19:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .