galatolofederico / manim-presentation

Tool for live presentations using manim
GNU General Public License v3.0
162 stars 18 forks source link

Often occuring "Index out of range" error #2

Closed linusheck closed 3 years ago

linusheck commented 3 years ago

Hi, thanks for the cool project! it often occurs that manim_presentation crashes with index out of bounds. For instance, consider this code (sorry it's not so minimal...)

It should stay on the slide with the second graph, but it just skips to the end and then crashes with "index out of bounds".

from manim import *
from manim_presentation import Slide 
red_edge = {
    "stroke_color": RED,
    "buff": 0
}
blue_edge = {
    "stroke_color": BLUE,
    "buff": 0
}
invisible_vertex = {
    "fill_opacity": 0
}
visible_vertex = {
    "fill_color": GREY
}

class ConstraintGraphScene(Slide):
    def construct(self):
        self.construct_vertices()
        self.clear()
        self.construct_latch()
        self.clear()
        self.play(Create(Text("")))
        self.pause()
        self.wait()

    def construct_latch(self):
        edge_config = {
            (2,1): blue_edge,
            (2,3): blue_edge,
            (4,2): blue_edge,
            (3,4): red_edge,
            (3,5): red_edge,
            (6,4): red_edge
        }
        vertex_config = {}
        for i in [1, 5, 6]:
            vertex_config[i] = invisible_vertex
        for i in [2, 3, 4]:
            vertex_config[i] = visible_vertex
        graph = Graph(vertex_config.keys(), edge_config.keys(), vertex_config=vertex_config, edge_config=edge_config, edge_type=Arrow)

        graph[1].move_to([-2,0,0])
        graph[2].move_to([-1,0,0])
        graph[3].move_to([0,1,0])
        graph[4].move_to([0,-1,0])
        graph[5].move_to([2,1,0])
        graph[6].move_to([2,-1,0])

        # graph.scale(1)

        self.play(Create(graph))
        self.play(Create(Text("Out A", color=BLACK).next_to(graph[6], RIGHT)))
        self.play(Create(Text("Out B", color=BLACK).next_to(graph[5], RIGHT)))
        self.play(Create(Text("Lock", color=BLACK).next_to(graph[1], LEFT)))
        self.pause()

    def construct_vertices(self):
        vertices = [1, 2, 3, 4]
        edges = [(1,3), (2,3), (3,4)]
        edge_config_and = {
            (1,3): red_edge,
            (2,3): red_edge,
            (3,4): blue_edge
        }
        edge_config_or = {
            (1,3): blue_edge,
            (2,3): blue_edge,
            (3,4): blue_edge
        }
        vertex_config = {
            1: invisible_vertex, 
            2: invisible_vertex,
            3: visible_vertex,
            4: invisible_vertex
        }
        graph_and_vertex = Graph(vertices, edges, edge_config=edge_config_and, vertex_config=vertex_config, edge_type=Arrow)
        graph_or_vertex = Graph(vertices, edges, edge_config=edge_config_or, vertex_config=vertex_config, edge_type=Arrow)
        graph_and_vertex.move_to(LEFT*2)
        graph_or_vertex.next_to(graph_and_vertex, RIGHT)
        graph_and_vertex.default_edge_config = blue_edge
        graph_or_vertex.default_edge_config = blue_edge

        and_vertex_text = Text("And", color=BLACK)
        and_vertex_text.next_to(graph_and_vertex, DOWN)
        or_vertex_text = Text("Or", color=BLACK)
        or_vertex_text.next_to(graph_or_vertex, DOWN)

        self.play(Create(graph_and_vertex))
        self.play(Create(graph_or_vertex))
        self.play(Create(and_vertex_text))
        self.play(Create(or_vertex_text))

        self.pause()

        self.start_loop()
        self.play(graph_and_vertex.animate.remove_edges((3,4)),
        graph_and_vertex.animate.add_edges((4,3), edge_type=Arrow))
        self.play(graph_and_vertex.animate.remove_edges((4,3)),
        graph_and_vertex.animate.add_edges((3,4), edge_type=Arrow))
        # self.play(graph.animate.add_edges((3,4)), edge_config=edge_config)
        self.end_loop()
        self.pause()
        self.wait()

I compiled the project with:

manim -ql constraint_graph_scene.py ConstraintGraphScene
manim_presentation ConstraintGraphScene
galatolofederico commented 3 years ago

Hi in the last lines you called a self.pause() right after the self.end_loop()

        self.end_loop()
        self.pause()
        self.wait()

I think that this is messing up with the indices because manim-presentation assumes that animations exists between the end of a loop and a pause (or even between two self.pause()). I should put some checks in the code and raise an error when this happens.

Anyway i removed the last self.pause()

        self.end_loop()
        #self.pause()
        self.wait()

and it works without crashing

linusheck commented 3 years ago

ahh! thanks :)