lib/glitch3d/bpy/canvas/lyfe.py in glitch3d-0.2.3.2 vs lib/glitch3d/bpy/canvas/lyfe.py in glitch3d-0.2.3.3

- old
+ new

@@ -1,62 +1,54 @@ # Game of life inspired scene -w = 40 -f = 1 -bpy.ops.anim.change_frame( frame = f ) +SIZE = 10 +DURATION=NUMBER_OF_FRAMES +SCENE.frame_start = 0 +SCENE.frame_end = DURATION +cubes = build_grid_object('CUBE', SIZE-1, 0.5, 1) -cubes = [[ 0 for i in range(w)] for j in range(w)] -for x in range(w): - for y in range(w): - bpy.ops.mesh.primitive_cube_add( location=(x * 2, y * 2, 0 )) - cubes[x][y] = bpy.context.active_object - cubes[x][y].data.materials.append( mat ); +cells = [[ 0 for i in range(SIZE)] for j in range(SIZE)] +next_generation = [[ 0 for i in range(SIZE)] for j in range(SIZE)] + +for x in range(SIZE): + for y in range(SIZE): cubes[x][y].scale=(.1,.1,.1) - bpy.ops.anim.keyframe_insert_menu( type='Scaling') + cells[x][y] = random.choice(range(2)) + make_object_gradient_fabulous(cubes[x][y], rand_color(), rand_color()) -cells = [[ 0 for i in range(w)] for j in range(w)] -nextGen = [[ 0 for i in range(w)] for j in range(w)] +def adjust_scale(): + for x in range(SIZE): + for y in range(SIZE): + if cells[x][y] == 1 : + cubes[x][y].scale=(random.uniform(0.4, 0.6), random.uniform(0.4, 0.6), random.uniform(0.4, 0.6)) + else: + cubes[x][y].scale=(.2,.2,.2) + for line in cubes: + add_frame(line) -cells[16][14] = 1 -cells[16][15] = 1 -cells[15][15] = 1 -cells[15][17] = 1 -cells[16][16] = 1 -cells[17][15] = 1 -cells[17][17] = 1 -cells[16][17] = 1 - -for l in range(50): - f += 5 - bpy.ops.anim.change_frame( frame = f ) - for x in range(w): - row = "" - for y in range(w): - nb = 0 +def life(l): + print("Life in " + str(l)) + for x in range(SIZE): + for y in range(SIZE): + neighbors_alive_count = 0 for i in range(-1,2): for j in range( -1, 2): - xx = (x + i + w) % w - yy = (y + j + w) % w - - if not( xx == x and yy == y): - nb += cells[xx][yy] - - if ( cells[x][y] == 1 and (nb == 2 or nb == 3)): - nextGen[x][y] = 1 - elif ( cells[x][y] == 0 and nb == 3 ): - nextGen[x][y] = 1 + x_index = (x + i + SIZE) % SIZE + y_index = (y + j + SIZE) % SIZE + if not( x_index == x and y_index == y): + neighbors_alive_count += cells[x_index][y_index] + if ( cells[x][y] == 1 and (neighbors_alive_count == 2 or neighbors_alive_count == 3)): + next_generation[x][y] = 1 + elif ( cells[x][y] == 0 and neighbors_alive_count == 3 ): + next_generation[x][y] = 1 else: - nextGen[x][y] = 0 + next_generation[x][y] = 0 + for x in range(SIZE): + for y in range(SIZE): + cells[x][y] = next_generation[x][y] + adjust_scale() - n = cubes[x][y] - bpy.context.scene.objects.active = n - n.select = True - if cells[x][y] == 1 : - #row += 'X' - cubes[x][y].scale=(1,1,1) - else: - #row += '.' - cubes[x][y].scale=(.1,.1,.1) - bpy.ops.anim.keyframe_insert_menu( type='Scaling') +print("Synthetic life begin") +adjust_scale() +for l in range(DURATION): + SCENE.frame_set(l) + life(l) - for x in range( w ): - for y in range(w): - cells[x][y] = nextGen[x][y] \ No newline at end of file