ここでは、ワールド位置によってオブジェクトをあるリストから別のリストに拘束する方法の例を示します。 dict
に値を追加してソートするなど、よりスマートな方法があるかもしれませんが、これはより読みやすく感じます。
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 < closest_dist:
closest_jnt = target
closest_dist = dist
cmds.parentConstraint(closest_jnt, obj, mo=True)
また、サイドノート、あなたの選択を取得しているときfirstSel.append(joints)
を行う必要はありませんように(あなたが実際にリストにリストを追加しています!)。 firstSel = mc.ls(sl=True, type = "joint")
のように直接割り当てることもできますし、リストに既にいくつかのアイテムがあり、それに追加したい場合は、追加することができます。firstSel.extend(joints)
可能な複製http://stackoverflow.com/questions/35833586/batch -constraining-objects-feathers-to-a-wing – UKDP