Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Building Gen v0.03
- from pymel.core import *
- import time
- import random as r
- import math
- start = time.clock()
- """
- Bit 000000 = z+,x-,z-,x+,y+,y-
- 0 = ground
- 1 = road
- 3 = wall
- 4 = roof
- 5 = roof wall
- 6 = wall to ground
- 7 = under
- 8 = above
- """
- """
- PiecesGrp = ls("Pieces")[0].listRelatives(c=1)
- PiecesGrpLen = len(PiecesGrp)
- PieceList = []
- for piece in PiecesGrp:
- newPiece = BuildingPiece()
- newPiece.obj = piece
- PieceList.append(newPiece)
- validIDObj = piece.listRelatives(c=0)[-2]
- validObjValidId = getAttr(validIDObj.getShape()+".text")
- newPiece.validId = validObjValidId
- validOkObj = piece.listRelatives(c=0)[-1]
- validObjValidOk = getAttr(validIDObj.getShape()+".text")
- newPiece.validOk = validObjValidOk
- """
- def grpr(name):
- g = ls(name)
- if g:
- delete(g)
- g = group(n=name,em=1)
- return g
- def isIn(BoundingBox, Position):#(bb,obj):#xmin ymin zmin xmax ymax zmax.
- isInBounds = False
- if Position[0] <= BoundingBox[0] or Position[0] >= BoundingBox[3]:
- add = 0
- elif Position[1] <= BoundingBox[1] or Position[1] >= BoundingBox[4]:
- add = 0
- elif Position[2] <= BoundingBox[2] or Position[2] >= BoundingBox[5]:
- add = 0
- else:
- add=1
- if add:
- isInBounds = True
- return isInBounds
- def GetSceneBounds(boundsList, doRound):
- sceneBB = [1000,1000,1000,-1000,-1000,-1000]
- for BB in boundsList:
- if BB[0] < sceneBB[0]:
- sceneBB[0] = BB[0]
- if BB[1] < sceneBB[1]:
- sceneBB[1] = BB[1]
- if BB[2] < sceneBB[2]:
- sceneBB[2] = BB[2]
- if BB[3] > sceneBB[3]:
- sceneBB[3] = BB[3]
- if BB[4] > sceneBB[4]:
- sceneBB[4] = BB[4]
- if BB[5] > sceneBB[5]:
- sceneBB[5] = BB[5]
- if doRound:
- sceneBBRounded = [0,0,0,0,0,0]
- i=0
- for BB in sceneBB:
- sceneBBRounded[i] = round(BB)
- i+=1
- return sceneBBRounded
- else:
- return sceneBB
- def showBB(BB):
- newBB = BB
- c = polyCube(w=0,h=0,d=0, n = "showCube_")
- move(newBB[0]+.5,0,0, str(c[0])+".f[5]", r=1,wd=1,ws=1)
- move(0,newBB[1]+.5,0, str(c[0])+".f[3]", r=1,wd=1,ws=1)
- move(0,0,newBB[2]+.5, str(c[0])+".f[2]", r=1,wd=1,ws=1)
- move(newBB[3]-.5,0,0, str(c[0])+".f[4]", r=1,wd=1,ws=1)
- move(0,newBB[4]-.5,0, str(c[0])+".f[1]", r=1,wd=1,ws=1)
- move(0,0,newBB[5]-.5, str(c[0])+".f[0]", r=1,wd=1,ws=1)
- def VoxelizeFromBB(BB):
- VoxelListRaw = []
- xRange = int(abs(BB[0]-BB[3])+1)
- yRange = int(abs(BB[1]-BB[4])+1)
- zRange = int(abs(BB[2]-BB[5])+1)
- print abs(BB[1]-BB[4]), xRange, yRange,zRange
- for z in range(0,zRange):
- for y in range(0,yRange):
- for x in range(0,xRange):
- VoxelPosition = [x+BB[0], y+BB[1], z+BB[2]]
- VoxelListRaw.append(VoxelPosition)
- return VoxelListRaw
- class BuildingPiece:
- def __init__(self):
- self.obj = []
- self.validId = [] # Side identifyers
- self.validKeys = []
- self.validOk = [] # valid sides to match
- self.pieceId = 0 # object index in group
- class GridNode:
- def __init__(self):
- self.pos = []
- self.obj = []
- self.isCollapsed = False # If node has been set
- self.isActive = False # is inside bounding box
- self.rank = 0 # how many sides match
- #self.validId = [0,0,0,0,0] # numbers to match
- #self.validOk = [] # valid sides to match
- #self.pieceId = 0 # object index in group
- self.voxelShift = [0,90,180,270]
- self.BitsShifted = 0 # rotation amount
- self.buildingPiece = []
- # Neighbor nodes: z+|0|Front, x-|1|Right, z-|2|Back, x+|3|Left, y+|4|Top, y-|5|Under
- """
- self.nFront = [] # Front
- self.nRight = [] # Right
- self.nBack = [] # Back
- self.nLeft = [] # Left
- self.nTop= [] # Top
- self.nUnder = [] # Under
- """
- self.SideList = ["Null","Null","Null","Null","Null","Null"]
- """
- self.SideList = []
- for i in range(0,5):
- emptyNode = BuildingPiece()
- emptyNode.validId = ["Null","Null","Null","Null","Null","Null"]
- emptyNode.validKeys = ["Wall","Flat"]
- self.SideList.append(emptyNode)
- """
- def GetNeighbors(self, ActiveVoxels):
- pos = self.pos
- nodeZ_Check = [pos[0], pos[1], pos[2]+1]
- nodeXN_Check = [pos[0]-1, pos[1], pos[2]]
- nodeZN_Check = [pos[0], pos[1], pos[2]-1]
- nodeX_Check = [pos[0]+1, pos[1], pos[2]]
- nodeY_Check = [pos[0], pos[1]-1, pos[2]]
- nodeYN_Check = [pos[0], pos[1]+1, pos[2]]
- CheckList = [nodeZ_Check, nodeXN_Check, nodeZN_Check, nodeX_Check, nodeY_Check, nodeYN_Check]
- for voxel in ActiveVoxels:
- i = 0
- for check in CheckList:
- if voxel.pos == check:
- self.SideList[i] = voxel
- i+=1
- def GetRandomPeice(self, PiecesGrp):
- PiecesGrpLen = len(PiecesGrp)
- randomPiece = r.randint(0,PiecesGrpLen-1)
- self.pieceId = randomPiece
- self.buildingPiece = PiecesGrp[randomPiece]
- #self.BitsShifted = r.randint(0,3)
- #self.ShiftBits(self.BitsShifted)
- def ShiftBits(self,amount):
- newId = self.buildingPiece.validId[-amount:]+ self.buildingPiece.validId[:-amount]
- #self.validId = newId
- self.buildingPiece.validId = newId
- def GetNeighborSide(side):
- nodeSide = 0
- if side == 0:
- nodeSide == 2
- if side == 1:
- nodeSide == 3
- if side == 2:
- nodeSide == 0
- if side == 3:
- nodeSide == 1
- if side == 4:
- nodeSide == 5
- if side == 5:
- nodeSide == 4
- return nodeSide
- def MakePieces():
- pieceList = []
- PieceGrp = grpr("PiecesGrp")
- # Wall
- Wall = polyCube(n="Wall_Piece",ch=0)[0]
- parent(Wall,PieceGrp)
- delete(Wall.f[1:5])
- move(Wall, 0,.5,0)
- move(Wall.scalePivot, 0,0,0)
- move(Wall.rotatePivot, 0,0,0)
- makeIdentity(Wall,t=1,a=1)
- WallPiece = BuildingPiece()
- WallPiece.validId = ["Wall","Null","Null","Null","Null","Null"]# what each side is
- WallPiece.validKeys = ["Wall","Flat"]
- WallPiece.validKeyList = [["Flat"],["WallC","Wall"],["Null"],["WallC","Wall"],["Wall"],["Wall","WallC", "Flat"]]# valid peices for each side
- WallPiece.obj = Wall
- pieceList.append(WallPiece)
- # WallCorner
- WallCorner = polyCube(n="WallCorner_Piece",ch=0)[0]
- parent(WallCorner,PieceGrp)
- delete(WallCorner.f[1:4])
- move(WallCorner, 0,.5,0)
- move(WallCorner.scalePivot, 0,0,0)
- move(WallCorner.rotatePivot, 0,0,0)
- makeIdentity(WallCorner,t=1,a=1)
- WallCornerPiece = BuildingPiece()
- WallCornerPiece.validId = ["WallC","Null","Null","Null","Null","Null"]
- WallCornerPiece.validKeys = ["WallC","Flat"]
- WallCornerPiece.validKeyList = [["Flat"],["Flat"],["WallC","Wall"],["WallC","Wall"],["Flat","Wall","WallC"],["Wall","WallC"]]# valid peices for each side
- WallCornerPiece.obj = WallCorner
- pieceList.append(WallCornerPiece)
- #
- # Flat
- Flat = polyPlane(n="Flat",sw=1,sh=1,ch=0)[0]
- parent(Flat,PieceGrp)
- #move(Flat, 0,-.5,0)
- #move(Flat.scalePivot, 0,0,0)
- #move(Flat.rotatePivot, 0,0,0)
- #makeIdentity(Flat,t=1,a=1)
- FlatPiece = BuildingPiece()
- FlatPiece.validId = ["Flat","Flat","Flat","Flat","Flat","Flat"]
- FlatPiece.validKeys = ["Flat", "Wall", "Null"]
- FlatPiece.validKeyList = [["WallC","Wall","Flat"],["WallC","Wall"],["Null"],["WallC","Wall"],["Null"],["Wall","WallC", "Null"]]# valid peices for each side
- FlatPiece.obj = Flat
- pieceList.append(FlatPiece)
- ###
- return pieceList
- ###
- # Get Bounding box objects
- BoundsGrp = ls("BoundsObjs")[0].listRelatives(c=1)
- BBList = []
- for bounds in BoundsGrp:
- BB = xform(bounds,bb=1,q=1)
- BBList.append(BB)
- sceneBB = GetSceneBounds(BBList, 1) # bb list, doRound
- #showBB(sceneBB)
- VoxelDispGrp = grpr("Vox_Disp_Objs")
- # Get Voxels in bounds
- toParent = []
- # Voxelize Scene
- VoxelList = VoxelizeFromBB(sceneBB)
- ###
- PieceList = MakePieces()
- PieceListlen = len(PieceList)
- doGround = 0
- VoxelNodeList = []
- ActiveVoxles = []
- for voxel in VoxelList:#xmin ymin zmin xmax ymax zmax.
- newNode = GridNode()
- newNode.pos = voxel
- for BB in BBList:
- bbCheck = isIn(BB, voxel)
- if bbCheck:
- newNode.isActive = True
- ActiveVoxles.append(newNode)
- # Assign random Piece
- newNode.GetRandomPeice(PieceList)
- VoxelNodeList.append(newNode)
- ActiveVoxlesLen = len(ActiveVoxles)
- ValidCombos = [[0,0],[0,1]]
- i = 0
- itterationAmnt = 1
- for i in range(0,itterationAmnt):
- for voxel in ActiveVoxles: # loop through voxels
- if not voxel.isCollapsed:
- voxel.GetNeighbors(ActiveVoxles)# get voxel neighbor
- voxelValidSides = voxel.buildingPiece.validId
- voxelValidKeys = voxel.buildingPiece.validKeys
- sideIsValid = False
- sideTracker = 0
- for side in voxel.SideList:# loop through neighbors
- if side != "Null":# is sky
- if voxel.rank >= side.rank:
- #for key in voxel.buildingPiece.validKeyList:
- sideListNeighborSide = GetNeighborSide(sideTracker)# index of array of neighboring node
- sideCheck = side.SideList[sideListNeighborSide]
- if sideCheck != "Null":
- print "Side:",voxel.buildingPiece.validId[sideTracker], "|Side checked:",sideCheck.buildingPiece.validId[sideListNeighborSide], "|Valid keys:",voxel.buildingPiece.validKeyList[sideTracker]
- if voxel.buildingPiece.validId[sideTracker] == sideCheck.buildingPiece.validId[sideListNeighborSide]:
- print "Match"
- sideIsValid = True
- """
- for validSide in voxelValidKeys:
- if side == validSide:
- side.rank += 1
- if side.rank == 6:
- side.isCollapsed = True
- """
- if not sideIsValid and side != "Null":
- side.GetRandomPeice(PieceList)
- sideTracker += 1
- #print "Current Sides:",voxel.SideList
- """
- rank = 0
- for id in currentId:
- print id
- for validKeys in currentValidKeys:
- if id == validKeys:
- rank += 1
- print voxel.buildingPiece.validKeys, rank
- ###
- #pick random voxel
- #get types for each side
- #get valid keys
- #get neighbors
- #for each side if not null
- #get side node
- #if rank less than 6 - pick highest rank
- #get lower rank node side
- check if neibor side is valid
- if valid rank + move on
- else check side node other sides
- if any side is valid
- rotate/bitshif
- else assign random piece.
- ###
- """
- # Voxel displaly
- for voxel in ActiveVoxles:
- #refresh(cw=1)
- #if voxel.rank > 1:
- d = duplicate(voxel.buildingPiece.obj)[0]
- #d = spaceLocator()
- #scale(d, .1,.1,.1)
- move(d, voxel.pos)
- rotate(d, 0,voxel.voxelShift[voxel.BitsShifted],0)
- voxel.obj = d
- toParent.append(d)
- l = spaceLocator()
- scale(l, .1,.1,.1)
- move(l, voxel.pos)
- toParent.append(l)
- parent(toParent, VoxelDispGrp)
- """
- #Neighbor test
- randomI = r.randint(0,len(ActiveVoxles)-1)
- randomVox = ActiveVoxles[randomI]
- scale(randomVox.obj, .2,.2,.2)
- for check in randomVox.CheckList:
- if check:
- scale(check.obj, .3,.3,.3)
- """
- ###
- """
- Model analisis
- for each tilePiece get valid peices for each side.
- # tile identified by #id/tag
- find tile peices
- get neighboring tiles
- if tile != null
- get side/peice
- remove duplicates
- add valid peices to tile sides
- """
- select(cl=1)
- elapsed = (time.clock() - start)
- print elapsed
Advertisement
Add Comment
Please, Sign In to add comment