grow.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # This code is called when instances of this SOP cook.
  2. #scaleFactor = node.parm("scaleFactor").eval()
  3. import random
  4. import hou
  5. node = hou.pwd()
  6. geo = node.geometry()
  7. # map user input
  8. GENERATIONS = hou.ch('generations')
  9. TROPISM = hou.Vector3(hou.parmTuple("tropDir").eval())
  10. TROPISMFACTOR = hou.ch('tropStrength')
  11. GRAVITYFACTOR = hou.ch('gFactor')
  12. SEED = 1233
  13. #hou.ch("falloff")
  14. # the "seed"
  15. # this is the first point from which the tree will grow.
  16. point = geo.createPoint()
  17. pointers = [point]
  18. # init the point attributes to store the values in, as these must exist.
  19. # As we start, the first point's normal will point upward to grow in that direction.
  20. geo.addAttrib(hou.attribType.Point, "N", hou.Vector3([0, 1, 0]))
  21. geo.addAttrib(hou.attribType.Point, "Branch", 0)
  22. geo.addAttrib(hou.attribType.Point, "Generation", 0)
  23. geo.addAttrib(hou.attribType.Point, "BranchPoint", 0)
  24. geo.addAttrib(hou.attribType.Point, 'NormalizedPosition', 0.0)
  25. geo.addAttrib(hou.attribType.Point, 'Diameter', 1.0)
  26. # Just in case, if we need groups, this is how to do it.
  27. #defaultGrp=geo.createPointGroup('name')
  28. #defaultGrp.add(point)
  29. def duplicatePoint(p):
  30. # hmm. maybe there is sth like this already, but i missed it...
  31. # this duplicates a point and all it's attributes.
  32. dupe=geo.createPoint()
  33. dupe.setPosition(p.position())
  34. allAttr=geo.pointAttribs()
  35. for i in allAttr:
  36. holder = None
  37. holder = p.attribValue(i)
  38. dupe.setAttribValue(i, holder)
  39. return dupe
  40. def tropism(pt,vector,factor):
  41. '''Move point towards another point, as in growing towards a light source (sun)
  42. or a food source (roots)
  43. '''
  44. P = pt.position()
  45. vector = vector.normalized()
  46. pt.setPosition(P + (1*factor) * vector)
  47. def moveAlongNormal(pt, factor):
  48. P = pt.position()
  49. #P = hou.Vector3(P)
  50. # be careful - this returns a value you cant simply add to P, must convert it to a hou.Vector3
  51. N = pt.attribValue("N")
  52. N = hou.Vector3(N)
  53. pt.setPosition(P + (1*factor) * N)
  54. def gravity(pt, factor):
  55. '''Convenience function to move down a point, multiplied by diameter.'''
  56. P = pt.position()
  57. # The downforce D
  58. D = (1 - pt.attribValue('Diameter'))*factor
  59. G = hou.Vector3([0, D, 0])
  60. pt.setPosition(P - G)
  61. def createRandomVector():
  62. rx = (random.random()-0.5)*2
  63. ry = (random.random()-0.5)*2
  64. rz = (random.random()-0.5)*2
  65. return hou.Vector3([rx, ry, rz])
  66. def addNoiseToNormal(pt, factor):
  67. N = pt.attribValue("N")
  68. N = hou.Vector3(N)
  69. rx = (random.random()-0.5)*2
  70. ry = (random.random()-0.5)*2
  71. rz = (random.random()-0.5)*2
  72. rvec = hou.Vector3([rx*factor, ry*factor, rz*factor])
  73. newN = rvec + 1 * N
  74. pt.setAttribValue("N", newN)
  75. return pt
  76. def rotateNormal(pt, factor, seed):
  77. N = pt.attribValue("N")
  78. N = hou.Vector3(N)
  79. x = (random.random()-0.5)*2
  80. y = (random.random()-0.5)*2
  81. z = (random.random()-0.5)*2
  82. print 'p random x {0} y {1} z {2}'.format(x, y, z)
  83. x = (hou.hmath.rand(seed+12*43)-0.5)*2
  84. y = (hou.hmath.rand(seed+2+2*3)-0.5)*2
  85. z = (hou.hmath.rand(seed+33*2)-0.5)*2
  86. print 'h random x {0} y {1} z {2}'.format(x, y, z)
  87. rvec = hou.Vector3([x*factor, y*factor, z*factor])
  88. newN = rvec + 1 * N
  89. pt.setAttribValue("N", newN)
  90. return pt
  91. def getPointsByAttribValue(attr, val):
  92. plist = []
  93. for p in geo.iterPoints():
  94. if p.attribValue(attr) == val:
  95. plist.append(p)
  96. return plist
  97. def getHighestNumericAttrVal(points, attr):
  98. val = 0.0
  99. for p in points:
  100. v = float(p.attribValue(attr))
  101. if v > val:
  102. val = v
  103. return val
  104. branchNum = 0
  105. def step(pointers,stepseed):
  106. ''' step through all head pointers (aka growing branches)'''
  107. updatedPointers=[]
  108. for p in pointers:
  109. #print 'pointer is point w/ # ' + str(p.number())
  110. newP = duplicatePoint(p)
  111. #print 'copy is point w/ # ' + str(newP.number())
  112. updatedPointers.append(newP)
  113. newP.setAttribValue('BranchPoint', newP.attribValue('BranchPoint')+1)
  114. # Should we branch?
  115. # TODO allow multiple branches at once
  116. rnd = random.random()
  117. rnd = hou.hmath.rand(stepseed+11)
  118. if rnd > 0.7:
  119. # Dirty, find a better way soon!
  120. global branchNum
  121. branchNum += 1
  122. global treeInfo
  123. #treeInfo['totalbranches']+=1
  124. #print 'branch'
  125. branchP = duplicatePoint(p)
  126. branchP.setAttribValue('BranchPoint', 0)
  127. b = branchP.attribValue('Branch')
  128. branchP.setAttribValue('Branch', branchNum)
  129. #branchP.setAttribValue('Branch',b+1)
  130. g = branchP.attribValue('Generation')
  131. branchP.setAttribValue('Generation', g + 1)
  132. updatedPointers.append(branchP)
  133. stepseed += 1
  134. newP = rotateNormal(newP, 0.4, stepseed)
  135. # now move along normal
  136. moveAlongNormal(newP, 1)
  137. v = hou.Vector3(0, 1, 1)
  138. tropism(newP, TROPISM, TROPISMFACTOR)
  139. gravity(newP, GRAVITYFACTOR)
  140. newP.setAttribValue('Diameter', newP.attribValue('Diameter')*0.7)
  141. stepseed += 1
  142. print 'seed', stepseed
  143. return updatedPointers
  144. x = step(pointers, 1)
  145. seed = 2
  146. for arsch in range(GENERATIONS):
  147. #seed=1
  148. x = step(x, seed)
  149. seed += 1
  150. # until there is a better way, 'postprocess' the tree to gain info that is not present at runtime.
  151. def postprocess():
  152. tInfo = {}
  153. '''
  154. for n in range(0,branchNum):
  155. for p in geo.iterPoints():
  156. #print p
  157. print p.attribValue('BranchPoint')
  158. #if p.attribValue('Branch') == n:
  159. # print p.attribValue('BranchPoint')
  160. '''
  161. for p in geo.iterPoints():
  162. curBranch = p.attribValue('Branch')
  163. tInfo['branch'] = curBranch
  164. print '[[[[[[[[[[***]]]]]]]]]]'
  165. totalBranches = int(getHighestNumericAttrVal(geo.iterPoints(), 'Branch'))
  166. for i in range(0,totalBranches):
  167. # go through all branches
  168. max = 0
  169. for p in getPointsByAttribValue('Branch',i):
  170. v = p.attribValue('BranchPoint')
  171. if v > max:
  172. max = v
  173. for p in getPointsByAttribValue('Branch',i):
  174. bp = p.attribValue('BranchPoint')
  175. #print max
  176. #print bp
  177. try:
  178. relpos = float(bp)/float(max)
  179. except:
  180. relpos = 0
  181. #p.setAttribValue('NormalizedPosition',1)
  182. p.setAttribValue('NormalizedPosition', float(relpos))
  183. #print 'branch {0} points {1}'.format(i,max)
  184. # TODO and thoughts
  185. # in addition to the branch 'id' we need a distance of each point of a branch towards it's parent.
  186. # useful: how many 'levels' are we away from the main parent? > point needs to have parent id, increment on branch
  187. # roots
  188. # DIAMETER should decrease by a step after each branch