2016-10-12 3 views
0

나는 두개의 해골 세트 (A와 B)를 가지고 있고, B 조인트에 대한 조인트 세트를 부모 조종하려고한다.Maya python. distance로 parentConstraint

내가 선택한 것은 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

그래도 사용할 수 있지만 나중에 삭제해야하는 새 개체가 만들어집니다. 그것을 메모리에서하는 것이 더 빠를 것입니다. –

관련 문제