cwisbg

Rando/SurfaceSnap

Jun 3rd, 2014
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.07 KB | None | 0 0
  1. # Rando
  2. # cwisbg.com
  3. import random as r
  4. from pymel.core import *
  5. def snapToSurface():
  6.    
  7.     # select list, then snap to object
  8.     sl = selected()  
  9.     startL = spaceLocator(n="startL")
  10.     endL = spaceLocator(n="endL")
  11.     cp = createNode("closestPointOnMesh",n="snapperCheck")
  12.     snapList = sl
  13.     snapto = snapSurfaceMesh
  14.     snapto.getShape().worldMatrix[0] >> cp.inputMatrix
  15.     startL.translate >> cp.inPosition
  16.     cp.position >> endL.translate
  17.     snapto.getShape().worldMesh[0] >> cp.inMesh
  18.     for s in snapList:
  19.         st = s.getTranslation()
  20.         move(startL, st)
  21.         et = endL.getTranslation()
  22.         move(s,et)
  23.     delete(cp,startL,endL)
  24.     alignToSurface()
  25.  
  26.     """  
  27.    sl = selected()  
  28.    startL = spaceLocator(n="startL")
  29.    endL = spaceLocator(n="endL")
  30.    cp = createNode("closestPointOnMesh",n="snapperCheck")
  31.    snapList = sl
  32.    snapto = snapSurfaceMesh
  33.    snapto.getShape().worldMatrix[0] >> cp.inputMatrix
  34.    startL.translate >> cp.inPosition
  35.    cp.position >> endL.translate
  36.    snapto.getShape().worldMesh[0] >> cp.inMesh
  37.    for s in snapList:
  38.        st = s.getTranslation()
  39.        sr = s.getRotation()
  40.        move(startL, st)
  41.        et = endL.getTranslation()
  42.        move(s,et)
  43.        tempGrp = ls("tempAlignToSurface")
  44.        if not tempGrp:
  45.            tempGrp = group(em=1,n = "tempAlignToSurface")
  46.        move(tempGrp, st)
  47.        sPapa = listRelatives(s, p=1)
  48.        parent(s,tempGrp)
  49.        nc = normalConstraint(snapto, tempGrp, aim= [0,1,0],u= [0,1,0],wut = "vector",wu =[0,1,0])
  50.        if sPapa:
  51.            parent(s,sPapa)
  52.        else:
  53.            parent(s,w=1)
  54.        delete(nc, tempGrp)
  55.    delete(cp,startL,endL)
  56.    """
  57.     select(sl)
  58. def alignToSurface():
  59.     alignCheck = alignToSurfaceBox.getValue()
  60.     sl = selected()
  61.     for s in sl:
  62.         if alignCheck:
  63.             nc = normalConstraint(snapSurfaceMesh, s, aim= [0,1,0],u= [0,1,0],wut = "vector",wu =[0,1,0])
  64.             delete(nc)
  65.         else:
  66.             pass
  67.            
  68.  
  69.  
  70.  
  71. def checkMe():
  72.     boxValue = checkBox1.getValue()
  73.     print "Checked", boxValue
  74. def buttonPressed():
  75.     cbScale = checkBoxScale.getValue()
  76.     cbTrans = checkBoxTranslate.getValue()
  77.     cbRot = checkBoxRotate.getValue()
  78.    
  79.     cbX = checkBoxX.getValue()
  80.     cbY = checkBoxY.getValue()
  81.     cbZ = checkBoxZ.getValue()
  82.     keyCheck = keyBox.getValue()
  83.    
  84.     amntS = float(sText.getText())
  85.     amntS1 = float(sText1.getText())
  86.    
  87.     amntT = float(tText.getText())
  88.     amntR = float(rText.getText())
  89.    
  90.     sl = selected()
  91.     slEnd = sl
  92.     for s in sl:
  93.         tx = 0
  94.         ty = 0
  95.         tz = 0
  96.         rx = 0
  97.         ry = 0
  98.         rz = 0
  99.         sx = 0
  100.         sy = 0
  101.         sz = 0          
  102.         if cbScale:
  103.             if cbX or cbY or cbZ:
  104.                 sr = r.uniform(amntS,amntS1)
  105.             scale(s,sr,sr,sr,r=1)
  106.         if cbTrans:
  107.             if cbX:
  108.                 tx = r.uniform(-amntT,amntT)
  109.             if cbY:
  110.                 ty = r.uniform(-amntT,amntT)
  111.             if cbZ:
  112.                 tz = r.uniform(-amntT,amntT)
  113.             move(s,tx,ty,tz,os=1,r=1)  
  114.         if cbRot:
  115.             if cbX:
  116.                 rx = r.uniform(0,amntR)
  117.             if cbY:
  118.                 ry = r.uniform(0,amntR)
  119.             if cbZ:
  120.                 rz = r.uniform(0,amntR)
  121.             rotate(s,rx,ry,rz,os=1,r=1)
  122.              
  123.         if keyCheck:
  124.             setKeyframe(s)
  125.    
  126.     snapCheck = snapToSurfaceBox.getValue()
  127.     if snapCheck:
  128.         snapToSurface()
  129.     alignToSurface()
  130.    
  131.            
  132.        
  133.            
  134. def scaleCheck():
  135.     sc = checkBoxScale.getValue()
  136.     if sc:
  137.         sText.setEnable(1)
  138.         sText1.setEnable(1)
  139.     else:
  140.         sText.setEnable(0)
  141.         sText1.setEnable(0)
  142. def transCheck():
  143.     bc = checkBoxTranslate.getValue()
  144.     if bc:
  145.         tText.setEnable(1)
  146.     else:
  147.         tText.setEnable(0)
  148. def rotCheck():
  149.     bc = checkBoxRotate.getValue()
  150.     if bc:
  151.         rText.setEnable(1)
  152.     else:
  153.         rText.setEnable(0)  
  154.                              
  155. def addSnapSurface():
  156.     global snapSurfaceMesh
  157.     snapSurfaceMesh = selected()[0]
  158.     snapSurface.setText(str(snapSurfaceMesh))
  159.     snapToSurfaceBox.setEnable(1)
  160.     snapToSurfaceBox.setValue(1)
  161.     alignToSurfaceBox.setValue(1)
  162.     alignToSurfaceBox.setEnable(1)
  163.    
  164.     print snapSurfaceMesh
  165.        
  166. win = window(title= "Gui", s=0)
  167. mainLayout = frameLayout(l="Randor",cll=1)
  168.  
  169. checkBoxLayout = frameLayout(l="Rando",p=mainLayout,cll=1)
  170.  
  171. scaleLayout = rowColumnLayout(nc=1,p=checkBoxLayout)
  172. checkBoxScale = checkBox(l = "Scale",p=scaleLayout,cc =Callback(scaleCheck))
  173. scaleTxtLayout = rowColumnLayout(nr=1,p=scaleLayout)
  174. sText = textField(tx=".5",p=scaleTxtLayout,w=50,en=0)
  175. sText1 = textField(tx="1.5",p=scaleTxtLayout,w=50,en=0)
  176.  
  177. transLayout = rowColumnLayout(nc=1,p=checkBoxLayout)
  178. checkBoxTranslate = checkBox(l = "Trans",p=transLayout,cc =Callback(transCheck))
  179. tText = textField(tx="10",p=transLayout,w=50,en=0)
  180.  
  181. rotLayout = rowColumnLayout(nc=1,p=checkBoxLayout)
  182. checkBoxRotate = checkBox(l = "Rot",p=rotLayout,v=0,cc =Callback(rotCheck))
  183. rText = textField(tx="360",p=rotLayout,w=50)
  184.  
  185. checkLayout = rowColumnLayout(nr=1,p=checkBoxLayout)
  186. checkBoxX = checkBox(l = "X",p=checkLayout,v=1)
  187. checkBoxY = checkBox(l = "Y",p=checkLayout,v=1)
  188. checkBoxZ = checkBox(l = "Z",p=checkLayout,v=1)
  189.  
  190. keyBox = checkBox(l = "Key",p=checkBoxLayout,v=0)#,bgc =[1,0,0])
  191. snapToSurfaceBox = checkBox(l = "SnapToSurface",p=checkBoxLayout,v=0,en=0)
  192. alignToSurfaceBox = checkBox(l = "AlignToSurface",p=checkBoxLayout,v=0,en=0)
  193. randoBtn = button(l="Randomiz!", command=Callback(buttonPressed),p=checkBoxLayout,bgc =[.1,.9,.4])
  194.  
  195.  
  196. snapToSurfaceLayout = frameLayout(l = "snap surface",p=mainLayout,w=50,cll=1,cl=1)
  197. addSurfaceButton = button(l="Add Surface",p=snapToSurfaceLayout,command = Callback(addSnapSurface))
  198. snapSurface = textField("surface",p=snapToSurfaceLayout)
  199.  
  200. snapToSurfaceBtn = button(l="Snap Selected",p=snapToSurfaceLayout,command = Callback(snapToSurface))
  201.  
  202.  
  203.  
  204. win.show()
Advertisement
Add Comment
Please, Sign In to add comment