cwisbg

Untitled

May 27th, 2019
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. from pymel.core import *
  2. import time
  3. import random as r
  4. import math
  5. start = time.clock()
  6. #
  7. """
  8. 0 = ground
  9. 1 = road
  10. 3 = wall
  11. 4 = roof
  12. 5 = roof wall
  13. 6 = wall to ground
  14. 8 = under
  15. 9 = above
  16. """
  17. class GridNode:
  18. voxelShift = [90,180,270]
  19. def __init__(self):
  20. self.pos = []
  21. self.obj = []
  22. self.isActive = False # is inside bounding box
  23. self.rank = 0 # how many sides match
  24. self.validId = 00000 # numbers to match
  25. self.pieceId = 0 # object index in group
  26. self.BitsShifted = 0 # rotation amount
  27. def GetPeice(self):
  28. PiecesGrp = ls("Pieces")[0].listRelatives(c=1)
  29. PiecesGrpLen = len(PiecesGrp)
  30. randomPiece = r.randint(0,PiecesGrpLen-1)
  31. self.pieceId = randomPiece
  32.  
  33. validObj = PiecesGrp[voxel.pieceId].listRelatives(c=0)[-1]
  34. validObjValidId = getAttr(validObj.getShape()+".text")
  35. self.validId = validObjValidId
  36.  
  37. def ShiftBits(self,amount):
  38. newId = self.validId[-amount:]+ self.validId[:-amount]
  39. newId2 = self.validId[:-amount]
  40. self.validId = newId
  41.  
  42.  
  43.  
  44.  
  45. def grpr(name):
  46. g = ls(name)
  47. if g:
  48. delete(g)
  49. g = group(n=name,em=1)
  50. return g
  51. def isIn(BoundingBox, Position):#(bb,obj):#xmin ymin zmin xmax ymax zmax.
  52. isInBounds = False
  53. if Position[0] <= BoundingBox[0] or Position[0] >= BoundingBox[3]:
  54. add = 0
  55. elif Position[1] <= BoundingBox[1] or Position[1] >= BoundingBox[4]:
  56. add = 0
  57. elif Position[2] <= BoundingBox[2] or Position[2] >= BoundingBox[5]:
  58. add = 0
  59. else:
  60. add=1
  61. if add:
  62. isInBounds = True
  63. return isInBounds
  64. def GetSceneBounds(boundsList, doRound):
  65. sceneBB = [1000,1000,1000,-1000,-1000,-1000]
  66. for BB in boundsList:
  67. if BB[0] < sceneBB[0]:
  68. sceneBB[0] = BB[0]
  69. if BB[1] < sceneBB[1]:
  70. sceneBB[1] = BB[1]
  71. if BB[2] < sceneBB[2]:
  72. sceneBB[2] = BB[2]
  73. if BB[3] > sceneBB[3]:
  74. sceneBB[3] = BB[3]
  75. if BB[4] > sceneBB[4]:
  76. sceneBB[4] = BB[4]
  77. if BB[5] > sceneBB[5]:
  78. sceneBB[5] = BB[5]
  79. if doRound:
  80. sceneBBRounded = [0,0,0,0,0,0]
  81. i=0
  82. for BB in sceneBB:
  83. sceneBBRounded[i] = round(BB)
  84. i+=1
  85. return sceneBBRounded
  86. else:
  87. return sceneBB
  88. def showBB(BB):
  89. newBB = BB
  90. c = polyCube(w=0,h=0,d=0, n = "showCube_")
  91. move(newBB[0]+.5,0,0, str(c[0])+".f[5]", r=1,wd=1,ws=1)
  92. move(0,newBB[1]+.5,0, str(c[0])+".f[3]", r=1,wd=1,ws=1)
  93. move(0,0,newBB[2]+.5, str(c[0])+".f[2]", r=1,wd=1,ws=1)
  94. move(newBB[3]-.5,0,0, str(c[0])+".f[4]", r=1,wd=1,ws=1)
  95. move(0,newBB[4]-.5,0, str(c[0])+".f[1]", r=1,wd=1,ws=1)
  96. move(0,0,newBB[5]-.5, str(c[0])+".f[0]", r=1,wd=1,ws=1)
  97. def VoxelizeFromBB(BB):
  98. VoxelListRaw = []
  99. xRange = int(abs(BB[0])+abs(BB[3])+1)
  100. yRange = int(abs(BB[1])+abs(BB[4])+1)
  101. zRange = int(abs(BB[2])+abs(BB[5])+1)
  102. for z in range(0,xRange):
  103. for y in range(0,yRange):
  104. for x in range(0,zRange):
  105. VoxelPosition = [x + BB[0], y + BB[1], z + BB[2]]
  106. VoxelListRaw.append(VoxelPosition)
  107. return VoxelListRaw
  108.  
  109.  
  110. # ThingsGrp = ls("ThingsGrp")[0].listRelatives(c=1)
  111.  
  112. #Bounding box objects
  113. BoundsGrp = ls("BoundsObjs")[0].listRelatives(c=1)
  114. BBList = []
  115. for bounds in BoundsGrp:
  116. BB = xform(bounds,bb=1,q=1)
  117. BBList.append(BB)
  118.  
  119. sceneBB = GetSceneBounds(BBList, 1) # bb list, doRound
  120. #sceneBBCenter = (sceneBB[0]+sceneBB[3])/2, (sceneBB[1]+sceneBB[4])/2, (sceneBB[2]+sceneBB[5])/2
  121. #showBB(sceneBB)
  122.  
  123. VoxelDispGrp = grpr("Vox_Disp_Objs")
  124.  
  125. # Get Voxels in bounds
  126. toParent = []
  127. VoxelList = VoxelizeFromBB(sceneBB)
  128. doGround = 1
  129. doDislpay = 0
  130. VoxelNodeList = []
  131. for voxel in VoxelList:#xmin ymin zmin xmax ymax zmax.
  132. newNode = GridNode()
  133. newNode.pos = voxel
  134. VoxelNodeList.append(newNode)
  135. for BB in BBList:
  136. bbCheck = isIn(BB, voxel)
  137. if bbCheck:
  138. newNode.isActive = True
  139. if doGround:
  140. #if voxel[1] < .2 and voxel[1] > -.2:
  141. if voxel[1] == 0:
  142. newNode.isActive = True
  143. if newNode.isActive:
  144. if doDislpay:
  145. c = spaceLocator()
  146. a = annotate(c, tx=str(voxel), p = voxel)
  147. setAttr(a+".displayArrow", 0)
  148. toParent.append(a)
  149. move(c, voxel)
  150. scale(c, .1,.1,.1)
  151. toParent.append(c)
  152. parent(toParent, VoxelDispGrp)
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. #Voxel calculate
  160. lastActive = []
  161. for voxel in VoxelNodeList:
  162. if voxel.isActive:
  163. voxel.GetPeice()
  164. lastActive = voxel
  165. #print lastActive.validId[0]
  166. #lastActive.ShiftBits(1)
  167. #print lastActive.validId
  168.  
  169.  
  170. i = 0
  171. end = len(VoxelNodeList)
  172. for voxel in VoxelNodeList:
  173. if i != end:
  174. if voxel.isActive and VoxelNodeList[i+1].isActive:
  175. for x in range(0,5):
  176. if voxel.validId[x] == VoxelNodeList[i+1].validId[x]:
  177. voxel.rank += 1
  178. i += 1
  179.  
  180.  
  181.  
  182. PiecesGrp = ls("Pieces")[0].listRelatives(c=1)
  183. PiecesGrpLen = len(PiecesGrp)
  184. ActiveVoxles = []
  185. # Voxel displaly
  186. for voxel in VoxelNodeList:
  187. if voxel.isActive and voxel.rank > 1:
  188. #refresh(cw=1)
  189. d = duplicate(PiecesGrp[voxel.pieceId])[0]
  190. move(d, voxel.pos)
  191. voxel.obj = d
  192. toParent.append(d)
  193. ActiveVoxles.append(voxel)
  194. parent(toParent, VoxelDispGrp)
  195.  
  196.  
  197. randomI = r.randint(0,len(ActiveVoxles)-1)
  198. randomVox = ActiveVoxles[randomI]
  199. #xmin ymin zmin xmax ymax zmax.
  200. xRange = int(abs(BB[0])+abs(BB[3])+1)
  201. yRange = int(abs(BB[1])+abs(BB[4])+1)
  202. zRange = int(abs(BB[2])+abs(BB[5])+1)
  203. pos = randomVox.pos
  204. xI = pos[0] + xRange * pos[1] + xRange * yRange * pos[2]
  205. print xI
  206. scale(ActiveVoxles[int(xI)], .3,.3,.3)
  207. """
  208. if pos.y < gridSizeY-1:
  209. xY = pos[0] + gridSizeX * (pos[1]+1) + gridSizeX * gridSizeY * pos[2]
  210. scale(ActiveVoxles[int(xY)], .1,.1,.1)
  211. """
  212.  
  213.  
  214. select(cl=1)
  215. elapsed = (time.clock() - start)
  216. print elapsed
Advertisement
Add Comment
Please, Sign In to add comment