Fix(animation): Resolve bug in chained ReplacementTransform #4372
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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
, thenB -> C
) fails after the second link in the chain.The root cause lies in the
clean_up_from_scene
method of theTransform
class. The original code usesscene.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 simplerscene.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.
Reviewer Checklist