Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #populate surface v3
- # cwisbg.com
- import random as r
- from pymel.core import *
- import time
- start = time.clock()
- def snapToSurfaceSl():
- # select list, then snap to object
- sl = selected()
- startL = spaceLocator(n="startL")
- endL = spaceLocator(n="endL")
- cp = createNode("closestPointOnMesh",n="snapperCheck")
- snapList = sl
- snapto = snapSurfaceMesh
- snapto.getShape().worldMatrix[0] >> cp.inputMatrix
- startL.translate >> cp.inPosition
- cp.position >> endL.translate
- snapto.getShape().worldMesh[0] >> cp.inMesh
- for s in snapList:
- st = s.getTranslation()
- move(startL, st)
- et = endL.getTranslation()
- move(s,et)
- delete(cp,startL,endL)
- select(sl)
- def snapToSurface(newMesh):
- # select list, then snap to object
- sl = newMesh
- startL = spaceLocator(n="startL")
- endL = spaceLocator(n="endL")
- cp = createNode("closestPointOnMesh",n="snapperCheck")
- snapList = sl
- snapto = snapSurfaceMesh
- snapto.getShape().worldMatrix[0] >> cp.inputMatrix
- startL.translate >> cp.inPosition
- cp.position >> endL.translate
- snapto.getShape().worldMesh[0] >> cp.inMesh
- for s in snapList:
- st = s.getTranslation()
- move(startL, st)
- et = endL.getTranslation()
- move(s,et)
- delete(cp,startL,endL)
- #alignToSurface(newMesh)
- select(sl)
- def alignToSurface(newMesh):
- sl = newMesh
- for s in sl:
- nc = normalConstraint(snapSurfaceMesh, s, aim= [0,1,0],u= [0,1,0],wut = "vector",wu =[0,1,0])
- delete(nc)
- def addSnapSurface():
- global snapSurfaceMesh
- snapSurfaceMesh = selected()[0]
- snapSurface.setText(str(snapSurfaceMesh))
- popSurfaceBtn.setEnable(1)
- snapSurfaceBtn.setEnable(1)
- def getPiv(s):
- select(s)
- setToolTo('Move')
- piv = manipMoveContext('Move', q=True, p=True,m=2)
- return piv
- def randoTrans(newMesh):
- #print "randoTrans"
- sl = newMesh
- amntT = float(tText.getText())
- for s in sl:
- #rint s
- tx = r.uniform(-amntT,amntT)
- ty = r.uniform(-amntT,amntT)
- tz = r.uniform(-amntT,amntT)
- move(s,tx,ty,tz,os=1,r=1)
- def randoRot(newMesh):
- sl = newMesh
- amntR = float(360)
- for s in sl:
- ry = r.uniform(0,amntR)
- rotate(s,0,ry,0,os=1,r=1)
- def randoScale(newMesh):
- sl = newMesh
- amntS = float(tText.getText())
- for s in sl:
- sr = r.uniform(.7,1.3)
- scale(s,sr,sr,sr,ls=1,r=1)
- def grpr(name):#___________________________ Grpr ____________________________
- g = ls(name)
- if not g:
- g = group(n=name,em=1)
- else:
- g = g[0]
- return g
- def smartPlacer(s):
- vtxColor = polyColorPerVertex(s,r=1,q=1)[0]
- decider = 0
- if vtxColor <= .3:
- decider = 0
- #print "NNNOOOO", vtxColor
- elif vtxColor >= .6:
- decider = 1
- #print "YEEESSS!!!", vtxColor
- else:
- maybe = r.randint(0,1)
- #print "maybe", vtxColor
- if maybe == 1:
- decider = 1
- #print "maybe is a YESS!!!",maybe
- if maybe == 0:
- decider = 0
- #print "maybe is a NO!!", maybe
- return decider
- """
- def popSurface1():
- global tracker
- sl = selected()
- instanceCheckBoxCheck = instanceCheckBox.getValue()
- vtxPaintCheckBoxCheck = vtxPaintCheckBox.getValue()
- densityCheck = qualityMenu.getValue()
- popList = []
- popGrp = grpr("population_grp")
- i = 0
- density = 1
- for vtx in snapSurfaceMesh.vtx:
- if density:
- namin = len(popGrp)
- if densityCheck == "Half":
- density = 0
- print "on ",i, " of:",len(snapSurfaceMesh.vtx), " step:1"
- if vtxPaintCheckBoxCheck:
- decider = smartPlacer(vtx)
- else:
- decider = 1
- if decider == 1:
- rI = r.randrange(0,len(sl))
- if instanceCheckBox:
- if namin > 1:
- d = instance(sl[rI],n=sl[rI]+"_population_"+str(i)+"_"+str(namin))[0]
- d = instance(sl[rI],n=sl[rI]+"_population_"+str(i))[0]
- else:
- if namin > 1:
- d = duplicate(sl[rI],n=sl[rI]+"_population_"+str(i)+"_"+str(namin))[0]
- d = duplicate(sl[rI],n=sl[rI]+"_population_"+str(i))[0]
- popList.append(d)
- pos = getPiv(vtx)
- x = pos
- move(d,x)
- parent(d,popGrp)
- else:
- pass
- i += 1
- else:
- density = 1
- select(cl=1)
- select(popList)
- rand = randoCheckBox.getValue()
- if rand:
- #print "randomizing!!!"
- randoTrans()
- randoRot()
- randoScale()
- snapToSurface()
- tracker += 1
- elapsed = (time.clock() - start)
- print "done, took:", elapsed
- """
- def popMesh(populateList,sl):
- global popGrp, tracker
- tracker = 0
- sl = sl
- instanceCheckBoxCheck = instanceCheckBox.getValue()
- i = 0
- newMesh = []
- popGrp = grpr("population_grp")
- for pop in populateList:
- namin = len(popGrp.listRelatives(c=1))
- rI = r.randrange(0,len(sl))
- if instanceCheckBox:
- if namin >= 1:
- d = instance(sl[rI],n=sl[rI]+"_population_"+str(i)+"_"+str(namin))[0]
- else:
- d = instance(sl[rI],n=sl[rI]+"_population_"+str(i))[0]
- else:
- if namin >= 1:
- d = duplicate(sl[rI],n=sl[rI]+"_population_"+str(i)+"_"+str(namin))[0]
- else:
- d = duplicate(sl[rI],n=sl[rI]+"_population_"+str(i))[0]
- move(d,pop)
- newMesh.append(d)
- parent(d,popGrp)
- i += 1
- tracker += 1
- return newMesh
- def getSurfaceData():
- densityCheck = qualityMenu.getValue()
- vtxPaintCheckBoxCheck = vtxPaintCheckBox.getValue()
- populateList = []
- half = 0
- for vtx in snapSurfaceMesh.f:
- if vtxPaintCheckBoxCheck:
- decider = smartPlacer(vtx)
- else:
- decider = 1
- if decider:
- if densityCheck == "Half":
- if half < len(snapSurfaceMesh.vtx):
- pos = getPiv(snapSurfaceMesh.vtx[half])
- populateList.append(pos)
- else:
- pos = getPiv(vtx)
- populateList.append(pos)
- half += 2
- return populateList
- def popSurface():
- global popGrp
- sl = selected()
- originalD = sl[0]
- populateList = getSurfaceData()
- chunkTimer = time.clock()
- newMesh = popMesh(populateList,sl)
- elapsedChunk = (time.clock() - chunkTimer)
- #print "done, took:", elapsedChunk, "for population ", len(snapSurfaceMesh.vtx)," vtxs"
- if alignCheckBox.getValue():
- alignToSurface(newMesh)
- if randoCheckBox.getValue():
- randoTrans(newMesh)
- randoRot(newMesh)
- randoScale(newMesh)
- snapToSurface(newMesh)
- select(cl=1)
- select(originalD)
- def testBtn():
- global snapSurfaceMesh
- snapSurfaceMesh = ls("pPlane1")[0]
- popSurface()
- win = window(title= "Ropulate Surface", s=0)
- mainLayout = frameLayout(l="Randor")
- #test = button(l="TEST",p=mainLayout,command = Callback(testBtn),en=1)
- addSurfaceButton = button(l="Add Surface",p=mainLayout,command = Callback(addSnapSurface),w=150)
- snapSurface = textField("surface",p=mainLayout)
- randoCheckBox = checkBox(l = "Randomize",p=mainLayout,v=1)
- tText = textField(tx=".1",p=mainLayout,w=50,en=1)
- vtxPaintCheckBox = checkBox(l = "Use Vtx Color",p=mainLayout,v=0)
- instanceCheckBox = checkBox(l = "Use Instance",p=mainLayout,v=1)
- alignCheckBox = checkBox(l = "Align to Surface",p=mainLayout,v=1)
- qualityMenu = optionMenu(l = "Density")
- halfQ = menuItem(l = "Half")
- nrmlQ = menuItem(l = "Normal")
- #highQ = menuItem(l = "Double")
- qualityMenu.setValue("Normal")
- popSurfaceBtn = button(l="Populate Surface",p=mainLayout,command = Callback(popSurface),en=0, bgc = [.25,7,.1])
- snapSurfaceBtn = button(l="Snap To Surface",p=mainLayout,command = Callback(snapToSurfaceSl),en=0)
- win.show()
- """
- getting vxt positions half, done, took: 0.182296687533 for 121 vtxs
- getting vxt positions half, done, took: 14.7384265835 for 10201 vtxs
- done, took: 0.850182868655 for 676 vtxs
- """
Advertisement
Add Comment
Please, Sign In to add comment