'Bone' object has no attribute 'constraints'

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:

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)

1 Answer

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.

• and also just realise that 'armature = ob.data' and 'altarmature = altob.data' is unnecessary for this script – Reynard Droste May 7 at 10:44