Ejemplo n.º 1
0
def animate_1d_automata(rule, nb_frames=10, scale=1., material_index=0):
    # Init automata
    automaton_size = nb_frames * 2
    automaton = Automaton1D(automaton_size, rule=rule)

    # Set middle cell as the only active one
    #automaton.grid = np.zeros(automaton_size, dtype=np.uint8)
    #automaton.grid[automaton_size // 2] = 1

    bpy.context.scene.frame_start = 0
    bpy.context.scene.frame_end = nb_frames

    gpencil_obj_name = "GPencil"
    gp_layer = init_grease_pencil(clear_layer=True,
                                  gpencil_obj_name=gpencil_obj_name)
    gp_frame = gp_layer.frames.new(0)

    gpencil = bpy.context.view_layer.objects[gpencil_obj_name]

    # center on middle cell
    cell_size = 1
    translate_vec = np.array([-(automaton_size / 2), 0, 0])
    for frame in range(nb_frames):
        gp_frame = gp_layer.frames.copy(gp_frame)
        for i, cell in enumerate(automaton.grid):
            # maintain pyramid shape (render only if further from the center at least the current frame number)
            if cell and ((automaton_size // 2 - frame) <= i <=
                         (automaton_size // 2 + frame)):
                # render cell
                centers = [
                    (i, frame, 0),  # normal center
                    #(i, frame - 1, automaton_size // 2 - frame),  # center down
                    #(i, frame - 1, -(automaton_size // 2) + frame),  # center up
                ]
                for center in centers:
                    centers_shifted = np.array(center) + translate_vec
                    draw_square(gp_frame,
                                centers_shifted,
                                cell_size,
                                material_index=material_index)
                    #draw_cube(gp_frame, centers_shifted, cell_size, material_index=material_index)

        automaton.update()

    # scale automaton size along the growth axis
    if scale != 1.:
        gpencil.scale[0] = scale
        gpencil.select_set(True)
        bpy.ops.object.transform_apply(location=False,
                                       rotation=False,
                                       scale=True)
Ejemplo n.º 2
0
def animate_2d_automata(rule, nb_frames: 10, use_grease_pencil=True):
    nb_rows = 10
    nb_cols = 10
    gol = Automaton2D(nb_rows, nb_cols, rule, seed=11)

    FRAMES_SPACING = 1
    bpy.context.scene.frame_start = 0
    bpy.context.scene.frame_end = nb_frames * FRAMES_SPACING

    if use_grease_pencil:
        gp_layer = init_grease_pencil(clear_layer=True)
    else:
        obj_size = 0.7
        subdivisions = 2
        scale_factor = 0.2
        init_mat_color = (0.7, 0.1, 0.1)

        # obj_generator = lambda idx: automata_blender_utils.icosphere_generator(obj_size, subdivisions, idx[0], idx[1], 0)
        obj_generator = lambda idx: automata_blender_utils.cube_generator(
            obj_size, idx[0], idx[1], 0)

        obj_updater = lambda obj, grid, idx: automata_blender_utils.object_updater_hide(
            obj, grid[idx])
        # obj_updater = lambda obj, grid, idx: automata_blender_utils.object_updater_scale(obj, grid[idx],
        #                                                                                 scale_factor=scale_factor)
        # obj_updater = lambda obj, grid, idx: automata_blender_utils.object_updater_color_vector(
        # obj, grid[:, idx[0], idx[1]])

        delete_all()
        obj_grid = automata_blender_utils.create_grid(gol, obj_generator)
        # automata_blender_utils.init_materials(obj_grid, init_mat_color)

    gol.update()
    for frame in range(nb_frames):
        if use_grease_pencil:
            gp_frame = gp_layer.frames.new(frame * FRAMES_SPACING)
            for idx, val in np.ndenumerate(gol.grid):
                if val:
                    draw_square(gp_frame, (idx[0], idx[1], 0), 1)
        else:
            bpy.context.scene.frame_set(frame)
            automata_blender_utils.update_grid(obj_grid, gol, obj_updater)
        gol.update()