# help with code

(sorry, I tried to make the title of the post a summary of its contents but I couldn’t came up with a phrase short enough)

Ok, so I’m new at this BPython stuff, and I’m trying to code a very simple 2D Cellular Automata field, applying the “rule of life”. You don’t have to know what those are to help me, though.

Basically, I’m creating a grid of NxN cubes and making them switch layers to create an “on/off” effect.

this is the code that I have:

import random
from Blender import *

#setup a “scene” and “mesh” that later functions refer to

es = Scene.GetCurrent()
mesh = Mesh.Primitives.Cube(1)

#create a “grid” dictionary where I store information on single cells of the grid

Grilla = {}

#a “cube” function, to create a cube

def Cubo(x,y,nombre):
ob = Object.New(“Mesh”,nombre)
ob.LocX=x
ob.LocY=y
return ob

#this function creates the NxN grid, and toggles cells randomly on or off(layer 2 or layer 1)

def Inicio(l):
for i in range(l):
for j in range(l):
nombre = str(i)+“x”+str(j)
cubo = Cubo(i,j,nombre)
cubo.Layer = random.randint(1,2)

#function to generate a neighborhood(list of strings to use as dictionary keys) for each cell

Vec = []
for i in range(-1,2):
for j in range(-1,2):
nombre = str(x+i)+“x”+str(y+j)
Vec.append(nombre)
return Vec

#function to analyze the neighborhood and return a the number of cells turned on in the neighborhood

def Regla(nombre):
cont = 0
for i in range(len(Grilla[nombre][1])):
celda = Grilla[nombre][1][i]
if Grilla.has_key(celda):
cont +=(Grilla[celda][0].Layer)-1
return cont

#function to make the grid advance a generation(for each grid in the cell it computes the neighborhood value and decides to turn it on or off)

def Generacion():
valores = Grilla.values()
for i in range(len(valores)):
ob = valores[i][0]
nombre = valores[i][0].name
vec = Regla(nombre)
if ob.Layer == 2:
if vec > 4 or vec < 2:
ob.Layer=1
if ob.Layer == 1:
if vec == 5:
ob.Layer=2

Inicio(20)
Redraw(-1)
for i in range(10):
Generacion()
Redraw(-1)
print "DONE

"

So far, it kinda works(the behavior doesn’t convince me, but that’s another story), except from one MAJOR issue:
after each run I “manually” delete all the cubes from the 3D window(from both layers). Yet somehow, the names of some cubes remain “taken”, so my code generates names like “name.00X”. Furthermore, the dictionary with the names of all the cubes doesn’t change, so I eventually I get a broken code because instead of using the key “name” I wind up using “name.00X”.
Basically, the code runs, but it only runs once. After that I have to paste it into a brand new file. I figure that strange behavior(the names of the cubes staying “taken”) has something to do with how I scripted it, but I can’t figure it out.

So if someone could please point out the mistake, I’d really appreciate it. And feel free to point any other rookie mistakes, if you want.

hi Cosimo, i have just only edited your last post for better readability: [quote]->

``````

``````

mport random
from Blender import *

#setup a “scene” and “mesh” that later functions refer to

es = Scene.GetCurrent()
mesh = Mesh.Primitives.Cube(1)

#create a “grid” dictionary where I store information on single cells of the grid

Grilla = {}

#a “cube” function, to create a cube

def Cubo(x,y,nombre):
ob = Object.New(“Mesh”,nombre)
ob.LocX=x
ob.LocY=y
return ob

#this function creates the NxN grid, and toggles cells randomly on or off(layer 2 or layer 1)

def Inicio(l):
for i in range(l):
for j in range(l):
nombre = str(i)+“x”+str(j)
cubo = Cubo(i,j,nombre)
cubo.Layer = random.randint(1,2)

#function to generate a neighborhood(list of strings to use as dictionary keys) for each cell

Vec = []
for i in range(-1,2):
for j in range(-1,2):
nombre = str(x+i)+“x”+str(y+j)
Vec.append(nombre)
return Vec

#function to analyze the neighborhood and return a the number of cells turned on in the neighborhood

def Regla(nombre):
cont = 0
for i in range(len(Grilla[nombre][1])):
celda = Grilla[nombre][1][i]
if Grilla.has_key(celda):
cont +=(Grilla[celda][0].Layer)-1
return cont

#function to make the grid advance a generation(for each grid in the cell it computes the neighborhood value and decides to turn it on or off)

def Generacion():
valores = Grilla.values()
for i in range(len(valores)):
ob = valores[i][0]
nombre = valores[i][0].name
vec = Regla(nombre)
if ob.Layer == 2:
if vec > 4 or vec < 2:
ob.Layer=1
if ob.Layer == 1:
if vec == 5:
ob.Layer=2

Inicio(20)
Redraw(-1)
for i in range(10):
Generacion()
Redraw(-1)
print "DONE

"