cwisbg

Building Gen

May 28th, 2019
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.90 KB | None | 0 0
  1. #Building Gen v0.01
  2. from pymel.core import *
  3. import time
  4. import random as r
  5. import math
  6. start = time.clock()
  7. """
  8. Bit 000000 = z+,x-,z-,x+,y+,y-
  9.  
  10. 0 = ground
  11. 1 = road
  12. 3 = wall
  13. 4 = roof
  14. 5 = roof wall
  15. 6 = wall to ground
  16. 7 = under
  17. 8 = above
  18. """
  19. """
  20. PiecesGrp = ls("Pieces")[0].listRelatives(c=1)
  21. PiecesGrpLen = len(PiecesGrp)
  22. PieceList = []
  23. for piece in PiecesGrp:
  24. newPiece = BuildingPiece()
  25. newPiece.obj = piece
  26. PieceList.append(newPiece)
  27. validIDObj = piece.listRelatives(c=0)[-2]
  28. validObjValidId = getAttr(validIDObj.getShape()+".text")
  29. newPiece.validId = validObjValidId
  30. validOkObj = piece.listRelatives(c=0)[-1]
  31. validObjValidOk = getAttr(validIDObj.getShape()+".text")
  32. newPiece.validOk = validObjValidOk
  33. """
  34.  
  35. def grpr(name):
  36. g = ls(name)
  37. if g:
  38. delete(g)
  39. g = group(n=name,em=1)
  40. return g
  41. def isIn(BoundingBox, Position):#(bb,obj):#xmin ymin zmin xmax ymax zmax.
  42. isInBounds = False
  43. if Position[0] <= BoundingBox[0] or Position[0] >= BoundingBox[3]:
  44. add = 0
  45. elif Position[1] <= BoundingBox[1] or Position[1] >= BoundingBox[4]:
  46. add = 0
  47. elif Position[2] <= BoundingBox[2] or Position[2] >= BoundingBox[5]:
  48. add = 0
  49. else:
  50. add=1
  51. if add:
  52. isInBounds = True
  53. return isInBounds
  54. def GetSceneBounds(boundsList, doRound):
  55. sceneBB = [1000,1000,1000,-1000,-1000,-1000]
  56. for BB in boundsList:
  57. if BB[0] < sceneBB[0]:
  58. sceneBB[0] = BB[0]
  59. if BB[1] < sceneBB[1]:
  60. sceneBB[1] = BB[1]
  61. if BB[2] < sceneBB[2]:
  62. sceneBB[2] = BB[2]
  63. if BB[3] > sceneBB[3]:
  64. sceneBB[3] = BB[3]
  65. if BB[4] > sceneBB[4]:
  66. sceneBB[4] = BB[4]
  67. if BB[5] > sceneBB[5]:
  68. sceneBB[5] = BB[5]
  69. if doRound:
  70. sceneBBRounded = [0,0,0,0,0,0]
  71. i=0
  72. for BB in sceneBB:
  73. sceneBBRounded[i] = round(BB)
  74. i+=1
  75. return sceneBBRounded
  76. else:
  77. return sceneBB
  78.  
  79. def showBB(BB):
  80. newBB = BB
  81. c = polyCube(w=0,h=0,d=0, n = "showCube_")
  82. move(newBB[0]+.5,0,0, str(c[0])+".f[5]", r=1,wd=1,ws=1)
  83. move(0,newBB[1]+.5,0, str(c[0])+".f[3]", r=1,wd=1,ws=1)
  84. move(0,0,newBB[2]+.5, str(c[0])+".f[2]", r=1,wd=1,ws=1)
  85. move(newBB[3]-.5,0,0, str(c[0])+".f[4]", r=1,wd=1,ws=1)
  86. move(0,newBB[4]-.5,0, str(c[0])+".f[1]", r=1,wd=1,ws=1)
  87. move(0,0,newBB[5]-.5, str(c[0])+".f[0]", r=1,wd=1,ws=1)
  88. def VoxelizeFromBB(BB):
  89. VoxelListRaw = []
  90. xRange = int(abs(BB[0]-BB[3])+1)
  91. yRange = int(abs(BB[1]-BB[4])+1)
  92. zRange = int(abs(BB[2]-BB[5])+1)
  93. for z in range(0,zRange):
  94. for y in range(0,yRange):
  95. for x in range(0,xRange):
  96. VoxelPosition = [x+BB[0], y+BB[1], z+BB[2]]
  97. VoxelListRaw.append(VoxelPosition)
  98. return VoxelListRaw
  99. def MakePieces():
  100. pieceList = []
  101. PieceGrp = grpr("PiecesGrp")
  102. # Wall
  103. Wall = polyCube(n="Wall_Piece",ch=0)[0]
  104. parent(Wall,PieceGrp)
  105. delete([Wall.f[1],Wall.f[3]])
  106. WallPiece = BuildingPiece()
  107. WallPiece.validId = ["Wall","Wall","Wall","Wall","Null","Null"]
  108. WallPiece.validKeys = ["Wall","Flat"]
  109. WallPiece.obj = Wall
  110. pieceList.append(WallPiece)
  111. ###
  112. # Flat
  113. Flat = polyPlane(n="Flat",sw=1,sh=1,ch=0)[0]
  114. parent(Flat,PieceGrp)
  115. move(Flat, 0,-.5,0)
  116. move(Flat.scalePivot, 0,0,0)
  117. move(Flat.rotatePivot, 0,0,0)
  118. makeIdentity(Flat,t=1,a=1)
  119. FlatPiece = BuildingPiece()
  120. FlatPiece.validId = ["Flat","Flat","Flat","Flat","Null","Flat"]
  121. FlatPiece.validKeys = ["Flat", "Wall", "Null"]
  122. FlatPiece.obj = Flat
  123. pieceList.append(FlatPiece)
  124. ###
  125. return pieceList
  126.  
  127.  
  128. class BuildingPiece:
  129. def __init__(self):
  130. self.obj = []
  131. self.validId = [] # Side identifyers
  132. self.validKeys = []
  133. self.validOk = [] # valid sides to match
  134. self.pieceId = 0 # object index in group
  135.  
  136.  
  137.  
  138. class GridNode:
  139.  
  140. def __init__(self):
  141. self.pos = []
  142. self.obj = []
  143. self.isCollapsed = False # If node has been set
  144. self.isActive = False # is inside bounding box
  145. self.rank = 0 # how many sides match
  146.  
  147. #self.validId = [0,0,0,0,0] # numbers to match
  148. #self.validOk = [] # valid sides to match
  149. #self.pieceId = 0 # object index in group
  150. self.voxelShift = [0,90,180,270]
  151. self.BitsShifted = 0 # rotation amount
  152. self.buildingPiece = []
  153. # Neighbor nodes: z+|0|Front, x-|1|Right, z-|2|Back, x+|3|Left, y+|4|Top, y-|5|Under
  154. """
  155. self.nFront = [] # Front
  156. self.nRight = [] # Right
  157. self.nBack = [] # Back
  158. self.nLeft = [] # Left
  159. self.nTop= [] # Top
  160. self.nUnder = [] # Under
  161. """
  162. #self.CheckList = [self.nodeZ,self.nodeXN,self.nodeZN,self.nodeX ,self.nodeY ,self.nodeYN]
  163. self.SideList = ["Null","Null","Null","Null","Null","Null"]
  164.  
  165. def GetSides(self, ActiveVoxels):
  166. pos = self.pos
  167. nodeZ_Check = [pos[0], pos[1], pos[2]+1]
  168. nodeXN_Check = [pos[0]-1, pos[1], pos[2]]
  169. nodeZN_Check = [pos[0], pos[1], pos[2]-1]
  170. nodeX_Check = [pos[0]+1, pos[1], pos[2]]
  171. nodeY_Check = [pos[0], pos[1]-1, pos[2]]
  172. nodeYN_Check = [pos[0], pos[1]+1, pos[2]]
  173. CheckList = [nodeZ_Check, nodeXN_Check, nodeZN_Check, nodeX_Check, nodeY_Check, nodeYN_Check]
  174. for voxel in ActiveVoxels:
  175. i = 0
  176. for check in CheckList:
  177. if voxel.pos == check:
  178. self.SideList[i] = voxel
  179. i+=1
  180.  
  181. def GetRandomPeice(self, PiecesGrp):
  182. PiecesGrpLen = len(PiecesGrp)
  183. randomPiece = r.randint(0,PiecesGrpLen-1)
  184. self.pieceId = randomPiece
  185. self.buildingPiece = PiecesGrp[randomPiece]
  186. #self.BitsShifted = r.randint(0,3)
  187. #self.ShiftBits(self.BitsShifted)
  188.  
  189. def ShiftBits(self,amount):
  190. newId = self.buildingPiece.validId[-amount:]+ self.buildingPiece.validId[:-amount]
  191. #self.validId = newId
  192. self.buildingPiece.validId = newId
  193.  
  194. # Get Bounding box objects
  195. BoundsGrp = ls("BoundsObjs")[0].listRelatives(c=1)
  196. BBList = []
  197. for bounds in BoundsGrp:
  198. BB = xform(bounds,bb=1,q=1)
  199. BBList.append(BB)
  200. sceneBB = GetSceneBounds(BBList, 1) # bb list, doRound
  201. #showBB(sceneBB)
  202. VoxelDispGrp = grpr("Vox_Disp_Objs")
  203. # Get Voxels in bounds
  204. toParent = []
  205. # Voxelize Scene
  206. VoxelList = VoxelizeFromBB(sceneBB)
  207.  
  208.  
  209. PieceList = MakePieces()
  210. PieceListlen = len(PieceList)
  211. doGround = 0
  212. VoxelNodeList = []
  213. ActiveVoxles = []
  214. for voxel in VoxelList:#xmin ymin zmin xmax ymax zmax.
  215. newNode = GridNode()
  216. newNode.pos = voxel
  217. for BB in BBList:
  218. bbCheck = isIn(BB, voxel)
  219. if bbCheck:
  220. newNode.isActive = True
  221. ActiveVoxles.append(newNode)
  222. # Assign random Piece
  223. newNode.GetRandomPeice(PieceList)
  224. VoxelNodeList.append(newNode)
  225.  
  226.  
  227. #FlatPiece.validId = ["Flat","Flat","Flat","Flat","Null","Flat"] # what the sides are
  228. #FlatPiece.validKeys = ["Flat", "Wall", "Null"] # valid connections too sides
  229. ActiveVoxlesLen = len(ActiveVoxles)
  230. ValidCombos = [[0,0],[0,1]]
  231. i = 0
  232. itterationAmnt = 1
  233. for i in range(0,itterationAmnt):
  234. for voxel in ActiveVoxles:
  235. if not voxel.isCollapsed:
  236. voxel.GetSides(ActiveVoxles)
  237. if voxel.SideList[5] == "Null":#sky
  238. voxel.buildingPiece= PieceList[1]
  239. voxel.isCollapsed = True
  240. #doNewPiece = False
  241. """
  242. doNewPiece = True
  243. for key in voxel.buildingPiece.validKeys:
  244. if key == "Null":
  245. voxel.buildingPiece= PieceList[1]
  246. voxel.isCollapsed = True
  247. doNewPiece = False
  248. if doNewPiece:
  249. voxel.GetRandomPeice(PieceList)
  250. """
  251.  
  252. else:
  253. voxel.buildingPiece= PieceList[0]
  254. voxel.isCollapsed = True
  255. #voxel.GetRandomPeice(PieceList)
  256. #print voxel.SideList[5].buildingPiece.validId[5]
  257.  
  258.  
  259. """
  260. s = 0
  261. for side in voxel.SideList:
  262. if side != "Null":
  263. for key in voxel.buildingPiece.validKeys:
  264. if key == side.buildingPiece.validId[s]:
  265. print "Up: ", side.buildingPiece.validId[5], voxel.buildingPiece.obj, "Obj key:", key, "Side piece: ", side.buildingPiece.validId[s], "S: ", s
  266. #if voxel.buildingPiece.validKeys, side.buildingPiece.validId:
  267.  
  268. #print "Current Sides ", voxel.buildingPiece.validKeys, side.buildingPiece.validId
  269. s+=1
  270.  
  271. currentId = voxel.buildingPiece.validId
  272. currentValidKeys = voxel.buildingPiece.validKeys
  273. rank = 0
  274. for id in currentId:
  275. doRankUp = False
  276. for validKey in currentValidKeys:
  277. if id == validKey:
  278. print "Valid: ", id, validKey
  279. doRankUp = True
  280. if doRankUp:
  281. rank += 1
  282. print voxel.buildingPiece.validKeys, rank
  283. """
  284. # get sides
  285. # get current node type
  286. # check neighbor node types
  287. # for each side node
  288. # if valid node type matches neighbor node type
  289. # side = valid
  290. # rank += 1
  291. # if not matches
  292. # check side ranks
  293. # change lowest ranged node
  294. #print "Voxel id ",voxel.buildingPiece.validId
  295.  
  296. #voxel.buildingPiece.validId
  297.  
  298. # Voxel displaly
  299. for voxel in ActiveVoxles:
  300. if voxel.isCollapsed:
  301. #refresh(cw=1)
  302. #if voxel.rank > 1:
  303. d = duplicate(voxel.buildingPiece.obj)[0]
  304. #d = spaceLocator()
  305. #scale(d, .1,.1,.1)
  306. move(d, voxel.pos)
  307. rotate(d, 0,voxel.voxelShift[voxel.BitsShifted],0)
  308. voxel.obj = d
  309. toParent.append(d)
  310. #a = annotate(c, tx=str(i), p = voxel.pos)
  311. #setAttr(a+".displayArrow", 0)
  312. #toParent.append(a)
  313. """
  314. l = spaceLocator()
  315. scale(l, .1,.1,.1)
  316. move(l, voxel.pos)
  317. toParent.append(l)
  318. """
  319.  
  320. parent(toParent, VoxelDispGrp)
  321.  
  322.  
  323. """
  324. #Neighbor test
  325. randomI = r.randint(0,len(ActiveVoxles)-1)
  326. randomVox = ActiveVoxles[randomI]
  327. scale(randomVox.obj, .2,.2,.2)
  328. for check in randomVox.CheckList:
  329. if check:
  330. scale(check.obj, .3,.3,.3)
  331. """
  332.  
  333.  
  334. select(cl=1)
  335. elapsed = (time.clock() - start)
  336. print elapsed
Advertisement
Add Comment
Please, Sign In to add comment