Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pymel.core import *
- import time
- import random as r
- import math
- start = time.clock()
- #
- """
- 0 = ground
- 1 = road
- 3 = wall
- 4 = roof
- 5 = roof wall
- 6 = wall to ground
- 8 = under
- 9 = above
- """
- class GridNode:
- voxelShift = [90,180,270]
- def __init__(self):
- self.pos = []
- self.obj = []
- self.isActive = False # is inside bounding box
- self.rank = 0 # how many sides match
- self.validId = 00000 # numbers to match
- self.pieceId = 0 # object index in group
- self.BitsShifted = 0 # rotation amount
- def GetPeice(self):
- PiecesGrp = ls("Pieces")[0].listRelatives(c=1)
- PiecesGrpLen = len(PiecesGrp)
- randomPiece = r.randint(0,PiecesGrpLen-1)
- self.pieceId = randomPiece
- validObj = PiecesGrp[voxel.pieceId].listRelatives(c=0)[-1]
- validObjValidId = getAttr(validObj.getShape()+".text")
- self.validId = validObjValidId
- def ShiftBits(self,amount):
- newId = self.validId[-amount:]+ self.validId[:-amount]
- newId2 = self.validId[:-amount]
- self.validId = newId
- 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])+abs(BB[3])+1)
- yRange = int(abs(BB[1])+abs(BB[4])+1)
- zRange = int(abs(BB[2])+abs(BB[5])+1)
- for z in range(0,xRange):
- for y in range(0,yRange):
- for x in range(0,zRange):
- VoxelPosition = [x + BB[0], y + BB[1], z + BB[2]]
- VoxelListRaw.append(VoxelPosition)
- return VoxelListRaw
- # ThingsGrp = ls("ThingsGrp")[0].listRelatives(c=1)
- #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
- #sceneBBCenter = (sceneBB[0]+sceneBB[3])/2, (sceneBB[1]+sceneBB[4])/2, (sceneBB[2]+sceneBB[5])/2
- #showBB(sceneBB)
- VoxelDispGrp = grpr("Vox_Disp_Objs")
- # Get Voxels in bounds
- toParent = []
- VoxelList = VoxelizeFromBB(sceneBB)
- doGround = 1
- doDislpay = 0
- VoxelNodeList = []
- for voxel in VoxelList:#xmin ymin zmin xmax ymax zmax.
- newNode = GridNode()
- newNode.pos = voxel
- VoxelNodeList.append(newNode)
- for BB in BBList:
- bbCheck = isIn(BB, voxel)
- if bbCheck:
- newNode.isActive = True
- if doGround:
- #if voxel[1] < .2 and voxel[1] > -.2:
- if voxel[1] == 0:
- newNode.isActive = True
- if newNode.isActive:
- if doDislpay:
- c = spaceLocator()
- a = annotate(c, tx=str(voxel), p = voxel)
- setAttr(a+".displayArrow", 0)
- toParent.append(a)
- move(c, voxel)
- scale(c, .1,.1,.1)
- toParent.append(c)
- parent(toParent, VoxelDispGrp)
- #Voxel calculate
- lastActive = []
- for voxel in VoxelNodeList:
- if voxel.isActive:
- voxel.GetPeice()
- lastActive = voxel
- #print lastActive.validId[0]
- #lastActive.ShiftBits(1)
- #print lastActive.validId
- i = 0
- end = len(VoxelNodeList)
- for voxel in VoxelNodeList:
- if i != end:
- if voxel.isActive and VoxelNodeList[i+1].isActive:
- for x in range(0,5):
- if voxel.validId[x] == VoxelNodeList[i+1].validId[x]:
- voxel.rank += 1
- i += 1
- PiecesGrp = ls("Pieces")[0].listRelatives(c=1)
- PiecesGrpLen = len(PiecesGrp)
- ActiveVoxles = []
- # Voxel displaly
- for voxel in VoxelNodeList:
- if voxel.isActive and voxel.rank > 1:
- #refresh(cw=1)
- d = duplicate(PiecesGrp[voxel.pieceId])[0]
- move(d, voxel.pos)
- voxel.obj = d
- toParent.append(d)
- ActiveVoxles.append(voxel)
- parent(toParent, VoxelDispGrp)
- randomI = r.randint(0,len(ActiveVoxles)-1)
- randomVox = ActiveVoxles[randomI]
- #xmin ymin zmin xmax ymax zmax.
- xRange = int(abs(BB[0])+abs(BB[3])+1)
- yRange = int(abs(BB[1])+abs(BB[4])+1)
- zRange = int(abs(BB[2])+abs(BB[5])+1)
- pos = randomVox.pos
- xI = pos[0] + xRange * pos[1] + xRange * yRange * pos[2]
- print xI
- scale(ActiveVoxles[int(xI)], .3,.3,.3)
- """
- if pos.y < gridSizeY-1:
- xY = pos[0] + gridSizeX * (pos[1]+1) + gridSizeX * gridSizeY * pos[2]
- scale(ActiveVoxles[int(xY)], .1,.1,.1)
- """
- select(cl=1)
- elapsed = (time.clock() - start)
- print elapsed
Advertisement
Add Comment
Please, Sign In to add comment