Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Building Gen v0.01
- 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)
- 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
- def MakePieces():
- pieceList = []
- PieceGrp = grpr("PiecesGrp")
- # Wall
- Wall = polyCube(n="Wall_Piece",ch=0)[0]
- parent(Wall,PieceGrp)
- delete([Wall.f[1],Wall.f[3]])
- WallPiece = BuildingPiece()
- WallPiece.validId = ["Wall","Wall","Wall","Wall","Null","Null"]
- WallPiece.validKeys = ["Wall","Flat"]
- WallPiece.obj = Wall
- pieceList.append(WallPiece)
- ###
- # 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","Null","Flat"]
- FlatPiece.validKeys = ["Flat", "Wall", "Null"]
- FlatPiece.obj = Flat
- pieceList.append(FlatPiece)
- ###
- return pieceList
- 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.CheckList = [self.nodeZ,self.nodeXN,self.nodeZN,self.nodeX ,self.nodeY ,self.nodeYN]
- self.SideList = ["Null","Null","Null","Null","Null","Null"]
- def GetSides(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
- # 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)
- #FlatPiece.validId = ["Flat","Flat","Flat","Flat","Null","Flat"] # what the sides are
- #FlatPiece.validKeys = ["Flat", "Wall", "Null"] # valid connections too sides
- ActiveVoxlesLen = len(ActiveVoxles)
- ValidCombos = [[0,0],[0,1]]
- i = 0
- itterationAmnt = 1
- for i in range(0,itterationAmnt):
- for voxel in ActiveVoxles:
- if not voxel.isCollapsed:
- voxel.GetSides(ActiveVoxles)
- if voxel.SideList[5] == "Null":#sky
- voxel.buildingPiece= PieceList[1]
- voxel.isCollapsed = True
- #doNewPiece = False
- """
- doNewPiece = True
- for key in voxel.buildingPiece.validKeys:
- if key == "Null":
- voxel.buildingPiece= PieceList[1]
- voxel.isCollapsed = True
- doNewPiece = False
- if doNewPiece:
- voxel.GetRandomPeice(PieceList)
- """
- else:
- voxel.buildingPiece= PieceList[0]
- voxel.isCollapsed = True
- #voxel.GetRandomPeice(PieceList)
- #print voxel.SideList[5].buildingPiece.validId[5]
- """
- s = 0
- for side in voxel.SideList:
- if side != "Null":
- for key in voxel.buildingPiece.validKeys:
- if key == side.buildingPiece.validId[s]:
- print "Up: ", side.buildingPiece.validId[5], voxel.buildingPiece.obj, "Obj key:", key, "Side piece: ", side.buildingPiece.validId[s], "S: ", s
- #if voxel.buildingPiece.validKeys, side.buildingPiece.validId:
- #print "Current Sides ", voxel.buildingPiece.validKeys, side.buildingPiece.validId
- s+=1
- currentId = voxel.buildingPiece.validId
- currentValidKeys = voxel.buildingPiece.validKeys
- rank = 0
- for id in currentId:
- doRankUp = False
- for validKey in currentValidKeys:
- if id == validKey:
- print "Valid: ", id, validKey
- doRankUp = True
- if doRankUp:
- rank += 1
- print voxel.buildingPiece.validKeys, rank
- """
- # get sides
- # get current node type
- # check neighbor node types
- # for each side node
- # if valid node type matches neighbor node type
- # side = valid
- # rank += 1
- # if not matches
- # check side ranks
- # change lowest ranged node
- #print "Voxel id ",voxel.buildingPiece.validId
- #voxel.buildingPiece.validId
- # Voxel displaly
- for voxel in ActiveVoxles:
- if voxel.isCollapsed:
- #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)
- #a = annotate(c, tx=str(i), p = voxel.pos)
- #setAttr(a+".displayArrow", 0)
- #toParent.append(a)
- """
- 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)
- """
- select(cl=1)
- elapsed = (time.clock() - start)
- print elapsed
Add Comment
Please, Sign In to add comment