2
$\begingroup$

I have two armature, each has the same bone structure, but the bones' location, length, and rotation is different, how to write python script to modify first armature, make first same with the second?

I want to copy the second Armature's bone position into the first one's pose mode. Not copy in edit mode.

I try these code:

restPoseBone = bpy.data.objects["Armature"].pose.bones["Bip01"]
otherPoseBone = bpy.data.objects["Armature2"].pose.bones["Bip01"]
restPoseBone.length = otherPoseBone.length
restPoseBone.rotation_quaternion = ??

how to calculate the rotation_quaternion for the pose bone?

this is a demo blender file, I want to copy "Armature2" edit mode position to "Armature" pose mode.

testArmature.blend http://pasteall.org/blend/31724

$\endgroup$
  • $\begingroup$ What do you want to calculate? Assigning restPoseBone.rotation_quaternion = otherPoseBone.rotation_quaternion works for me $\endgroup$ – stacker Sep 17 '14 at 6:40
  • $\begingroup$ the two armature only has same structure, but all bones have different edit mode position, I just want to make the second armature's edit mode position as the pose mode position for first armature. $\endgroup$ – liyonghelpme Sep 17 '14 at 7:53
1
$\begingroup$

The following python code copies the bone configuration from the active object to all selected armatures. You do not need to bother with rotation matrix here because the bones are defined in local space, thus the global rotation of the armatures does not need to be taken into account:

import bpy

# Assume, that context.object is an armature
context = bpy.context
scene   = context.scene
source  = context.object

# Targets are all other selected armatures
targets = [a for a in context.selected_objects if a != source and a.type=='ARMATURE']

# Store the bone data of source:
bpy.ops.object.mode_set(mode='EDIT')
bone_store = []
for ebone in source.data.edit_bones:
    bone_store.append([ebone.name, ebone.head.copy(), ebone.tail.copy(), ebone.roll])
bpy.ops.object.mode_set(mode='OBJECT')


for target in targets:
    scene.objects.active = target
    bpy.ops.object.mode_set(mode='EDIT')

    # Transfer the bones to the other armature.
    # The following works because the bone data is defined in local space:

    ebones = target.data.edit_bones
    for bone_data in bone_store:
        bid =  bone_data[0]
        if bid in ebones:
            ebone      = ebones[bid]
            ebone.head = bone_data[1].copy()
            ebone.tail = bone_data[2].copy()
            ebone.roll = bone_data[3]

    # Note: You might also want to copy constraints

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

scene.objects.active = source

And here is a snippet that copies only the pose from one armature to the other:

import bpy

# Assume, that context.object is an armature
context = bpy.context
scene   = context.scene
source  = context.object

# Targets are all other selected armatures
targets = [a for a in context.selected_objects if a != source and a.type=='ARMATURE']

bpy.ops.object.mode_set(mode='POSE')
bpy.ops.pose.select_all(action="SELECT")
bpy.ops.pose.copy()

for target in targets:
    scene.objects.active = target
    bpy.ops.object.mode_set(mode='POSE')
    bpy.ops.pose.paste()
$\endgroup$
  • $\begingroup$ if I don't want to change edit bones' head, tail, roll, I want to change pose bone's data , how can i do that? $\endgroup$ – liyonghelpme Sep 17 '14 at 7:40
  • $\begingroup$ oops, my fault. Copy the pose is implemented in Blender: select your source object, go to pose mode, select all bones, then press CTRL C, switch over to the target armature, enter pose mode, then press CTRL V. I add another snippet for python in a moment. $\endgroup$ – Gaia Clary Sep 17 '14 at 7:46
  • $\begingroup$ I can not find how to download the blendfile from that page. Could you post it to pasteall.org/blend instead ? $\endgroup$ – Gaia Clary Sep 17 '14 at 11:04
  • $\begingroup$ yes, this file thanks. pasteall.org/blend/31724, I have find a method to copy one armature edit mode bone position to another armature pose mode. I will paste the code here. $\endgroup$ – liyonghelpme Sep 22 '14 at 8:35
  • $\begingroup$ I tried the above code to copy from one armature in some rest position to a different posed armature also in the rest position. But, I see no changes, the pose remains the same. The pose is not changed at all. And the above script throws errors, I think python is not happy with spaces or tabs introduced by extra formatting. $\endgroup$ – Arise Jan 13 '17 at 13:21

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.