Advertisement
napland

maze generator

May 2nd, 2015
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.47 KB | None | 0 0
  1. #original script by Nikolaus Gradwohl 2012-05-21T05:08:35+02:00
  2. #http://www.local-guru.net/blog/2012/5/21/blender-labyrinth-generator
  3.  
  4. from random import shuffle
  5. from array import *
  6. import bpy
  7.  
  8. def GetName( cnt ):
  9.     if (cnt < 100 and cnt > 9):
  10.         return 'm0' + str(cnt)
  11.     if (cnt < 10):
  12.         return 'm00' + str(cnt)
  13.     return 'm' + str(cnt)
  14.    
  15. def MakeObject( coords , iter):
  16.     print("making " + GetName(iter))
  17.     mesh = bpy.data.meshes.new(GetName(iter))
  18.     object = bpy.data.objects.new( GetName(iter), mesh )
  19.     bpy.context.scene.objects.link( object )
  20.     mesh.from_pydata( coords, [], [(0,1,2,3)] )
  21.     mesh.update( calc_edges=True )
  22.     bpy.context.scene.objects.active = object
  23.     sol = object.modifiers.new('SLD' , 'SOLIDIFY')
  24.     sol.thickness = 2.5
  25.     bpy.ops.object.modifier_apply(apply_as='DATA', modifier="SLD")
  26.     return object
  27.  
  28. def CleanUp():
  29.     #bpy.ops.object.join()
  30.     #select the object, set cursor to 0,0,0 , set object geometry to center and move object to cursor (0,0,0)
  31.     bpy.context.scene.cursor_location = (0.0,0.0,0.0)  
  32.     bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')  
  33.     bpy.context.active_object.location = bpy.context.scene.cursor_location
  34.     bpy.ops.object.mode_set(mode='EDIT')
  35.     bpy.ops.mesh.remove_doubles()
  36.     bpy.ops.mesh.select_all(action='DESELECT')
  37.     bpy.ops.mesh.select_interior_faces()
  38.     bpy.ops.mesh.delete(type='FACE')
  39.     bpy.ops.mesh.select_all(action='SELECT')
  40.     bpy.ops.mesh.dissolve_limited(angle_limit=0.7853981634)
  41.  
  42. N=1
  43. S=2
  44. E=4
  45. W=8
  46.  
  47. odir = {}
  48. odir[N] = S
  49. odir[S] = N
  50. odir[W] = E
  51. odir[E] = W
  52.  
  53. WIDTH=9
  54. HEIGHT=9
  55. cellSize = 11.875
  56. wallWidth = 2.5
  57. zDepth = 10.0
  58. step = cellSize
  59.  
  60. objectList = []
  61.  
  62. d = [[0 for col in range(WIDTH)] for row in range(HEIGHT)]
  63.  
  64. for i in range(0,WIDTH):
  65.     for j in range(0, HEIGHT):
  66.         d[i][j] = 0
  67.    
  68. def carve( x, y ):
  69.     dirs = [N,S,W,E]
  70.     shuffle(dirs)
  71.    
  72.     for i in dirs:
  73.         nx = x
  74.         if i == E:
  75.             nx =  nx + 1
  76.         if i == W:
  77.             nx = nx - 1
  78.         ny = y
  79.         if i == S:
  80.             ny = ny +1
  81.         if i == N:
  82.             ny = ny - 1
  83.         if nx >= 0 and nx < WIDTH and ny >=0 and ny < HEIGHT and d[nx][ny] == 0:
  84.                 d[x][y] = d[x][y] | i
  85.                 d[nx][ny] = d[nx][ny]  | odir[i]
  86.                 carve(nx, ny)
  87.        
  88.  
  89. carve(0,0)
  90.  
  91. count = 0
  92. for x in range(0,WIDTH):
  93.     for y in range( 0, HEIGHT ):
  94.         if ( d[x][y] & N ) == 0:    
  95.             c = []  
  96.             c.append((x * step , y * step, 0 ))
  97.             c.append(((x+1) * step, y * step, 0 ))
  98.             c.append(((x+1) * step, y * step, zDepth ))
  99.             c.append((x * step, y * step, zDepth ))
  100.             objectList.append(MakeObject(c , count))
  101.             count += 1
  102.         if ( d[x][y] & S ) == 0:
  103.             c = []
  104.             c.append((x * step , (y+1) * step, 0 ))
  105.             c.append(((x+1) * step, (y+1) * step, 0 ))
  106.             c.append(((x+1) * step, (y+1) * step, zDepth ))
  107.             c.append((x * step, (y+1) * step, zDepth ))                              
  108.             objectList.append(MakeObject(c , count))
  109.             count += 1
  110.         if ( d[x][y] & E ) == 0:
  111.             c = []
  112.             c.append(((x+1) * step, y * step, 0 ))
  113.             c.append(((x+1) * step, (y+1) * step, 0 ))
  114.             c.append(((x+1) * step, (y+1) * step, zDepth ))
  115.             c.append(((x+1) * step, y * step, zDepth ))                                      
  116.             objectList.append(MakeObject(c , count))
  117.             count += 1
  118.         if ( d[x][y] & W ) == 0:  
  119.             c = []                        
  120.             c.append((x * step, y * step, 0 ))
  121.             c.append((x * step, (y+1) * step, 0 ))
  122.             c.append((x * step, (y+1) * step, zDepth ))
  123.             c.append((x * step, y * step, zDepth ))                                    
  124.             objectList.append(MakeObject(c , count))
  125.             count += 1
  126.  
  127. #for obj in objectList:
  128. #    bpy.context.scene.objects.active  = obj
  129. #    obj.select = True
  130.  
  131. mainObject = objectList[0]
  132. bpy.context.scene.objects.active  = mainObject
  133. mainObject.select = True
  134. for objToUnion in objectList:
  135.     if (objToUnion != mainObject):
  136.         boo = mainObject.modifiers.new('Booh' , 'BOOLEAN')
  137.         boo.object = objToUnion
  138.         boo.operation = 'UNION'
  139.         bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Booh")
  140.         bpy.context.scene.objects.unlink(objToUnion)
  141.        
  142. CleanUp()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement