2
$\begingroup$

I have a dict of 2d data and I want to generate bones (skeleton), assuming z = 0 for translation and rotation is across z axis in given dict.

data = {"bones":[{"name":"root"},
{"name":"COM","parent":"root","length":35.18,"x":-128.09,"y":160.22},
{"name":"torso","parent":"COM","length":102.78,"x":127.78,"y":-116.67,"rotation":90},
{"name":"armL","parent":"torso","length":76.14,"x":62.96,"y":-39.81,"rotation":-103.29},
{"name":"armR","parent":"torso","length":92.75,"x":68.52,"y":32.4,"rotation":104.14}]}

here is my blender script to iterate on the dict. I can create bones. currently placing new bone on top of each other(almost). what I can handle is, initial head position directly , on x and y coords placing tail position directly via length but

HOW to apply rotation? any ideas

Sample code for current progress..

import json,os,sys
from mathutils import Matrix
from mathutils import Vector
import bpy

def createBaseArmature():
    bpy.ops.object.add(type='ARMATURE',enter_editmode=True,location=(0,0,0))
    ob = bpy.context.object
    return ob
def createBones(amt,bonesData):
    print (str(len(bonesData)) + " bones found")
    for each in bonesData:
        bone = amt.edit_bones.new(each['name'])
        if 'parent' in each:
            bone.parent = amt.edit_bones[each['parent']]
            bone.head = bone.parent.tail
            rot = Matrix.Translation((0,0,0))
            bone.tail = rot * Vector ((0,0,each['length'])) + bone.head
        else:
            bone.head = (0,0,0)
            bone.tail = (0,0,1)

obj = createBaseArmature()
amt = obj.data
createBones(amt,data['bones'])
$\endgroup$
1
$\begingroup$

While you can adjust the head/tail/roll directly, EditBone's have a helper function for this: EditBone.transform, which I think is the most direct answer to your question, just pass a rotation matrix to it.

This is simply a python helper function, see the code.

$\endgroup$

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.