Exemple #1
0
def test_vgroup_init(using_opengl_renderer):
    """Test the VGroup instantiation."""
    VGroup()
    VGroup(OpenGLVMobject())
    VGroup(OpenGLVMobject(), OpenGLVMobject())
    with pytest.raises(TypeError):
        VGroup(OpenGLMobject())
    with pytest.raises(TypeError):
        VGroup(OpenGLMobject(), OpenGLMobject())
def test_bracelabel_copy(using_opengl_renderer, tmp_path):
    """Test that a copy is a deepcopy."""
    # For this test to work, we need to tweak some folders temporarily
    original_text_dir = config["text_dir"]
    original_tex_dir = config["tex_dir"]
    mediadir = Path(tmp_path) / "deepcopy"
    config["text_dir"] = str(mediadir.joinpath("Text"))
    config["tex_dir"] = str(mediadir.joinpath("Tex"))
    for el in ["text_dir", "tex_dir"]:
        Path(config[el]).mkdir(parents=True)

    # Before the refactoring of OpenGLMobject.copy(), the class BraceLabel was the
    # only one to have a non-trivial definition of copy.  Here we test that it
    # still works after the refactoring.
    orig = BraceLabel(OpenGLMobject(), "label")
    copy = orig.copy()

    assert orig is orig
    assert orig is not copy
    assert orig.brace is not copy.brace
    assert orig.label is not copy.label
    assert orig.submobjects is not copy.submobjects
    assert orig.submobjects[0] is orig.brace
    assert copy.submobjects[0] is copy.brace
    assert orig.submobjects[0] is not copy.brace
    assert copy.submobjects[0] is not orig.brace

    # Restore the original folders
    config["text_dir"] = original_text_dir
    config["tex_dir"] = original_tex_dir
Exemple #3
0
def test_vgroup_add(using_opengl_renderer):
    """Test the VGroup add method."""
    obj = VGroup()
    assert len(obj.submobjects) == 0
    obj.add(OpenGLVMobject())
    assert len(obj.submobjects) == 1
    with pytest.raises(TypeError):
        obj.add(OpenGLMobject())
    assert len(obj.submobjects) == 1
    with pytest.raises(TypeError):
        # If only one of the added object is not an instance of OpenGLVMobject, none of them should be added
        obj.add(OpenGLVMobject(), OpenGLMobject())
    assert len(obj.submobjects) == 1
    with pytest.raises(ValueError):
        # a OpenGLMobject cannot contain itself
        obj.add(obj)
Exemple #4
0
def test_vdict_add(using_opengl_renderer):
    """Test the VDict add method."""
    obj = VDict()
    assert len(obj.submob_dict) == 0
    obj.add([("a", OpenGLVMobject())])
    assert len(obj.submob_dict) == 1
    with pytest.raises(TypeError):
        obj.add([("b", OpenGLMobject())])
Exemple #5
0
def test_scene_add_remove(using_opengl_renderer):
    with tempconfig({"dry_run": True}):
        scene = Scene()
        assert len(scene.mobjects) == 0
        scene.add(OpenGLMobject())
        assert len(scene.mobjects) == 1
        scene.add(*(OpenGLMobject() for _ in range(10)))
        assert len(scene.mobjects) == 11

        # Check that adding a mobject twice does not actually add it twice
        repeated = OpenGLMobject()
        scene.add(repeated)
        assert len(scene.mobjects) == 12
        scene.add(repeated)
        assert len(scene.mobjects) == 12

        # Check that Scene.add() returns the Scene (for chained calls)
        assert scene.add(OpenGLMobject()) is scene
        to_remove = OpenGLMobject()
        scene = Scene()
        scene.add(to_remove)
        scene.add(*(OpenGLMobject() for _ in range(10)))
        assert len(scene.mobjects) == 11
        scene.remove(to_remove)
        assert len(scene.mobjects) == 10
        scene.remove(to_remove)
        assert len(scene.mobjects) == 10

        # Check that Scene.remove() returns the instance (for chained calls)
        assert scene.add(OpenGLMobject()) is scene
