Skip to content

Fix(animation): Resolve bug in chained ReplacementTransform #4372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

HosseinShojaei
Copy link

@HosseinShojaei HosseinShojaei commented Aug 3, 2025

Overview: What does this pull request change?

This PR fixes a bug in ReplacementTransform where a chained sequence of animations fails if all involved mobjects are present on the scene from the start.

The change ensures that the internal state of the target mobject is not corrupted during the animation's cleanup phase, allowing it to be correctly used as a source in subsequent animations.

  • Fixes a bug causing chained ReplacementTransform animations to fail.

Motivation and Explanation: Why and how do your changes improve the library?

Currently, running a chain of ReplacementTransform animations (e.g., A -> B, then B -> C) fails after the second link in the chain.

The root cause lies in the clean_up_from_scene method of the Transform class. The original code uses scene.replace(self.mobject, self.target_mobject), which re-adds an existing mobject to the scene and corrupts its internal state.

This PR fixes the issue by replacing the scene.replace(...) call with a simpler scene.remove(self.mobject). This new approach only removes the redundant source mobject and never touches the target, preserving its state integrity for the next animation in the chain.

Visual Demonstration

Before (Buggy Behavior):
The animation incorrectly stops after the second transformation.

ChainedReplacementTransform.mp4

After (Corrected Behavior):
The animation chain now completes successfully.

ChainedReplacementTransform.mp4

Links to added or changed documentation pages

N/A

Further Information and Comments

The bug can be reproduced with the following minimal example.

from manim import *

class ChainedReplacementTransformBug(Scene):
    def construct(self):
        numbers = VGroup(*[Integer(i) for i in range(5)])
        numbers.arrange(direction=UP, buff=1)
        self.add(numbers)

        self.play(ReplacementTransform(numbers[0], numbers[1]))
        self.play(ReplacementTransform(numbers[1], numbers[2]))
        # Animation fails on the next line in the original code
        self.play(ReplacementTransform(numbers[2], numbers[3]))
        self.play(ReplacementTransform(numbers[3], numbers[4]))

        self.wait()

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New
Development

Successfully merging this pull request may close these issues.

1 participant