2
$\begingroup$

I want to generate several bones on vertices with python. I have figured out how to make an Armature Object, and how to make edit bones within it. Now I need to get these bones onto their appropriate vertices. Can I create them on certain points in the first place? Or can I somehow move them there?

Do I use head, head_local, translate, matrices, or something else?

$\endgroup$
  • $\begingroup$ if bones are connected you have to move head and tail with head_local tail_local to vertex coordinates (given that armature and object has the same loc-rot-scale ) $\endgroup$ – Chebhou Apr 6 '15 at 1:35
  • $\begingroup$ I am still figuring out how to even connect them. I am making a bone on every 4th point down an edge loop. I need to make the bones in the right place, and then have them be connected to each other. $\endgroup$ – Drudge Apr 6 '15 at 1:46
  • 1
    $\begingroup$ check this page it has all what you need $\endgroup$ – Chebhou Apr 6 '15 at 1:58
1
$\begingroup$

Using Bmesh to get the required coordinates.

import bpy, bmesh #first import bpy and bmesh
obj = bpy.context.active_object

if obj.mode == 'EDIT':
    bm = bmesh.from_edit_mesh(obj.data)
    vertices = bm.verts

else:
    vertices = obj.data.vertices

verts = [obj.matrix_world * vert.co for vert in vertices] #this should get your coordinates as tuples.

plain_verts = [vert.to_tuple() for vert in verts]
for I in range(0,482):#where you'd like to set your bones i chose from 0 - 482 because i wanted to fill a sphere.
    print(plain_verts[I])#so you can see your data
    bpy.context.scene.cursor_location = (plain_verts[I])#move the cursor to the vertices position.
    bpy.ops.object.armature_add()#this uses blenders menu method to add the bone, at the cursor.

After like 3 minutes of searching online I was able to find this, and by reading the commands in Blender easily figured out how to create a bone.

Is this not what you're looking for?

Or did you want to know how to join and rotate bones?

because that would be

bpy.data.objects['selectedbonesbones'].select = True
bpy.context.scene.objects.active = bpy.data.objects["selectedbonesbones"]

and look here to rotate the bones in pose mode.

If you want to rotate in object mode it should be something like

bpy.data.objects['armature'].rotation_euler[0] = 1
bpy.data.objects['armature'].rotation_euler[1] = 2
bpy.data.objects['armature'].rotation_euler[2] = 3

If you want to rotate in edit mode, well I haven't found anything very useful but you can try this, line 335 to 364. Tell me if it works!

$\endgroup$
  • $\begingroup$ sidenote you can do something like: import bpy bpy.data.objects["Armature.042"].pose.bones["Bone.006"].location[0]=0 for pose mode in bpy. $\endgroup$ – Frederick May 5 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.