def test_opengl_mobject_inheritance():
    mob = OpenGLMobject()
    a = OpenGLMobjectA()
    b = OpenGLMobjectB()
    c = OpenGLMobjectC()

    assert type(AnimationA1(mob)) is AnimationA1
    assert type(AnimationA1(a)) is AnimationA2
    assert type(AnimationA1(b)) is AnimationA2
    assert type(AnimationA1(c)) is AnimationA3
Exemple #7
0
    def __init__(
        self,
        mobject: Mobject | None,
        lag_ratio: float = DEFAULT_ANIMATION_LAG_RATIO,
        run_time: float = DEFAULT_ANIMATION_RUN_TIME,
        rate_func: Callable[[float], float] = smooth,
        reverse_rate_function: bool = False,
        name: str = None,
        remover: bool = False,  # remove a mobject from the screen?
        suspend_mobject_updating: bool = True,
        introducer: bool = False,
        *,
        _on_finish: Callable[[], None] = lambda _: None,
        **kwargs,
    ) -> None:
        self._typecheck_input(mobject)
        self.run_time: float = run_time
        self.rate_func: Callable[[float], float] = rate_func
        self.reverse_rate_function: bool = reverse_rate_function
        self.name: str | None = name
        self.remover: bool = remover
        self.introducer: bool = introducer
        self.suspend_mobject_updating: bool = suspend_mobject_updating
        self.lag_ratio: float = lag_ratio
        self._on_finish: Callable[[Scene], None] = _on_finish
        if config["renderer"] == "opengl":
            self.starting_mobject: OpenGLMobject = OpenGLMobject()
            self.mobject: OpenGLMobject = (mobject if mobject is not None else
                                           OpenGLMobject())
        else:
            self.starting_mobject: Mobject = Mobject()
            self.mobject: Mobject = mobject if mobject is not None else Mobject(
            )
        if kwargs:
            logger.debug("Animation received extra kwargs: %s", kwargs)

        if hasattr(self, "CONFIG"):
            logger.error((
                "CONFIG has been removed from ManimCommunity.",
                "Please use keyword arguments instead.",
            ), )
Exemple #8
0
def test_set_fill(using_opengl_renderer):
    m = OpenGLMobject()
    assert m.color.hex == "#fff"
    m.set_color(BLACK)
    assert m.color.hex == "#000"

    m = OpenGLVMobject()
    assert m.color.hex == "#fff"
    m.set_color(BLACK)
    assert m.color.hex == "#000"
def test_opengl_mobject_copy(using_opengl_renderer):
    """Test that a copy is a deepcopy."""
    orig = OpenGLMobject()
    orig.add(*(OpenGLMobject() for _ in range(10)))
    copy = orig.copy()

    assert orig is orig
    assert orig is not copy
    assert orig.submobjects is not copy.submobjects
    for i in range(10):
        assert orig.submobjects[i] is not copy.submobjects[i]
Exemple #10
0
def test_overlapping_family(using_opengl_renderer):
    """Check that each member of the family is only gathered once."""
    mob, child1, child2, = (
        OpenGLMobject(),
        OpenGLMobject(),
        OpenGLMobject(),
    )
    gchild1, gchild2, gchild_common = OpenGLMobject(), OpenGLMobject(), OpenGLMobject()
    child1.add(gchild1, gchild_common)
    child2.add(gchild2, gchild_common)
    mob.add(child1, child2)
    family = mob.get_family()
    assert mob in family
    assert len(family) == 6
    assert family.count(gchild_common) == 1
Exemple #11
0
def test_vdict_init(using_opengl_renderer):
    """Test the VDict instantiation."""
    # Test empty VDict
    VDict()
    # Test VDict made from list of pairs
    VDict([("a", OpenGLVMobject()), ("b", OpenGLVMobject()),
           ("c", OpenGLVMobject())])
    # Test VDict made from a python dict
    VDict({
        "a": OpenGLVMobject(),
        "b": OpenGLVMobject(),
        "c": OpenGLVMobject()
    })
    # Test VDict made using zip
    VDict(
        zip(["a", "b", "c"],
            [OpenGLVMobject(),
             OpenGLVMobject(),
             OpenGLVMobject()]))
    # If the value is of type OpenGLMobject, must raise a TypeError
    with pytest.raises(TypeError):
        VDict({"a": OpenGLMobject()})
