Skip to content

Fix shared reference issue in traced_points initialization#2426

Open
mathsvisualization wants to merge 1 commit into3b1b:masterfrom
mathsvisualization:fix/traced-points-reference
Open

Fix shared reference issue in traced_points initialization#2426
mathsvisualization wants to merge 1 commit into3b1b:masterfrom
mathsvisualization:fix/traced-points-reference

Conversation

@mathsvisualization
Copy link
Contributor

@mathsvisualization mathsvisualization commented Feb 14, 2026

Motivation

While using TracingTail, I observed incorrect trail behavior.
The issue was caused by list multiplication during initialization:

self.traced_points = n_points * [curr_point]

This creates multiple references to the same NumPy array instead of independent copies. As a result, updating one point updated all stored points, producing visual artifacts and incorrect tracing behavior.

This PR fixes the issue by ensuring each traced point is an independent copy.

Proposed changes

  • Replaced list multiplication with list comprehension.
  • Ensured each stored traced point is created using .copy().
  • Prevented shared mutable reference bug in TracingTail.

Before

self.traced_points: list[np.ndarray] = n_points * [curr_point]

After

self.traced_points: list[np.ndarray] = [
    curr_point.copy() for _ in range(n_points)
]

Test

The issue can be reproduced using a simple pendulum example.

Code

from manimlib import *
import numpy as np

class SimplePendulum(Scene):
    def construct(self):
        # Parameters
        length = 3
        angle = -PI / 4
        origin = UP * 2

        # Pivot point
        pivot = Dot(origin)

        # Bob
        bob = Dot(radius=0.15, stroke_color=GREY)
        bob.move_to(origin + length * np.array([
            np.sin(angle),
            -np.cos(angle),
            0
        ]))

        # String
        string = always_redraw(
            lambda: Line(origin, bob.get_center(), stroke_color=WHITE)
        )

        # Trail
        trail = TracingTail(
            bob.get_center,
            time_traced=2.0,
            stroke_color=WHITE,
        )

        self.add(pivot, string, bob, trail)

        # Animation (oscillation)
        def update_bob(mob, dt):
            nonlocal angle
            angle += -0.8 * np.sin(angle) * dt
            mob.move_to(origin + length * np.array([
                np.sin(angle),
                -np.cos(angle),
                0
            ]))

        bob.add_updater(update_bob)

        self.wait(6)

Result

Before the fix:

  • Trail points overlapped incorrectly.
  • Multiple segments collapsed due to shared references.

IMG_20260214_133548

SimplePendulum.mp4

After the fix:

  • Trail updates correctly.
  • Each traced point is independent.
  • No visual artifacts appear.
SimplePendulum.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant