Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pymel.core import *
- import maya.api.OpenMaya as om
- import random as r
- import math
- def grpr(name):
- g = ls(name)
- if g:
- delete(g)
- g = group(n=name,em=1)
- return g
- def getDist(p1,p2):
- dist = math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2 + (p1[2] - p2[2])**2)
- return dist
- def getPiv(s):
- currentSceneScale = currentUnit(q=1, l=1)
- tempSceneScale = currentUnit(l = "cm")
- select(s)
- setToolTo('Move')
- piv = manipMoveContext('Move', q=True, p=True,m=2)
- return piv
- currentUnit(l = currentSceneScale)
- class PointThing:
- found = 0
- doMake = 1
- pos = []
- obj = []
- def __init_(self, Obj):
- self.pos = []
- self.obj = Obj
- def CullPoints(PointList, CullRadius):
- #cull close points
- CulledPointList = []
- for p in PointList:
- for pp in PointList:
- if pp != p and pp.found != 1:
- dist = getDist(p.pos, pp.pos)
- if dist < CullRadius:
- p.found = 1
- p.doMake = 0
- if p.doMake:
- CulledPointList.append(p)
- return CulledPointList
- def ClusterPoints(PointList,MinClusterRadius, ClusterRadius, ClusterStrength, ClusterItterations):
- for i in range(0,ClusterItterations):
- for p in PointList:
- newPos = om.MVector(0,0,0)
- for pp in PointList:
- if pp != p and pp.found != 1:
- dist = getDist(p.pos, pp.pos)
- if dist < ClusterRadius and dist > MinClusterRadius:
- diff = om.MVector(p.pos[0],p.pos[1],p.pos[2]) - om.MVector(pp.pos[0],pp.pos[1],pp.pos[2])
- newPos -= diff
- p.found = 1
- om.MVector.normalize(newPos)
- newPPos = om.MVector(p.pos[0],p.pos[1],p.pos[2])
- newPPos += newPos * ClusterStrength
- p.pos = newPPos
- #move(p.obj, p.pos)
- ObjectGroup = grpr("ObjectGroup")
- BoundsObj = ls("Bounds")[0]
- BB = xform(BoundsObj,bb=1,q=1)
- treeObj = ls("Tree")[0]
- treeObj1 = ls("Tree1")[0]
- rockObj = ls("Rock")[0]
- BigScat = [treeObj,treeObj1,rockObj]
- BigScatLen = len(BigScat)
- bushObj = ls("Bush")[0]
- grassObj = ls("Grass")[0]
- SmallScat = [bushObj, grassObj]
- AvoidPath = ls("curve1")[0]
- # Scatter tree points in bounds
- MaxPointCount = 200
- toParent = []
- PointArray = []
- for i in range(0,MaxPointCount):
- newPosX = r.uniform(BB[0],BB[3])
- newPosY = r.uniform(BB[1],BB[4])
- newPosZ = r.uniform(BB[2],BB[5])
- newPos = [newPosX,newPosY,newPosZ]
- #l = spaceLocator(p = newPos)
- #toParent.append(l)
- newPoint = PointThing()
- newPoint.pos = newPos
- #newPoint.obj = point
- PointArray.append(newPoint)
- NewPointArray = CullPoints(PointArray, 2)
- ClusterPoints(NewPointArray, 3, 15, .5, 5)
- #toParent = []
- #make points
- for p in NewPointArray:
- if p.doMake:
- point = duplicate(BigScat[r.randint(0,BigScatLen-1)])[0]
- move(point, p.pos)
- rotate(point, 0,r.uniform(0,360),0)
- rScale = r.uniform(.8,1.2)
- rScaleY = r.uniform(.5,1.5)
- scale(point, rScale,rScaleY,rScale)
- setAttr(point.visibility, 1)
- toParent.append(point)
- # place bushes
- bushOffsetRange = [1,3]# min, max offset
- bushAmnt = 10
- for p in PointArray:
- if p.doMake:
- for i in range(0,bushAmnt):
- newPosX = r.uniform(-1,1)# + p.pos[0]
- newPosZ = r.uniform(-1,1)# + p.pos[2]
- #newPosX = r.uniform(-1,1) * r.uniform(bushOffsetRange[0],bushOffsetRange[1]) + p.pos[0]
- newPos = om.MVector(newPosX, 0, newPosZ)
- om.MVector.normalize(newPos)
- newPos *= r.uniform(bushOffsetRange[0],bushOffsetRange[1])
- newPos += om.MVector(p.pos[0],p.pos[1],p.pos[2])
- #l = spaceLocator(p = newPos)
- #toParent.append(l)
- #point = duplicate(treeObj)[0]
- point = duplicate(SmallScat[r.randint(0,1)])[0]
- move(point, newPos)
- rotate(point, 0,r.uniform(0,360),0)
- rScale = r.uniform(.8,1.2)
- rScaleY = r.uniform(.1,1.5)
- scale(point, rScale,rScaleY,rScale)
- setAttr(point.visibility, 1)
- toParent.append(point)
- parent(toParent, ObjectGroup)
- parent(toParent, ObjectGroup)
- select(cl=1)
Advertisement
Add Comment
Please, Sign In to add comment