cwisbg

#Populate surface v3

Aug 5th, 2014
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.52 KB | None | 0 0
  1. #populate surface v3
  2. # cwisbg.com
  3. import random as r
  4. from pymel.core import *
  5. import time
  6. start = time.clock()
  7. def snapToSurfaceSl():
  8.     # select list, then snap to object
  9.     sl = selected()  
  10.     startL = spaceLocator(n="startL")
  11.     endL = spaceLocator(n="endL")
  12.     cp = createNode("closestPointOnMesh",n="snapperCheck")
  13.     snapList = sl
  14.     snapto = snapSurfaceMesh
  15.     snapto.getShape().worldMatrix[0] >> cp.inputMatrix
  16.     startL.translate >> cp.inPosition
  17.     cp.position >> endL.translate
  18.     snapto.getShape().worldMesh[0] >> cp.inMesh
  19.     for s in snapList:
  20.         st = s.getTranslation()
  21.         move(startL, st)
  22.         et = endL.getTranslation()
  23.         move(s,et)
  24.     delete(cp,startL,endL)
  25.     select(sl)
  26. def snapToSurface(newMesh):
  27.     # select list, then snap to object
  28.     sl = newMesh  
  29.     startL = spaceLocator(n="startL")
  30.     endL = spaceLocator(n="endL")
  31.     cp = createNode("closestPointOnMesh",n="snapperCheck")
  32.     snapList = sl
  33.     snapto = snapSurfaceMesh
  34.     snapto.getShape().worldMatrix[0] >> cp.inputMatrix
  35.     startL.translate >> cp.inPosition
  36.     cp.position >> endL.translate
  37.     snapto.getShape().worldMesh[0] >> cp.inMesh
  38.     for s in snapList:
  39.         st = s.getTranslation()
  40.         move(startL, st)
  41.         et = endL.getTranslation()
  42.         move(s,et)
  43.     delete(cp,startL,endL)
  44.     #alignToSurface(newMesh)
  45.     select(sl)
  46. def alignToSurface(newMesh):
  47.     sl = newMesh
  48.     for s in sl:
  49.         nc = normalConstraint(snapSurfaceMesh, s, aim= [0,1,0],u= [0,1,0],wut = "vector",wu =[0,1,0])
  50.         delete(nc)                      
  51. def addSnapSurface():
  52.     global snapSurfaceMesh  
  53.     snapSurfaceMesh = selected()[0]
  54.     snapSurface.setText(str(snapSurfaceMesh))
  55.     popSurfaceBtn.setEnable(1)
  56.     snapSurfaceBtn.setEnable(1)    
  57.  
  58.  
  59. def getPiv(s):
  60.     select(s)
  61.     setToolTo('Move')
  62.     piv = manipMoveContext('Move', q=True, p=True,m=2)  
  63.     return piv  
  64.    
  65. def randoTrans(newMesh):
  66.     #print "randoTrans"
  67.     sl = newMesh
  68.     amntT = float(tText.getText())
  69.     for s in sl:
  70.         #rint s
  71.         tx = r.uniform(-amntT,amntT)
  72.         ty = r.uniform(-amntT,amntT)
  73.         tz = r.uniform(-amntT,amntT)
  74.         move(s,tx,ty,tz,os=1,r=1)
  75. def randoRot(newMesh):
  76.     sl = newMesh
  77.     amntR = float(360)
  78.     for s in sl:
  79.         ry = r.uniform(0,amntR)
  80.         rotate(s,0,ry,0,os=1,r=1)
  81. def randoScale(newMesh):
  82.     sl = newMesh
  83.     amntS = float(tText.getText())
  84.     for s in sl:
  85.         sr = r.uniform(.7,1.3)
  86.         scale(s,sr,sr,sr,ls=1,r=1)        
  87. def grpr(name):#___________________________ Grpr ____________________________
  88.     g = ls(name)
  89.     if not g:
  90.         g = group(n=name,em=1)
  91.     else:
  92.         g = g[0]
  93.     return g                    
  94. def smartPlacer(s):
  95.     vtxColor = polyColorPerVertex(s,r=1,q=1)[0]
  96.     decider = 0
  97.     if vtxColor <= .3:
  98.         decider = 0
  99.         #print "NNNOOOO", vtxColor
  100.     elif vtxColor >= .6:
  101.         decider = 1
  102.         #print "YEEESSS!!!", vtxColor
  103.     else:
  104.         maybe = r.randint(0,1)
  105.         #print "maybe", vtxColor
  106.         if maybe == 1:
  107.             decider = 1
  108.             #print "maybe is a YESS!!!",maybe
  109.         if maybe == 0:
  110.             decider = 0
  111.             #print "maybe is a NO!!", maybe
  112.     return decider
  113. """        
  114. def popSurface1():
  115.    global tracker
  116.    
  117.    sl = selected()
  118.    instanceCheckBoxCheck = instanceCheckBox.getValue()
  119.    vtxPaintCheckBoxCheck = vtxPaintCheckBox.getValue()
  120.    densityCheck = qualityMenu.getValue()
  121.    popList = []
  122.    popGrp = grpr("population_grp")
  123.    i = 0
  124.    
  125.    density = 1
  126.    for vtx in snapSurfaceMesh.vtx:
  127.        if density:
  128.            namin = len(popGrp)
  129.            if densityCheck == "Half":
  130.                density = 0
  131.            print "on ",i, " of:",len(snapSurfaceMesh.vtx), " step:1"
  132.            if vtxPaintCheckBoxCheck:
  133.                decider = smartPlacer(vtx)
  134.            else:
  135.                decider = 1
  136.            if decider == 1:
  137.                rI = r.randrange(0,len(sl))
  138.                if instanceCheckBox:
  139.                    if namin > 1:
  140.                        d = instance(sl[rI],n=sl[rI]+"_population_"+str(i)+"_"+str(namin))[0]
  141.                    d = instance(sl[rI],n=sl[rI]+"_population_"+str(i))[0]
  142.                else:
  143.                    if namin > 1:
  144.                        d = duplicate(sl[rI],n=sl[rI]+"_population_"+str(i)+"_"+str(namin))[0]
  145.                    d = duplicate(sl[rI],n=sl[rI]+"_population_"+str(i))[0]
  146.                popList.append(d)
  147.                pos = getPiv(vtx)
  148.                x = pos
  149.                move(d,x)
  150.                parent(d,popGrp)
  151.            else:
  152.                pass
  153.            i += 1
  154.        else:
  155.            density = 1
  156.    select(cl=1)    
  157.    select(popList)  
  158.    rand = randoCheckBox.getValue()
  159.    if rand:
  160.        #print "randomizing!!!"
  161.        randoTrans()
  162.        randoRot()
  163.        randoScale()
  164.  
  165.        snapToSurface()
  166.  
  167.    tracker += 1
  168.    elapsed = (time.clock() - start)
  169.    print "done, took:", elapsed
  170. """    
  171. def popMesh(populateList,sl):
  172.     global popGrp, tracker
  173.     tracker = 0  
  174.     sl = sl
  175.     instanceCheckBoxCheck = instanceCheckBox.getValue()
  176.     i = 0
  177.     newMesh = []
  178.     popGrp = grpr("population_grp")
  179.     for pop in populateList:
  180.         namin = len(popGrp.listRelatives(c=1))
  181.         rI = r.randrange(0,len(sl))
  182.         if instanceCheckBox:
  183.             if namin >= 1:
  184.                 d = instance(sl[rI],n=sl[rI]+"_population_"+str(i)+"_"+str(namin))[0]
  185.             else:
  186.                 d = instance(sl[rI],n=sl[rI]+"_population_"+str(i))[0]
  187.         else:
  188.             if namin >= 1:
  189.                 d = duplicate(sl[rI],n=sl[rI]+"_population_"+str(i)+"_"+str(namin))[0]
  190.             else:
  191.                 d = duplicate(sl[rI],n=sl[rI]+"_population_"+str(i))[0]
  192.         move(d,pop)
  193.         newMesh.append(d)
  194.         parent(d,popGrp)
  195.         i += 1
  196.     tracker += 1
  197.     return newMesh
  198.        
  199. def getSurfaceData():
  200.     densityCheck = qualityMenu.getValue()
  201.     vtxPaintCheckBoxCheck = vtxPaintCheckBox.getValue()
  202.     populateList = []
  203.     half = 0
  204.     for vtx in snapSurfaceMesh.f:
  205.         if vtxPaintCheckBoxCheck:
  206.             decider = smartPlacer(vtx)
  207.         else:
  208.             decider = 1
  209.         if decider:
  210.             if densityCheck == "Half":
  211.                 if half < len(snapSurfaceMesh.vtx):
  212.                     pos = getPiv(snapSurfaceMesh.vtx[half])
  213.                     populateList.append(pos)
  214.             else:
  215.                 pos = getPiv(vtx)
  216.                 populateList.append(pos)
  217.         half += 2
  218.     return populateList
  219. def popSurface():
  220.     global popGrp
  221.     sl = selected()
  222.     originalD = sl[0]
  223.     populateList = getSurfaceData()
  224.     chunkTimer = time.clock()
  225.     newMesh = popMesh(populateList,sl)
  226.     elapsedChunk = (time.clock() - chunkTimer)
  227.     #print "done, took:", elapsedChunk, "for population ", len(snapSurfaceMesh.vtx)," vtxs"
  228.     if alignCheckBox.getValue():
  229.         alignToSurface(newMesh)
  230.     if randoCheckBox.getValue():      
  231.         randoTrans(newMesh)
  232.         randoRot(newMesh)
  233.         randoScale(newMesh)
  234.         snapToSurface(newMesh)  
  235.     select(cl=1)
  236.     select(originalD)
  237.    
  238.    
  239. def testBtn():
  240.     global snapSurfaceMesh
  241.     snapSurfaceMesh = ls("pPlane1")[0]
  242.     popSurface()
  243.  
  244.              
  245. win = window(title= "Ropulate Surface", s=0)
  246. mainLayout = frameLayout(l="Randor")
  247. #test = button(l="TEST",p=mainLayout,command = Callback(testBtn),en=1)
  248.  
  249. addSurfaceButton = button(l="Add Surface",p=mainLayout,command = Callback(addSnapSurface),w=150)
  250. snapSurface = textField("surface",p=mainLayout)
  251. randoCheckBox = checkBox(l = "Randomize",p=mainLayout,v=1)
  252.  
  253. tText = textField(tx=".1",p=mainLayout,w=50,en=1)
  254. vtxPaintCheckBox = checkBox(l = "Use Vtx Color",p=mainLayout,v=0)
  255. instanceCheckBox = checkBox(l = "Use Instance",p=mainLayout,v=1)
  256. alignCheckBox = checkBox(l = "Align to Surface",p=mainLayout,v=1)
  257.  
  258. qualityMenu = optionMenu(l = "Density")
  259. halfQ = menuItem(l = "Half")
  260. nrmlQ = menuItem(l = "Normal")
  261. #highQ = menuItem(l = "Double")
  262. qualityMenu.setValue("Normal")
  263.  
  264.  
  265. popSurfaceBtn = button(l="Populate Surface",p=mainLayout,command = Callback(popSurface),en=0, bgc = [.25,7,.1])
  266. snapSurfaceBtn = button(l="Snap To Surface",p=mainLayout,command = Callback(snapToSurfaceSl),en=0)
  267. win.show()
  268.  
  269.  
  270.  
  271.  
  272. """
  273.  
  274. getting vxt positions half, done, took: 0.182296687533 for  121  vtxs
  275. getting vxt positions half, done, took: 14.7384265835 for  10201  vtxs
  276. done, took: 0.850182868655 for  676  vtxs
  277. """
Advertisement
Add Comment
Please, Sign In to add comment