2016-10-12 8 views
0

Ich habe zwei Sätze von Skeletten (A und B), und versuchen, Eltern-Constraint Eine Reihe von Gelenken zu B-Gelenke.Maya Python. ParentConstraint nach Entfernung

Was ich getan habe, wurde eine Auswahl in A-Array und B-Auswahl in B-Array gesetzt. aber wie kann ich Elternteil Constraint A nach B durch Entfernung, die der Abstand ist 0 im Grunde, so dass die Zehe Gelenk nicht mit den Fingern einhaken gehen. so etwas wie Balg.

Ich würde mich freuen, wenn jemand mich beraten kann.

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

Mögliche Duplikat von http://stackoverflow.com/questions/35833586/batch verwenden -constraining-objects-feathers-to-a-wing – UKDP

Antwort

0

Hier ist ein Beispiel dafür, wie Sie Objekte aus einer Liste in einer anderen durch ihre Welt Positionen einschränken können. Es kann klügere Wege geben, es zu tun, wie das Hinzufügen der Werte zu einem dict dann sortieren, aber das fühlt sich lesbarer an.

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) 

Auch als eine Randnotiz, keine Notwendigkeit firstSel.append(joints) zu tun, wenn Sie Ihre Auswahl, wenn es darum (Sie tatsächlich eine Liste in eine Liste hinzuzufügen!). Sie können es entweder direkt zuweisen wie firstSel = mc.ls(sl=True, type = "joint"), oder wenn die Liste bereits mit einigen Elementen vorhanden ist und Sie hinzufügen möchten, können Sie firstSel.extend(joints)

+0

Sie können auch dist = cmds.distanceDimension (sp = obj_pos, ep = target_pos) verwenden –

+0

Ja, aber dies erstellt ein neues Objekt, das Sie später löschen müssen. Es wäre schneller, es im Speicher zu tun. –