Created
October 27, 2015 17:38
-
-
Save Naphier/0d7db321981073594c3c to your computer and use it in GitHub Desktop.
Maze generator script for python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#original script by Nikolaus Gradwohl 2012-05-21T05:08:35+02:00 | |
#http://www.local-guru.net/blog/2012/5/21/blender-labyrinth-generator | |
from random import shuffle | |
from array import * | |
import bpy | |
def GetName( cnt ): | |
if (cnt < 100 and cnt > 9): | |
return 'm0' + str(cnt) | |
if (cnt < 10): | |
return 'm00' + str(cnt) | |
return 'm' + str(cnt) | |
def MakeObject( coords , iter): | |
print("making " + GetName(iter)) | |
mesh = bpy.data.meshes.new(GetName(iter)) | |
object = bpy.data.objects.new( GetName(iter), mesh ) | |
bpy.context.scene.objects.link( object ) | |
mesh.from_pydata( coords, [], [(0,1,2,3)] ) | |
mesh.update( calc_edges=True ) | |
bpy.context.scene.objects.active = object | |
sol = object.modifiers.new('SLD' , 'SOLIDIFY') | |
sol.thickness = 2.5 | |
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="SLD") | |
return object | |
def CleanUp(): | |
#bpy.ops.object.join() | |
#select the object, set cursor to 0,0,0 , set object geometry to center and move object to cursor (0,0,0) | |
bpy.context.scene.cursor_location = (0.0,0.0,0.0) | |
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY') | |
bpy.context.active_object.location = bpy.context.scene.cursor_location | |
bpy.ops.object.mode_set(mode='EDIT') | |
bpy.ops.mesh.remove_doubles() | |
bpy.ops.mesh.select_all(action='DESELECT') | |
bpy.ops.mesh.select_interior_faces() | |
bpy.ops.mesh.delete(type='FACE') | |
bpy.ops.mesh.select_all(action='SELECT') | |
bpy.ops.mesh.dissolve_limited(angle_limit=0.7853981634) | |
N=1 | |
S=2 | |
E=4 | |
W=8 | |
odir = {} | |
odir[N] = S | |
odir[S] = N | |
odir[W] = E | |
odir[E] = W | |
WIDTH=9 | |
HEIGHT=9 | |
cellSize = 11.875 | |
wallWidth = 2.5 | |
zDepth = 10.0 | |
step = cellSize | |
objectList = [] | |
d = [[0 for col in range(WIDTH)] for row in range(HEIGHT)] | |
for i in range(0,WIDTH): | |
for j in range(0, HEIGHT): | |
d[i][j] = 0 | |
def carve( x, y ): | |
dirs = [N,S,W,E] | |
shuffle(dirs) | |
for i in dirs: | |
nx = x | |
if i == E: | |
nx = nx + 1 | |
if i == W: | |
nx = nx - 1 | |
ny = y | |
if i == S: | |
ny = ny +1 | |
if i == N: | |
ny = ny - 1 | |
if nx >= 0 and nx < WIDTH and ny >=0 and ny < HEIGHT and d[nx][ny] == 0: | |
d[x][y] = d[x][y] | i | |
d[nx][ny] = d[nx][ny] | odir[i] | |
carve(nx, ny) | |
carve(0,0) | |
count = 0 | |
for x in range(0,WIDTH): | |
for y in range( 0, HEIGHT ): | |
if ( d[x][y] & N ) == 0: | |
c = [] | |
c.append((x * step , y * step, 0 )) | |
c.append(((x+1) * step, y * step, 0 )) | |
c.append(((x+1) * step, y * step, zDepth )) | |
c.append((x * step, y * step, zDepth )) | |
objectList.append(MakeObject(c , count)) | |
count += 1 | |
if ( d[x][y] & S ) == 0: | |
c = [] | |
c.append((x * step , (y+1) * step, 0 )) | |
c.append(((x+1) * step, (y+1) * step, 0 )) | |
c.append(((x+1) * step, (y+1) * step, zDepth )) | |
c.append((x * step, (y+1) * step, zDepth )) | |
objectList.append(MakeObject(c , count)) | |
count += 1 | |
if ( d[x][y] & E ) == 0: | |
c = [] | |
c.append(((x+1) * step, y * step, 0 )) | |
c.append(((x+1) * step, (y+1) * step, 0 )) | |
c.append(((x+1) * step, (y+1) * step, zDepth )) | |
c.append(((x+1) * step, y * step, zDepth )) | |
objectList.append(MakeObject(c , count)) | |
count += 1 | |
if ( d[x][y] & W ) == 0: | |
c = [] | |
c.append((x * step, y * step, 0 )) | |
c.append((x * step, (y+1) * step, 0 )) | |
c.append((x * step, (y+1) * step, zDepth )) | |
c.append((x * step, y * step, zDepth )) | |
objectList.append(MakeObject(c , count)) | |
count += 1 | |
#for obj in objectList: | |
# bpy.context.scene.objects.active = obj | |
# obj.select = True | |
mainObject = objectList[0] | |
bpy.context.scene.objects.active = mainObject | |
mainObject.select = True | |
for objToUnion in objectList: | |
if (objToUnion != mainObject): | |
boo = mainObject.modifiers.new('Booh' , 'BOOLEAN') | |
boo.object = objToUnion | |
boo.operation = 'UNION' | |
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Booh") | |
bpy.context.scene.objects.unlink(objToUnion) | |
CleanUp() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment