Skip to content

ImageMobjects are not properly hashed #2924

Open
@NicoWeio

Description

@NicoWeio

Description of bug / unexpected behavior

I found that certain operations on ImageMobjects do not invalidate the animation cache, that is, they don't change the mobject's hash value.
In consequence, in a typical workflow of repeated renders, Manim sometimes generates videos that do not match the code they were generated with.

This is because the pixel_array is explicitly filtered out of the hashing operation:

KEYS_TO_FILTER_OUT = {
"original_id",
"background",
"pixel_array",
"pixel_array_to_cairo_context",
}

Expected behavior

All (visible) modifications to an ImageMobject should result in its hash being different.

How to reproduce the issue

Code for reproducing the problem
  1. Run the provided code (manim filename.py without a manim.cfg)
  2. Change the alpha value
  3. Run it again
  4. Manim inappropriately re-uses the cached animation.
from manim import *

class TestImageCaching(Scene):
    def construct(self):
        im = ImageMobject('example.png')

        # change here           ↓↓↓
        im.set_color(RED, alpha=0.2)

        self.add(im)
        self.wait()

Additional comments

The most obvious “fix” would be to remove "pixel_array" from the exclude list – which, considering that #2923 works, doesn't break Manim – but I assume it's there for a reason.
Edit: As you can see in the code snipped below, #2923 “works”, because a repr of the (truncated) array is hashed, not all of its values. In other words, this only works if you're lucky.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    🆕 New

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions