2016-10-12 21 views
0

私はスケルトンの2つのセット(AとB)を持っていて、Bジョイントへのジョイントセットのペアレント制約を試しています。Maya Python。 parentConstraint by distance

私が行ったことは、A配列に選択を入れ、B配列にB選択を入れました。 しかし、私は親制約Aを距離Bまで進めることができます。距離は0で、基本的につま先のつまみは指で縛られません。 次のようなものです。

私に助言を与えることができれば感謝します。

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) 
+0

可能な複製http://stackoverflow.com/questions/35833586/batch -constraining-objects-feathers-to-a-wing – UKDP

答えて

0

ここでは、ワールド位置によってオブジェクトをあるリストから別のリストに拘束する方法の例を示します。 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)

+0

dist = cmds.distanceDimension(sp = obj_pos、ep = target_pos) –

+0

はい、これは後で削除する必要がある新しいオブジェクトを作成します。それをメモリで行うほうが速いでしょう。 –