blender labyrinth generator

Nikolaus Gradwohl2012-05-21T05:08:35+00:00

I ported my labyrinth generator from processing to a blender python script

you can download the blend file here

labyrinth

from random import shuffle from array import * import bpy

N=1
S=2
E=4
W=8

odir = {}
odir[N] = S
odir[S] = N
odir[W] = E
odir[E] = W

WIDTH=40
HEIGHT=40

coords = []
faces = []

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:
        print(i)
        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)

idx=0
step = 0.1

for x in range(0,WIDTH):
    for y in range( 0, HEIGHT ):
        if ( d[x][y] & N ) == 0:
            coords.append((x * step , y * step, 0 ))
            coords.append(((x+1) * step, y * step, 0 ))
            coords.append(((x+1) * step, y * step, step ))
            coords.append((x * step, y * step, step ))
            faces.append(( idx, idx+1, idx+2, idx+3))
            idx+=4
        if ( d[x][y] & S ) == 0:
            coords.append((x * step , (y+1) * step, 0 ))
            coords.append(((x+1) * step, (y+1) * step, 0 ))
            coords.append(((x+1) * step, (y+1) * step, step ))
            coords.append((x * step, (y+1) * step, step ))
            faces.append(( idx, idx+1, idx+2, idx+3))
            idx+=4
        if ( d[x][y] & E ) == 0:
            coords.append(((x+1) * step, y * step, 0 ))
            coords.append(((x+1) * step, (y+1) * step, 0 ))
            coords.append(((x+1) * step, (y+1) * step, step ))
            coords.append(((x+1) * step, y * step, step ))
            faces.append(( idx, idx+1, idx+2, idx+3))
            idx+=4
        if ( d[x][y] & W ) == 0:
            coords.append((x * step, y * step, 0 ))
            coords.append((x * step, (y+1) * step, 0 ))
            coords.append((x * step, (y+1) * step, step ))
            coords.append((x * step, y * step, step ))
            faces.append(( idx, idx+1, idx+2, idx+3))
            idx+=4

mesh = bpy.data.meshes.new(name="MyMesh")

object = bpy.data.objects.new( 'MESH', mesh )
bpy.context.scene.objects.link( object )

mesh.from_pydata( coords, [], faces )
mesh.update( calc_edges=True )
Tweet This! submit to reddit Digg! Tags: | 6 comments | no trackbacks

See also:

9^3 cubes
flying through the menger-sponge
grease pencil experiment 6 - grid
grease pencil experiment 4 - filled shapes
grease pencil experiment 4 - depth of field

Trackbacks

Comments

Leave a response

  1. hector 2013-03-12T11:13:12+00:00

    The program crash when you call carve(0,0)

  2. Jacob 2015-06-10T17:05:34+00:00

    Is it possible to adjust the width of the paths? Because I'm trying to make an object pathfind through it.

  3. Júnior 2017-01-12T14:52:17+00:00

    Hello, Can it be used for commercially? To produce games ...

  4. Nikolaus Gradwohl 2017-01-16T06:40:29+00:00

    @Júnior - yes. But I would love it if you give credits and add a link to it somewhere (for example in an about screen, download site, etc. )

  5. Nelly 2017-09-01T08:17:05+00:00

    Hi, could you please explain the code? I really can't get some fragments.. for Ex. odir = {} odir[N] = S odir[S] = N odir[W] = E odir[E] = W

    What does this do? Thank you in advance.

  6. Nikolaus Gradwohl 2017-09-05T06:04:19+00:00

    The odir map stores the opposite directions - for example the opposite direction for "North" is "South" so odir[N] = S and so on

Leave a comment