# some functions to manage constraints with python
def add_IK_influence_driver(b, tgt_ob):
    dpl = 'pose.bones["walk"]["left-IK-FK"]'
    dpr = 'pose.bones["walk"]["right-IK-FK"]'
    for cs in b.constraints:
        if 'IK' in cs.name:
            d = b.id_data.driver_add('pose.bones["{}"].constraints["{}"].influence'.format(b.name, cs.name))
            v = d.driver.variables.new()
            v.name = 'var'
            v.type = 'SINGLE_PROP'
            v.targets[0].id = tgt_ob
            if b.name.endswith('R'):
                v.targets[0].data_path = dpr
            else:
                v.targets[0].data_path = dpl

            d.driver.expression = 'var'


def mute_constraints(b, mute=True):
    for cs in b.constraints:
        print(cs.name, 'muted')
        cs.mute = mute

def delete_contraints(b, cstype=False, csname=False):
    '''get a posebone and optionnaly a type or name''' 
    if cstype:#remove this type only
        for cs in b.constraints:
            if cs.type == cstype:
                print('removing', cs.name)
                b.constraints.remove(cs)
    if csname:#remove this name only
        for cs in b.constraints:
            if cs.name == csname:
                print('removing', cs.name)
                b.constraints.remove(cs)               
    if not cstype and not csname:#remove all
        for cs in b.constraints:
            print('removing', cs.name)
            b.constraints.remove(cs)


def add_copy_transform(b, tgt_arm, tgt_bone_name, name='', influence=1):
    '''posebone, tgt-arm is an armature object, subtarget bone name'''
    #add_cp
    cs = b.constraints.new('COPY_TRANSFORMS')
    cs.target = tgt_arm
    cs.subtarget = tgt_bone_name
    cs.influence = influence
    if name:
        cs.name = name
    print("added constraint", cs)

#iterate functions
def cp_transform():
    for b in bpy.context.selected_pose_bones:

def mute_all_const():
    for b in bpy.context.selected_pose_bones:

for b in bpy.context.selected_pose_bones:

    #add_IK_influence_driver(b, b.id_data)

    mute_constraints(b, False)

    # delete_contraints(b)
    #add_copy_transform(b, bpy.context.object, b.name.replace('DEF-', ''), name = 'FK')
    #add_copy_transform(b, bpy.context.object, b.name.replace('DEF-', 'IK_'), name = 'IK', influence=0)