cwisbg

ForestGen

May 22nd, 2019
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. from pymel.core import *
  2. import maya.api.OpenMaya as om
  3. import random as r
  4. import math
  5.  
  6. def grpr(name):
  7. g = ls(name)
  8. if g:
  9. delete(g)
  10. g = group(n=name,em=1)
  11. return g
  12. def getDist(p1,p2):
  13. dist = math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2 + (p1[2] - p2[2])**2)
  14. return dist
  15. def getPiv(s):
  16. currentSceneScale = currentUnit(q=1, l=1)
  17. tempSceneScale = currentUnit(l = "cm")
  18. select(s)
  19. setToolTo('Move')
  20. piv = manipMoveContext('Move', q=True, p=True,m=2)
  21. return piv
  22. currentUnit(l = currentSceneScale)
  23. class PointThing:
  24. found = 0
  25. doMake = 1
  26. pos = []
  27. obj = []
  28. def __init_(self, Obj):
  29. self.pos = []
  30. self.obj = Obj
  31.  
  32. def CullPoints(PointList, CullRadius):
  33. #cull close points
  34. CulledPointList = []
  35. for p in PointList:
  36. for pp in PointList:
  37. if pp != p and pp.found != 1:
  38. dist = getDist(p.pos, pp.pos)
  39. if dist < CullRadius:
  40. p.found = 1
  41. p.doMake = 0
  42. if p.doMake:
  43. CulledPointList.append(p)
  44. return CulledPointList
  45. def ClusterPoints(PointList,MinClusterRadius, ClusterRadius, ClusterStrength, ClusterItterations):
  46. for i in range(0,ClusterItterations):
  47. for p in PointList:
  48. newPos = om.MVector(0,0,0)
  49. for pp in PointList:
  50. if pp != p and pp.found != 1:
  51. dist = getDist(p.pos, pp.pos)
  52. if dist < ClusterRadius and dist > MinClusterRadius:
  53. diff = om.MVector(p.pos[0],p.pos[1],p.pos[2]) - om.MVector(pp.pos[0],pp.pos[1],pp.pos[2])
  54. newPos -= diff
  55. p.found = 1
  56. om.MVector.normalize(newPos)
  57. newPPos = om.MVector(p.pos[0],p.pos[1],p.pos[2])
  58. newPPos += newPos * ClusterStrength
  59. p.pos = newPPos
  60. #move(p.obj, p.pos)
  61.  
  62. ObjectGroup = grpr("ObjectGroup")
  63. BoundsObj = ls("Bounds")[0]
  64. BB = xform(BoundsObj,bb=1,q=1)
  65.  
  66. treeObj = ls("Tree")[0]
  67. treeObj1 = ls("Tree1")[0]
  68. rockObj = ls("Rock")[0]
  69. BigScat = [treeObj,treeObj1,rockObj]
  70. BigScatLen = len(BigScat)
  71. bushObj = ls("Bush")[0]
  72. grassObj = ls("Grass")[0]
  73. SmallScat = [bushObj, grassObj]
  74.  
  75. AvoidPath = ls("curve1")[0]
  76.  
  77. # Scatter tree points in bounds
  78. MaxPointCount = 200
  79. toParent = []
  80. PointArray = []
  81. for i in range(0,MaxPointCount):
  82. newPosX = r.uniform(BB[0],BB[3])
  83. newPosY = r.uniform(BB[1],BB[4])
  84. newPosZ = r.uniform(BB[2],BB[5])
  85. newPos = [newPosX,newPosY,newPosZ]
  86. #l = spaceLocator(p = newPos)
  87. #toParent.append(l)
  88. newPoint = PointThing()
  89. newPoint.pos = newPos
  90. #newPoint.obj = point
  91. PointArray.append(newPoint)
  92.  
  93. NewPointArray = CullPoints(PointArray, 2)
  94. ClusterPoints(NewPointArray, 3, 15, .5, 5)
  95.  
  96.  
  97. #toParent = []
  98. #make points
  99. for p in NewPointArray:
  100. if p.doMake:
  101. point = duplicate(BigScat[r.randint(0,BigScatLen-1)])[0]
  102. move(point, p.pos)
  103. rotate(point, 0,r.uniform(0,360),0)
  104. rScale = r.uniform(.8,1.2)
  105. rScaleY = r.uniform(.5,1.5)
  106. scale(point, rScale,rScaleY,rScale)
  107. setAttr(point.visibility, 1)
  108. toParent.append(point)
  109.  
  110. # place bushes
  111. bushOffsetRange = [1,3]# min, max offset
  112. bushAmnt = 10
  113. for p in PointArray:
  114. if p.doMake:
  115. for i in range(0,bushAmnt):
  116. newPosX = r.uniform(-1,1)# + p.pos[0]
  117. newPosZ = r.uniform(-1,1)# + p.pos[2]
  118.  
  119. #newPosX = r.uniform(-1,1) * r.uniform(bushOffsetRange[0],bushOffsetRange[1]) + p.pos[0]
  120.  
  121. newPos = om.MVector(newPosX, 0, newPosZ)
  122. om.MVector.normalize(newPos)
  123. newPos *= r.uniform(bushOffsetRange[0],bushOffsetRange[1])
  124. newPos += om.MVector(p.pos[0],p.pos[1],p.pos[2])
  125.  
  126. #l = spaceLocator(p = newPos)
  127. #toParent.append(l)
  128. #point = duplicate(treeObj)[0]
  129.  
  130. point = duplicate(SmallScat[r.randint(0,1)])[0]
  131. move(point, newPos)
  132. rotate(point, 0,r.uniform(0,360),0)
  133. rScale = r.uniform(.8,1.2)
  134. rScaleY = r.uniform(.1,1.5)
  135. scale(point, rScale,rScaleY,rScale)
  136. setAttr(point.visibility, 1)
  137. toParent.append(point)
  138.  
  139. parent(toParent, ObjectGroup)
  140.  
  141.  
  142. parent(toParent, ObjectGroup)
  143. select(cl=1)
Advertisement
Add Comment
Please, Sign In to add comment