Blender 3D python For Every pose bone add a constraint not working

2k views Asked by At
        selected = bpy.context.selected_pose_bones
        for bone in selected:
            bpy.ops.pose.constraint_add(type='COPY_ROTATION')

every time i run this it the contstraints on the same pose bone not on all the pose bones.

does anyone know how to make it add a constraint on every selected pose bone .

1

There are 1 answers

0
sambler On BEST ANSWER

bpy.ops.pose.constraint_add() is an operator that only effects the active item, in this case a pose bone. While you are looping through the bones in the selection you aren't using the reference to each bone as you loop through.

Instead of using an operator you can manually create the constraints on each specific bone and adjust other parameters as you go.

for bone in bpy.context.selected_pose_bones:
    nc = bone.constraints.new(type='COPY_ROTATION')
    nc.target = bpy.data.objects['Armature']
    nc.subtarget = bone.parent
    nc.influence = 0.5