0
$\begingroup$

I'm trying to code a script that will copy the rotations of the bones from one rig to another on all the bones

ob = bpy.context.object
altob = bpy.data.objects["Armature.001"]

if (ob.type == 'ARMATURE') & (altob.type == 'ARMATURE'):
    armature = ob.data
    altarmature = altob.data    
    for bone in armature.bones:
        for altbone in altarmature.bones:
            if bone.name == altbone.name:
                #crc = bone.constraints.new('COPY_ROTATION')
                #crc.target = altob
                #crc.subtarget = altbone.name
                print(bone.name + " = " + altbone.name) 

so if I just have the print names of the bones then I get all the bones matching like this:

enter image description here

but with the other code where I try to make a new constraint and set the other rig as the target and the correct bone as the subtarget then I get the message at the top of the screen shot where it tells me the bone has no attribute 'constraints'.

Can someone tell me what I'm doing wrong? I'm using 2.80 (I need this specific version to make this project work)

$\endgroup$

1 Answer 1

1
$\begingroup$

Okay I found a solution. I read that to get reference to pose bones (where the constraint can be set) you need to do it from the object and not the armature, so I adjusted my script to look like this:

import bpy
ob = bpy.context.object
altob = bpy.data.objects["Armature.001"]

if (ob.type == 'ARMATURE') & (altob.type == 'ARMATURE'):
    armature = ob.data
    altarmature = altob.data    
    for bone in ob.pose.bones:
        for altbone in altob.pose.bones:
            if bone.name == altbone.name:
                crc = bone.constraints.new('COPY_ROTATION')
                crc.target = altob
                crc.subtarget = altbone.name
                print(bone.name + " = " + altbone.name)

so instead of setting 'bone' and 'altbone' to the 'armature.bones', I set it to the object 'ob.pose.bones' and 'altob.pose.bones' and it worked like a dream. Just need a piece in here to clear all the constraints before setting new ones, but that's something for another day.

$\endgroup$
1
  • $\begingroup$ and also just realise that 'armature = ob.data' and 'altarmature = altob.data' is unnecessary for this script $\endgroup$ May 7, 2020 at 10:44

You must log in to answer this question.

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