1
$\begingroup$

I am using Blender 2.49b, and I have the following hand structure enter image description here

I would like to rotate the bones "Bone.001" to "Bone.025" using values (Quaternion WXYZ) given as input from text files.

Although I read the text files, I am not sure how to set the rotation values of every bone.

...         
scn = Scene.GetCurrent()
arm_ob = scn.objects.active
arm_data = arm_ob.getData()
bones = arm_data.bones.values()

for bone in bones:
    # use bone here
    for handIdx in handIndices:
        pos = dataStore['Bone'][handIdx-1][frameIdx]
        bone.setRotation(pos[0], pos[1], pos[2], pos[3])    
        bone.insertIpoKey(Object.IpoKeyTypes.LOCROTSIZE)

Scene.GetCurrent().update()

setRotation property doesn't work, giving the following error:

AttributeError: 'Bone' object has no attribute 'setRotation'

Could you please help me to find the solution on this manner.

Thanks in advance for your help and consideration.

Best Wishes,

Andreas

PS: If necessary, I can provide the blender file and code.

$\endgroup$
1

1 Answer 1

4
$\begingroup$

Bones have two rotations. One is the rest orientation, and the other is the pose orientation.

The pose orientation is manipulated something like

obj.pose.bones[0].rotation_mode = 'QUATERNION'
obj.pose.bones[0].rotation_quaternion = q

But the rest orientation requires you to be in edit mode, and you have two choices. You can manipulate the head, the tail, and the roll (requires extra math). Or you can set the head and tail to a base state and use functions to transform the bone (and let those functions do the math for you). Here's an excerpt from http://web.purplefrog.com/~thoth/blender/python-cookbook/bone-orientation-demo.html

translate = Vector([0,0,1])
bpy.ops.object.mode_set(mode='EDIT')
bone = obj.data.edit_bones[0] 
bone.head = [0,0,0]
bone.tail = [0,1,0] # Using the Y axis is pretty important, otherwise we can't use the inverse of Q to pose the cube to an unrotated state
bone.transform(q.to_matrix())
bone.translate(translate)

I learned about EditBone.transform from How to rotate an EditBone? is it possible .

I'll just jump to the conclusion that you want to alter the pose. The other weird problem with your code is that it looks like you are setting the orientation of each bone several times (handIndices) and replacing a keyframe each time. Since you are having difficulty updating your code to use the techniques in my examples, here is a rewritten version of your code with a couple of alternative implementations to choose from:

scn = Scene.GetCurrent()
arm_ob = scn.objects.active
bones = arm_ob.pose.bones

if False:
   #more like the original code
   for bone in bones:
      # use bone here
      for handIdx in handIndices:
         # why are we setting the rotation several times for the same bone?
         pos = dataStore['Bone'][handIdx-1][frameIdx]
         bone.rotation_quaternion = pos
         bone.insertIpoKey(Object.IpoKeyTypes.LOCROTSIZE)
else:
   # maybe handIndices is a list of bone indices?
   for handIdx in handIndices:
      pos = dataStore['Bone'][handIdx-1][frameIdx]
      bone = bones[handIdx-1]
      bone.rotation_quaternion = pos
      bone.insertIpoKey(Object.IpoKeyTypes.LOCROTSIZE)

Scene.GetCurrent().update()
$\endgroup$
6
  • $\begingroup$ And then there's blender.stackexchange.com/questions/9318/… $\endgroup$
    – Mutant Bob
    Mar 8, 2016 at 21:39
  • $\begingroup$ Thanks @Mutant Bob. Let me be more specific; I have calculated the quaternion values for each bone on a different application (MATLAB) and saved it on separate text files. Now I just want to set these rotation values on each bone. I can give you my code or my blender file if this helps. $\endgroup$
    – Andreas
    Mar 9, 2016 at 2:58
  • $\begingroup$ Thanks for your code. Unfortunately I am still unable to set the values on the bones.rotation. I get the following error: AtributeError: 'Bone' object has no attribute 'rotation_quaternion' IndentationError: unident does not match ano outer indentation level $\endgroup$
    – Andreas
    Mar 9, 2016 at 19:17
  • $\begingroup$ You can find on the following link my blender file: drive.google.com/file/d/0B9xLA7mxLRwzU2dMUGRyMU4wTk0/…, as well as an example of data (the text files with the rotations) drive.google.com/file/d/0B9xLA7mxLRwzYWFoUWg0dFQzSHc/… $\endgroup$
    – Andreas
    Mar 9, 2016 at 19:29
  • $\begingroup$ You forgot to use bones = arm_ob.pose.bones. Also, your script appears to use an ANCIENT API. To make it work on my 2.76 blender I had to modify it quite a bit: pasteall.org/66151/python . If you need to use the old API, you might want to follow batFINGER's suggestions. $\endgroup$
    – Mutant Bob
    Mar 9, 2016 at 22:03

You must log in to answer this question.

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