2

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?

Ascalon
  • 6,579
  • 8
  • 55
  • 121
  • 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 ) – Chebhou Apr 06 '15 at 01:35
  • 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. – Ascalon Apr 06 '15 at 01:46
  • 1
    check this page it has all what you need – Chebhou Apr 06 '15 at 01:58

1 Answers1

1

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!

Duarte Farrajota Ramos
  • 59,425
  • 39
  • 130
  • 187
Frederick
  • 159
  • 12
  • 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.

    – Frederick May 05 '19 at 18:07
  • gotta be out of edit mode for this to work. and bpy.ops.object.armature_add() will add an armature, not a bone – Phil Jun 05 '22 at 13:50