# How can I script moving bone constraints up the constraints stack?

I'm running into walls trying to reorder the bone constraints stack on blender 2.78.

I have an armature for which I want to re-order bone constraints on every bone. The way to do this seems to be to use

bpy.ops.constraint.move_up(constraint=name, owner='BONE')


Where name is the name of the constraint.

I'm assuming I need to be in pose mode and I need to select the bone I want to operate on, so:

bpy.ops.object.mode_set(mode = 'POSE')
bone = bpy.context.active_object.pose.bones['the_bone_i_want']
bone.bone.select = True
bpy.ops.constraint.move_up(constraint=name, owner='BONE')


This produces a RuntimeError: Operator bpy.ops.constraint.move_up.poll() Context missing 'constraint'

OK, so I'm in the wrong context. Why? Which context has the necessary constraint?

I also thought about changing the active area to the properties beforehand, like this:

bpy.context.area.type = 'PROPERTIES'


This does change the active area, but doesn't get rid of the error message.

Can anyone point out what I'm doing wrong, or give me a way to reorder bone constraints that works?

I figured it out:

import bpy

name='Secondary Controller'
bone_name = 'ConstraintHolder'

bone = bpy.context.active_object.pose.bones[bone_name]
bpy.context.active_object.data.bones.active = bone.bone
my_context = bpy.context.copy()
my_context["constraint"] = bone.constraints[name]

bpy.ops.constraint.move_up(my_context, constraint=name, owner='BONE')


Seems like I need to add the 'constraint' key to the context myself, and pass in the constraint I want to work on. I also need to continue passing the name of the constraint to the operator. Doesn't make any sense, but it works.

Thanks to Campell Barton: https://developer.blender.org/T39891