cwisbg

BG

May 29th, 2019
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.56 KB | None | 0 0
  1. #Building Gen v0.03
  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. print abs(BB[1]-BB[4]), xRange, yRange,zRange
  94. for z in range(0,zRange):
  95. for y in range(0,yRange):
  96. for x in range(0,xRange):
  97. VoxelPosition = [x+BB[0], y+BB[1], z+BB[2]]
  98. VoxelListRaw.append(VoxelPosition)
  99. return VoxelListRaw
  100. class BuildingPiece:
  101. def __init__(self):
  102. self.obj = []
  103. self.validId = [] # Side identifyers
  104. self.validKeys = []
  105. self.validOk = [] # valid sides to match
  106. self.pieceId = 0 # object index in group
  107. class GridNode:
  108. def __init__(self):
  109. self.pos = []
  110. self.obj = []
  111. self.isCollapsed = False # If node has been set
  112. self.isActive = False # is inside bounding box
  113. self.rank = 0 # how many sides match
  114. #self.validId = [0,0,0,0,0] # numbers to match
  115. #self.validOk = [] # valid sides to match
  116. #self.pieceId = 0 # object index in group
  117. self.voxelShift = [0,90,180,270]
  118. self.BitsShifted = 0 # rotation amount
  119. self.buildingPiece = []
  120. # Neighbor nodes: z+|0|Front, x-|1|Right, z-|2|Back, x+|3|Left, y+|4|Top, y-|5|Under
  121. """
  122. self.nFront = [] # Front
  123. self.nRight = [] # Right
  124. self.nBack = [] # Back
  125. self.nLeft = [] # Left
  126. self.nTop= [] # Top
  127. self.nUnder = [] # Under
  128. """
  129. self.SideList = ["Null","Null","Null","Null","Null","Null"]
  130. """
  131. self.SideList = []
  132. for i in range(0,5):
  133. emptyNode = BuildingPiece()
  134. emptyNode.validId = ["Null","Null","Null","Null","Null","Null"]
  135. emptyNode.validKeys = ["Wall","Flat"]
  136. self.SideList.append(emptyNode)
  137. """
  138. def GetNeighbors(self, ActiveVoxels):
  139. pos = self.pos
  140. nodeZ_Check = [pos[0], pos[1], pos[2]+1]
  141. nodeXN_Check = [pos[0]-1, pos[1], pos[2]]
  142. nodeZN_Check = [pos[0], pos[1], pos[2]-1]
  143. nodeX_Check = [pos[0]+1, pos[1], pos[2]]
  144. nodeY_Check = [pos[0], pos[1]-1, pos[2]]
  145. nodeYN_Check = [pos[0], pos[1]+1, pos[2]]
  146. CheckList = [nodeZ_Check, nodeXN_Check, nodeZN_Check, nodeX_Check, nodeY_Check, nodeYN_Check]
  147. for voxel in ActiveVoxels:
  148. i = 0
  149. for check in CheckList:
  150. if voxel.pos == check:
  151. self.SideList[i] = voxel
  152.  
  153. i+=1
  154. def GetRandomPeice(self, PiecesGrp):
  155. PiecesGrpLen = len(PiecesGrp)
  156. randomPiece = r.randint(0,PiecesGrpLen-1)
  157. self.pieceId = randomPiece
  158. self.buildingPiece = PiecesGrp[randomPiece]
  159. #self.BitsShifted = r.randint(0,3)
  160. #self.ShiftBits(self.BitsShifted)
  161. def ShiftBits(self,amount):
  162. newId = self.buildingPiece.validId[-amount:]+ self.buildingPiece.validId[:-amount]
  163. #self.validId = newId
  164. self.buildingPiece.validId = newId
  165. def GetNeighborSide(side):
  166. nodeSide = 0
  167. if side == 0:
  168. nodeSide == 2
  169. if side == 1:
  170. nodeSide == 3
  171. if side == 2:
  172. nodeSide == 0
  173. if side == 3:
  174. nodeSide == 1
  175. if side == 4:
  176. nodeSide == 5
  177. if side == 5:
  178. nodeSide == 4
  179. return nodeSide
  180. def MakePieces():
  181. pieceList = []
  182. PieceGrp = grpr("PiecesGrp")
  183. # Wall
  184. Wall = polyCube(n="Wall_Piece",ch=0)[0]
  185. parent(Wall,PieceGrp)
  186. delete(Wall.f[1:5])
  187. move(Wall, 0,.5,0)
  188. move(Wall.scalePivot, 0,0,0)
  189. move(Wall.rotatePivot, 0,0,0)
  190. makeIdentity(Wall,t=1,a=1)
  191. WallPiece = BuildingPiece()
  192. WallPiece.validId = ["Wall","Null","Null","Null","Null","Null"]# what each side is
  193. WallPiece.validKeys = ["Wall","Flat"]
  194. WallPiece.validKeyList = [["Flat"],["WallC","Wall"],["Null"],["WallC","Wall"],["Wall"],["Wall","WallC", "Flat"]]# valid peices for each side
  195. WallPiece.obj = Wall
  196. pieceList.append(WallPiece)
  197. # WallCorner
  198. WallCorner = polyCube(n="WallCorner_Piece",ch=0)[0]
  199. parent(WallCorner,PieceGrp)
  200. delete(WallCorner.f[1:4])
  201. move(WallCorner, 0,.5,0)
  202. move(WallCorner.scalePivot, 0,0,0)
  203. move(WallCorner.rotatePivot, 0,0,0)
  204. makeIdentity(WallCorner,t=1,a=1)
  205. WallCornerPiece = BuildingPiece()
  206. WallCornerPiece.validId = ["WallC","Null","Null","Null","Null","Null"]
  207. WallCornerPiece.validKeys = ["WallC","Flat"]
  208. WallCornerPiece.validKeyList = [["Flat"],["Flat"],["WallC","Wall"],["WallC","Wall"],["Flat","Wall","WallC"],["Wall","WallC"]]# valid peices for each side
  209. WallCornerPiece.obj = WallCorner
  210. pieceList.append(WallCornerPiece)
  211. #
  212. # Flat
  213. Flat = polyPlane(n="Flat",sw=1,sh=1,ch=0)[0]
  214. parent(Flat,PieceGrp)
  215. #move(Flat, 0,-.5,0)
  216. #move(Flat.scalePivot, 0,0,0)
  217. #move(Flat.rotatePivot, 0,0,0)
  218. #makeIdentity(Flat,t=1,a=1)
  219. FlatPiece = BuildingPiece()
  220. FlatPiece.validId = ["Flat","Flat","Flat","Flat","Flat","Flat"]
  221. FlatPiece.validKeys = ["Flat", "Wall", "Null"]
  222. FlatPiece.validKeyList = [["WallC","Wall","Flat"],["WallC","Wall"],["Null"],["WallC","Wall"],["Null"],["Wall","WallC", "Null"]]# valid peices for each side
  223. FlatPiece.obj = Flat
  224. pieceList.append(FlatPiece)
  225. ###
  226.  
  227. return pieceList
  228. ###
  229. # Get Bounding box objects
  230. BoundsGrp = ls("BoundsObjs")[0].listRelatives(c=1)
  231. BBList = []
  232. for bounds in BoundsGrp:
  233. BB = xform(bounds,bb=1,q=1)
  234. BBList.append(BB)
  235. sceneBB = GetSceneBounds(BBList, 1) # bb list, doRound
  236. #showBB(sceneBB)
  237. VoxelDispGrp = grpr("Vox_Disp_Objs")
  238. # Get Voxels in bounds
  239. toParent = []
  240. # Voxelize Scene
  241. VoxelList = VoxelizeFromBB(sceneBB)
  242. ###
  243.  
  244. PieceList = MakePieces()
  245. PieceListlen = len(PieceList)
  246. doGround = 0
  247. VoxelNodeList = []
  248. ActiveVoxles = []
  249. for voxel in VoxelList:#xmin ymin zmin xmax ymax zmax.
  250. newNode = GridNode()
  251. newNode.pos = voxel
  252. for BB in BBList:
  253. bbCheck = isIn(BB, voxel)
  254. if bbCheck:
  255. newNode.isActive = True
  256. ActiveVoxles.append(newNode)
  257. # Assign random Piece
  258. newNode.GetRandomPeice(PieceList)
  259. VoxelNodeList.append(newNode)
  260.  
  261.  
  262.  
  263. ActiveVoxlesLen = len(ActiveVoxles)
  264. ValidCombos = [[0,0],[0,1]]
  265. i = 0
  266. itterationAmnt = 1
  267. for i in range(0,itterationAmnt):
  268. for voxel in ActiveVoxles: # loop through voxels
  269. if not voxel.isCollapsed:
  270. voxel.GetNeighbors(ActiveVoxles)# get voxel neighbor
  271. voxelValidSides = voxel.buildingPiece.validId
  272. voxelValidKeys = voxel.buildingPiece.validKeys
  273. sideIsValid = False
  274. sideTracker = 0
  275. for side in voxel.SideList:# loop through neighbors
  276. if side != "Null":# is sky
  277. if voxel.rank >= side.rank:
  278. #for key in voxel.buildingPiece.validKeyList:
  279. sideListNeighborSide = GetNeighborSide(sideTracker)# index of array of neighboring node
  280. sideCheck = side.SideList[sideListNeighborSide]
  281. if sideCheck != "Null":
  282. print "Side:",voxel.buildingPiece.validId[sideTracker], "|Side checked:",sideCheck.buildingPiece.validId[sideListNeighborSide], "|Valid keys:",voxel.buildingPiece.validKeyList[sideTracker]
  283. if voxel.buildingPiece.validId[sideTracker] == sideCheck.buildingPiece.validId[sideListNeighborSide]:
  284. print "Match"
  285. sideIsValid = True
  286.  
  287.  
  288. """
  289. for validSide in voxelValidKeys:
  290. if side == validSide:
  291. side.rank += 1
  292. if side.rank == 6:
  293. side.isCollapsed = True
  294. """
  295. if not sideIsValid and side != "Null":
  296. side.GetRandomPeice(PieceList)
  297. sideTracker += 1
  298. #print "Current Sides:",voxel.SideList
  299.  
  300. """
  301.  
  302. rank = 0
  303. for id in currentId:
  304. print id
  305. for validKeys in currentValidKeys:
  306. if id == validKeys:
  307. rank += 1
  308. print voxel.buildingPiece.validKeys, rank
  309. ###
  310. #pick random voxel
  311. #get types for each side
  312. #get valid keys
  313. #get neighbors
  314. #for each side if not null
  315. #get side node
  316. #if rank less than 6 - pick highest rank
  317. #get lower rank node side
  318. check if neibor side is valid
  319. if valid rank + move on
  320. else check side node other sides
  321. if any side is valid
  322. rotate/bitshif
  323. else assign random piece.
  324. ###
  325. """
  326.  
  327. # Voxel displaly
  328. for voxel in ActiveVoxles:
  329. #refresh(cw=1)
  330. #if voxel.rank > 1:
  331. d = duplicate(voxel.buildingPiece.obj)[0]
  332. #d = spaceLocator()
  333. #scale(d, .1,.1,.1)
  334. move(d, voxel.pos)
  335. rotate(d, 0,voxel.voxelShift[voxel.BitsShifted],0)
  336. voxel.obj = d
  337. toParent.append(d)
  338.  
  339. l = spaceLocator()
  340. scale(l, .1,.1,.1)
  341. move(l, voxel.pos)
  342. toParent.append(l)
  343.  
  344. parent(toParent, VoxelDispGrp)
  345.  
  346.  
  347. """
  348. #Neighbor test
  349. randomI = r.randint(0,len(ActiveVoxles)-1)
  350. randomVox = ActiveVoxles[randomI]
  351. scale(randomVox.obj, .2,.2,.2)
  352. for check in randomVox.CheckList:
  353. if check:
  354. scale(check.obj, .3,.3,.3)
  355. """
  356. ###
  357. """
  358. Model analisis
  359. for each tilePiece get valid peices for each side.
  360. # tile identified by #id/tag
  361. find tile peices
  362. get neighboring tiles
  363. if tile != null
  364. get side/peice
  365. remove duplicates
  366. add valid peices to tile sides
  367. """
  368.  
  369.  
  370.  
  371. select(cl=1)
  372. elapsed = (time.clock() - start)
  373. print elapsed
Advertisement
Add Comment
Please, Sign In to add comment