Exemple #12
0
def test_family(using_opengl_renderer):
    """Check that the family is gathered correctly."""
    # Check that an empty OpenGLMobject's family only contains itself
    mob = OpenGLMobject()
    assert mob.get_family() == [mob]

    # Check that all children are in the family
    mob = OpenGLMobject()
    children = [OpenGLMobject() for _ in range(10)]
    mob.add(*children)
    family = mob.get_family()
    assert len(family) == 1 + 10
    assert mob in family
    for c in children:
        assert c in family

    # Nested children should be in the family
    mob = OpenGLMobject()
    grandchildren = {}
    for _ in range(10):
        child = OpenGLMobject()
        grandchildren[child] = [OpenGLMobject() for _ in range(10)]
        child.add(*grandchildren[child])
    mob.add(*list(grandchildren.keys()))
    family = mob.get_family()
    assert len(family) == 1 + 10 + 10 * 10
    assert mob in family
    for c in grandchildren:
        assert c in family
        for gc in grandchildren[c]:
            assert gc in family
Exemple #13
0
def test_set_color(using_opengl_renderer):
    m = OpenGLMobject()
    assert m.color.hex == "#fff"
    np.alltrue(m.rgbas == np.array((0.0, 0.0, 0.0, 1.0)))

    m.set_color(BLACK)
    assert m.color.hex == "#000"
    np.alltrue(m.rgbas == np.array((1.0, 1.0, 1.0, 1.0)))

    m.set_color(PURE_GREEN, opacity=0.5)
    assert m.color.hex == "#0f0"
    np.alltrue(m.rgbas == np.array((0.0, 1.0, 0.0, 0.5)))

    m = OpenGLVMobject()
    assert m.color.hex == "#fff"
    np.alltrue(m.fill_rgba == np.array((0.0, 0.0, 0.0, 1.0)))
    np.alltrue(m.stroke_rgba == np.array((0.0, 0.0, 0.0, 1.0)))

    m.set_color(BLACK)
    assert m.color.hex == "#000"
    np.alltrue(m.fill_rgba == np.array((1.0, 1.0, 1.0, 1.0)))
    np.alltrue(m.stroke_rgba == np.array((1.0, 1.0, 1.0, 1.0)))

    m.set_color(PURE_GREEN, opacity=0.5)
    assert m.color.hex == "#0f0"
    np.alltrue(m.fill_rgba == np.array((0.0, 1.0, 0.0, 0.5)))
    np.alltrue(m.stroke_rgba == np.array((0.0, 1.0, 0.0, 0.5)))
def test_opengl_mobject_add(using_opengl_renderer):
    """Test OpenGLMobject.add()."""
    """Call this function with a Container instance to test its add() method."""
    # check that obj.submobjects is updated correctly
    obj = OpenGLMobject()
    assert len(obj.submobjects) == 0
    obj.add(OpenGLMobject())
    assert len(obj.submobjects) == 1
    obj.add(*(OpenGLMobject() for _ in range(10)))
    assert len(obj.submobjects) == 11

    # check that adding a OpenGLMobject twice does not actually add it twice
    repeated = OpenGLMobject()
    obj.add(repeated)
    assert len(obj.submobjects) == 12
    obj.add(repeated)
    assert len(obj.submobjects) == 12

    # check that OpenGLMobject.add() returns the OpenGLMobject (for chained calls)
    assert obj.add(OpenGLMobject()) is obj
    obj = OpenGLMobject()

    # a OpenGLMobject cannot contain itself
    with pytest.raises(ValueError):
        obj.add(obj)

    # can only add OpenGLMobjects
    with pytest.raises(TypeError):
        obj.add("foo")
def test_opengl_mobject_remove(using_opengl_renderer):
    """Test OpenGLMobject.remove()."""
    obj = OpenGLMobject()
    to_remove = OpenGLMobject()
    obj.add(to_remove)
    obj.add(*(OpenGLMobject() for _ in range(10)))
    assert len(obj.submobjects) == 11
    obj.remove(to_remove)
    assert len(obj.submobjects) == 10
    obj.remove(to_remove)
    assert len(obj.submobjects) == 10

    assert obj.remove(OpenGLMobject()) is obj