Maya python. parentConstraint by distance
<p>I have two sets of skeletons (A and B), and trying to parent constraint A set of joints to B joints.</p>

<p>What I did was put A selection into A array and B selection to B array.
but how can I proceed parent constraint A to B by distance, which the distance is 0 basically so the toe joint doesn't hook up with fingers.
something like bellow. </p>

<p>I'd appreciate if some one can advise me.</p>

<pre><code>firstSel = []
secondSel = []

def firstSelection(*args):

    sel = mc.select(hi = True)
    joints = mc.ls(sl=True, type = "joint")
    firstSel.append(joints)
    print "this is first selection"


def secondSelection(*args):

    tsmgRoot = mc.select(hi = True)
    tsmgJoints = mc.ls(sl=True, type = "joint")
    secondSel.append(tsmgJoints)
</code></pre>

<p>Here's an example of how you can constrain objects from one list to another by their world positions. There may be smarter ways of doing it, like adding the values to a <code>dict</code> then sorting it, but this feels more readable.</p>

<pre><code>import math

# Add code here to get your two selection lists

# Loop through skeleton A
for obj in first_sel:
    closest_jnt = None # Name of the closest joint
    closest_dist = 0 # Distance of the closest joint

    obj_pos = cmds.xform(obj, q=True, ws=True, t=True)

    # Loop through skeleton B to find the nearest joint
    for target in second_sel:
        target_pos = cmds.xform(target, q=True, ws=True, t=True)

        # Calculate the distance between the 2 objects
        dist = math.sqrt((target_pos[0]-obj_pos[0])**2 + 
                         (target_pos[1]-obj_pos[1])**2 + 
                         (target_pos[2]-obj_pos[2])**2)

        # If this is the first object we loop through, automatically say it's the closest
        # Otherwise, check to see if this distance beats the closest distance
        if closest_jnt is None or dist &lt; closest_dist:
            closest_jnt = target
            closest_dist = dist

    cmds.parentConstraint(closest_jnt, obj, mo=True)
</code></pre>

<p>Also as a side note, no need to do <code>firstSel.append(joints)</code> when you're getting your selection (you're actually adding a list to a list!). You can either just assign it directly like <code>firstSel = mc.ls(sl=True, type = "joint")</code>, or if the list already exists with some items and you want to add to it, you can use <code>firstSel.extend(joints)</code></p>

python maya