cwisbg

Python utilities

May 21st, 2013
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.00 KB | None | 0 0
  1. import math
  2. def getDistance(a, b): # d = getDistance(a,b)
  3.     pointA = a.getTranslation()
  4.     pointB = b.getTranslation()
  5.     d = math.sqrt( math.pow( pointA[0] - pointB[0], 2) + math.pow( pointA[1] - pointB[1], 2) + math.pow( pointA[2] - pointB[2], 2) )
  6.     return d
  7. #__________________________________________
  8. import time
  9. start = time.clock()
  10. elapsed = (time.clock() - start)
  11. print "done in:", elapsed,
  12.  
  13.  
  14. #__________________________________________
  15.  
  16. # Snapper
  17. # cwisbg.com
  18. from pymel.core import *
  19. sl = selected()
  20. tp =pointConstraint(sl[1], sl[0])
  21. to = orientConstraint(sl[1], sl[0])
  22. delete(tp)
  23. delete(to)
  24. select(cl=1)
  25. select(sl[0])
  26.  
  27. #__________________________________________
  28.  
  29. # Locator at pivot
  30. # cwisbg.com
  31. from pymel.core import *
  32. currentSceneScale = currentUnit(q=1, l=1)
  33. tempSceneScale = currentUnit(l = "cm")
  34. def getPiv(s, type):
  35.     if type == "transform":
  36.         papCheck = listRelatives(s,p=1)
  37.         if papCheck:
  38.             try:
  39.                 parent(s,w=1)
  40.                 rot = s.getRotation()
  41.                 parent(s, papCheck)
  42.             except:
  43.                 #print "could not get Rotation"
  44.                 rot = s.getRotation()
  45.                 #rot = [0,0,0]
  46.                 pass
  47.         else:
  48.             rot = s.getRotation()
  49.     else:
  50.         rot = [0,0,0]        
  51.     select(s)
  52.     setToolTo('Move')
  53.     piv = manipMoveContext('Move', q=True, p=True,m=2)
  54.     return piv, rot
  55. sl = selected()
  56. if sl:
  57.     type = nodeType(sl[0])
  58.     if type == "mesh":
  59.         t , r = getPiv(sl, type)
  60.         l = spaceLocator(p = (0,0,0), n = "{0}_saveLocation".format(sl[0]))
  61.         move(l, t, ws=1)
  62.         rotate(l, r)
  63.     else:    
  64.         for s in sl:
  65.             type = nodeType(s)
  66.             t , r = getPiv(s, type)
  67.             l = spaceLocator(p = (0,0,0), n = "{0}_saveLocation".format(s))
  68.             move(l, t, ws=1)
  69.             rotate(l, r)
  70. else:            
  71.     l = spaceLocator(p = (0,0,0))            
  72. currentUnit(l = currentSceneScale)
  73. print "done"
  74.  
  75. #__________________________________________
  76.  
  77. # Retarget Buffer    
  78. from pymel.core import *
  79. def getPiv(s):
  80.     currentSceneScale = currentUnit(q=1, l=1)
  81.     tempSceneScale = currentUnit(l = "cm")
  82.     select(s)
  83.     setToolTo('Move')
  84.     piv = manipMoveContext('Move', q=True, p=True,m=2)
  85.     currentUnit(l = currentSceneScale)  
  86.     return piv
  87. def makeBufferGrp():
  88.     sl = selected()
  89.     for s in sl:    
  90.         name = s.split("|")[-1]
  91.         grpCheck = ls("{0}_bufferGrp".format(name))
  92.         if grpCheck:
  93.             p = listRelatives(grpCheck,p=1)
  94.             c = listRelatives(grpCheck,c=1)
  95.             if p:
  96.                 parent(c,p)
  97.             else:
  98.                 parent(c, w=1)
  99.             delete(grpCheck)
  100.         pap = listRelatives(s,p=1)
  101.         if pap:
  102.             g = group(n="{0}_bufferGrp".format(name),em=1,p = pap[0])
  103.             #g = group(n="{0}_bufferGrp".format(name),em=1,w=1)
  104.             #makeIdentity( g, a=1,t=1,r=1,s=1, n=0)
  105.            
  106.         else:
  107.            
  108.             g = group(n="{0}_bufferGrp".format(name),em=1,w=1)
  109.             print "freezing",g
  110.             #makeIdentity( g, a=1,t=1,r=1,s=1, n=0)
  111.         pc = parentConstraint(s,g)
  112.         delete(pc)
  113.         makeIdentity( g, a=1,t=1,r=1,s=1, n=0)
  114.         parent(s, g)
  115.         #parent(g, pap[0])      
  116. def removeBufferGrp():
  117.     sl = selected()
  118.     for s in sl:    
  119.         name = s.split("|")[-1]
  120.         grpCheck = ls("{0}_bufferGrp".format(name))
  121.         if grpCheck:
  122.             p = listRelatives(grpCheck,p=1)
  123.             c = listRelatives(grpCheck,c=1)
  124.             if p:
  125.                 parent(c,p)
  126.             else:
  127.                 parent(c, w=1)
  128.             delete(grpCheck)              
  129. def makeConnections():
  130.     fsl = flipSl.getValue()
  131.     dc = directConnectBox.getValue()
  132.     m = mOffset.getValue()
  133.     fAndT =  parentConstraintBox.getValue()
  134.     sl = selected()
  135.     if len(sl) == 2:
  136.         if fsl:
  137.             target = sl[1]
  138.             ctrl = sl[0]
  139.             bufferGrp = listRelatives(sl[0], p=1)
  140.             name = sl[0].split("|")[-1]
  141.  
  142.         else:
  143.             target = sl[0]
  144.             ctrl = sl[1]
  145.             bufferGrp = listRelatives(sl[1], p=1)[0]
  146.             name = sl[1].split("|")[-1]
  147.            
  148.         oCheck = ls("{0}_orientConstraint".format(name))
  149.        
  150.         if not dc:
  151.             target = listRelatives(target, p=1)[0]
  152.            
  153.         if oCheck:
  154.             delete(oCheck)
  155.  
  156.         if m:
  157.             offsetObject = ctrl
  158.         else:
  159.             offsetObject = target
  160.         saveOffset = spaceLocator(p = (0,0,0))
  161.         pc = parentConstraint(offsetObject, saveOffset)
  162.         delete(pc)  
  163.                    
  164.         if fAndT:
  165.             o = parentConstraint(target, bufferGrp, n = "{0}_parentConstraint".format(name), mo = m)
  166.         else:
  167.             #___________________________________X_____________________________
  168.             xx = xBoxX.getValue()
  169.             xy = xBoxY.getValue()
  170.             xz = xBoxZ.getValue()
  171.             bufferGrp = bufferGrp[0]
  172.             print target, bufferGrp, ctrl
  173.             if xx:
  174.                 target.rotateX >> bufferGrp.rotateX        
  175.                 print "connect x to x"
  176.             if xy:
  177.                 target.rotateX >> bufferGrp.rotateY  
  178.                 print "connect x to y"
  179.             if xz:
  180.                 target.rotateX >> bufferGrp.rotateZ  
  181.                 print "connect x to z"
  182.             #___________________________________Y_____________________________
  183.             yx = yBoxX.getValue()
  184.             yy = yBoxY.getValue()
  185.             yz = yBoxZ.getValue()
  186.             if yx:
  187.                 target.rotateY >> bufferGrp.rotateX        
  188.             if yy:
  189.                 target.rotateY >> bufferGrp.rotateY  
  190.             if yz:
  191.                 target.rotateY >> bufferGrp.rotateZ    
  192.             #___________________________________Y_____________________________
  193.             zx = zBoxX.getValue()
  194.             zy = zBoxY.getValue()
  195.             zz = zBoxZ.getValue()
  196.             if zx:
  197.                 target.rotateZ >> bufferGrp.rotateX        
  198.             if zy:
  199.                 target.rotateZ >> bufferGrp.rotateY  
  200.             if zz:
  201.                 target.rotateZ >> bufferGrp.rotateZ
  202.                                          
  203.             #target.rotate >> bufferGrp[0].rotate
  204.             o = orientConstraint(saveOffset, ctrl, n = "{0}_orientConstraint".format(name), mo = 0)
  205.             delete(o)
  206.         delete(saveOffset)
  207.     setAttr(ctrl+".translateX",0)
  208.     setAttr(ctrl+".translateY",0)
  209.     setAttr(ctrl+".translateZ",0)
  210.     setAttr(ctrl+".rotateX",0)
  211.     setAttr(ctrl+".rotateY",0)
  212.     setAttr(ctrl+".rotateZ",0)
  213.     else:
  214.         print "please select stuff"
  215. def unMakeConnections():
  216.     sl = selected()
  217.     for s in sl:  
  218.         ctrl = listRelatives(s, p=1)[0]
  219.         print ctrl
  220.         disconnectAttr(ctrl.rotateX)
  221.         disconnectAttr(ctrl.rotateY)
  222.         disconnectAttr(ctrl.rotateZ)
  223.         name = s.split("|")[-1]
  224.         pCheck = ls("{0}_parentConstraint".format(name))
  225.         oCheck = ls("{0}_orientConstraint".format(name))
  226.         if oCheck:
  227.             delete(oCheck)
  228.         if pCheck:
  229.             delete(pCheck)  
  230.         rotate(ctrl, 0,0,0)
  231.            
  232.            
  233.                      
  234. def flipSelection():
  235.     x = flipSl.getValue()
  236.     if not x:
  237.         slTip.setLabel("Target - Control")
  238.     else:
  239.         slTip.setLabel("Control - Target")
  240.     print "flipping selection", x  
  241. def bakeAnim():
  242.     print "baking animation"
  243.     sl = selected()
  244.     if len(sl) < 1:
  245.         print "select Something"
  246.     else:
  247.         bakeList = []
  248.         for s in sl:
  249.             name = s.split("|")[-1:][0]
  250.             grpCheck = ls("{0}_bufferGrp".format(name))
  251.             bakeList.append(grpCheck)
  252.         tStart = playbackOptions(q=1,ast=1)
  253.         tEnd = playbackOptions(q=1,aet=1)
  254.         bakeResults(bakeList, simulation=1, t = (tStart,tEnd),at="rotate",sb = 1,dic = 1,preserveOutsideKeys=1,sparseAnimCurveBake=0,
  255.         removeBakedAttributeFromLayer=0, bakeOnOverrideLayer=1,controlPoints =0, s=1)
  256.         print "done baking animation"  
  257. def bakeJointAnim():
  258.     print "baking animation"
  259.     sl = selected()
  260.     if len(sl) < 1:
  261.         print "select Something"
  262.     else:
  263.         bakeList = []
  264.         tStart = playbackOptions(q=1,ast=1)
  265.         tEnd = playbackOptions(q=1,aet=1)
  266.         bakeResults(sl, simulation=1, t = (tStart,tEnd),at="rotate",sb = 1,dic = 1,preserveOutsideKeys=1,sparseAnimCurveBake=0,
  267.         removeBakedAttributeFromLayer=0, bakeOnOverrideLayer=1,controlPoints =0, s=1)
  268.         print "done baking animation"  
  269.      
  270. def selectOnlyJoints():
  271.     sl = selected()
  272.     c = sl[0]
  273.     while c:
  274.         c = listRelatives(c,c=1)
  275.         for child in c:
  276.             if nodeType(child) == "joint":
  277.                 select(child, add=1)  
  278.  
  279. def XsetCheckBoxesX():
  280.     xBoxY.setValue(0)
  281.     xBoxZ.setValue(0)
  282. def XsetCheckBoxesY():
  283.     xBoxX.setValue(0)
  284.     xBoxZ.setValue(0)                    
  285. def XsetCheckBoxesZ():
  286.     xBoxY.setValue(0)
  287.     xBoxY.setValue(0)                
  288. def YsetCheckBoxesX():
  289.     yBoxY.setValue(0)
  290.     yBoxZ.setValue(0)
  291. def YsetCheckBoxesY():
  292.     yBoxX.setValue(0)
  293.     yBoxZ.setValue(0)                    
  294. def YsetCheckBoxesZ():
  295.     yBoxY.setValue(0)
  296.     yBoxY.setValue(0)                
  297. def ZsetCheckBoxesX():
  298.     zBoxY.setValue(0)
  299.     zBoxZ.setValue(0)
  300. def ZsetCheckBoxesY():
  301.     zBoxX.setValue(0)
  302.     zBoxZ.setValue(0)                    
  303. def ZsetCheckBoxesZ():
  304.     zBoxY.setValue(0)
  305.     zBoxY.setValue(0)  
  306.    
  307. def tester():
  308.     sl = selected()
  309.     target = sl[0]
  310.     bufferGrp = sl[1]
  311.     xx = xBoxX.getValue()
  312.     xy = xBoxY.getValue()
  313.     xz = xBoxZ.getValue()
  314.     if xx:
  315.         target.rotateX >> bufferGrp.rotateX        
  316.         print "connect x to x"
  317.     if xy:
  318.         target.rotateX >> bufferGrp.rotateY  
  319.         print "connect x to y"
  320.     if xz:
  321.         target.rotateX >> bufferGrp.rotateZ  
  322.         print "connect x to z"            
  323.    
  324.          
  325. from pymel.core import *
  326. x = 0
  327. win = window(title= "Re-Target Tool", s=0)
  328. mainLayout = rowColumnLayout(numberOfRows=4)
  329. bufferGrpBtn = frameLayout(l = "Buffer Groups", cll=1, cl=1, p = mainLayout )
  330. makeBufferGrpBtn = button(bgc = [1,.9,.0], l="Make Buffer Grp", command=Callback(makeBufferGrp),p = bufferGrpBtn)
  331. removeBufferGrpBtn = button(bgc = [.1,.3,.1], l="Remove Buffer Grp", command=Callback(removeBufferGrp),p = bufferGrpBtn)
  332. buttonLayout = frameLayout(l = "Connect Targets", p = mainLayout, w= 200,cll=1,cl=1 )
  333. flipSl = checkBox(l = "Flip Selection Order", cc = Callback(flipSelection), p = buttonLayout,v=1)
  334. slTip = text(l = "Control - Target", p = buttonLayout)
  335.  
  336. mOffset = checkBox(l = "Maintain Offset", p = buttonLayout,v=1)
  337. parentConstraintBox = checkBox(l = "Transform and Rotation", p = buttonLayout, v=1)
  338. directConnectBox = checkBox(l = "Direct Connection", p = buttonLayout, v=1)
  339.  
  340.  
  341. rotationOrderLayout = frameLayout(l = "Rotation Order Remapping", cll=1, cl=1)
  342.  
  343. #xBox = checkBox(l = "X To", p = rotationOrderLayout, v=1)
  344.  
  345. xOrderLayout = rowColumnLayout(nr = 1, p = rotationOrderLayout )
  346. xOrderTxt = text(bgc = [0.4,0.08,0.08], l = "  X  ", p = xOrderLayout)
  347. xBoxX= checkBox(l = "X", p = xOrderLayout, v=1, cc = Callback(XsetCheckBoxesX))
  348. xBoxY= checkBox(l = "Y", p = xOrderLayout, v=0, cc = Callback(XsetCheckBoxesY))
  349. xBoxZ= checkBox(l = "Z", p = xOrderLayout, v=0, cc = Callback(XsetCheckBoxesZ))
  350.  
  351. yOrderLayout = rowColumnLayout(nr = 1, p = rotationOrderLayout )
  352. yOrderTxt = text(bgc = [.1,.6,0], l = "  Y  ", p = yOrderLayout)
  353. yBoxX= checkBox(l = "X", p = yOrderLayout, v=0, cc = Callback(YsetCheckBoxesX))
  354. yBoxY= checkBox(l = "Y", p = yOrderLayout, v=1, cc = Callback(YsetCheckBoxesY))
  355. yBoxZ= checkBox(l = "Z", p = yOrderLayout, v=0, cc = Callback(YsetCheckBoxesZ))
  356.  
  357. zOrderLayout = rowColumnLayout(nr = 1, p = rotationOrderLayout )
  358. zOrderTxt = text(bgc = [0,0,1], l = "  Z  ", p = zOrderLayout)
  359. zBoxX= checkBox(l = "X", p = zOrderLayout, v=0, cc = Callback(ZsetCheckBoxesX))
  360. zBoxY= checkBox(l = "Y", p = zOrderLayout, v=0, cc = Callback(ZsetCheckBoxesY))
  361. zBoxZ= checkBox(l = "Z", p = zOrderLayout, v=1, cc = Callback(ZsetCheckBoxesZ))
  362.  
  363. connect = button(bgc = [0,.7,.2], l="Connect to Target", command=Callback(makeConnections),p = buttonLayout)
  364. text(l = "Select Joint and", p = buttonLayout, w= 200)
  365. disconnect = button(bgc = [.7,.1,0],l="Disconnect Target", command=Callback(unMakeConnections),p = buttonLayout)
  366. bakeLayout = frameLayout(l = "Bake Animation", p = mainLayout, w= 200,cll=1,cl=1 )
  367. text(l = "Select Root Joint and", p = bakeLayout, w= 200)
  368. selectBakeJoints = button(bgc = [1,.6,.0] ,l="Select Joint Hierarchy", command=Callback(selectOnlyJoints),p=bakeLayout,w= 200)
  369. bakeGrpAnimation = button(bgc = [.1,.3,1],l="Bake Animation to Group", command=Callback(bakeAnim),p=bakeLayout)
  370. bakeGrpAnimation = button(l="Test Button", command=Callback(tester),p=mainLayout)
  371. win.show()
  372.  
  373.  
  374.  
  375. #__________________________________________
  376.  
  377. #__________________________________________
Advertisement
Add Comment
Please, Sign In to add comment