Skip to content

Conversation

SHARANcanCODE
Copy link

Description

This PR fixes issue #4240 where using next_section(skip_animations=True) or very short run times with the OpenGL renderer would cause a ValueError crash due to shape mismatches during interpolation.

Problem Analysis

The issue occurred in the OpenGL mobject interpolation method when:

  • Animations were skipped or had very short run times
  • The Write animation was used (though other animations could be affected)
  • The OpenGL renderer attempted to interpolate between mobjects with mismatched data shapes
  • Specifically: ValueError: could not broadcast input array from shape (27,3) into shape (3,3)

Changes Made

  • Added shape compatibility validation before interpolation in OpenGLMobject.interpolate()
  • Implemented graceful handling of shape mismatches by resizing target arrays when needed
  • Added fallback mechanism that uses mobject2 data when interpolation fails
  • Added comprehensive error handling for ValueError and TypeError during interpolation
  • Preserves normal animation functionality while preventing crashes

Testing

  • Verified that shape validation logic works correctly
  • Tested fallback mechanisms with different shape scenarios
  • The original failing code should now work without crashing

Fixes

Closes #4240

Example Usage

The following code (from the original issue) should now work without crashes:

from manim import *

class MyScene(Scene):
    def construct(self):
        self.next_section(skip_animations=True)
        c = Circle(color=RED, fill_opacity=1)
        self.play(Write(c))
        self.wait(2)
        self.next_section()
        s = Square(color=GREEN_B)
        self.play(Create(s))
        self.wait(1)

And this minimal reproduction case should also work:

from manim import *

class MyScene(Scene):
    def construct(self):
        c = Circle(color=RED, fill_opacity=1)
        self.play(Write(c), run_time=0.02)  # Very short run time

Deep Sharan added 2 commits August 31, 2025 20:56
…rs_to_display'

- Add missing default_numbers_to_display() method to NumberLine class
- Method returns tick range excluding numbers in numbers_to_exclude list
- Resolves issue where get_number_mobjects() would fail with AttributeError
- Fixes #4244
…animations

- Add shape validation before interpolation in OpenGL mobject
- Handle shape mismatches gracefully by resizing target arrays
- Implement fallback mechanism using mobject2 data when interpolation fails
- Prevents ValueError crashes during very short run times or skip_animations=True
- Fixes #4240
@henrikmidtiby
Copy link
Contributor

Thanks for your contribution.

It seems like your changes from PR #4414 are also included in this PR.
Could you please remove the changes in the file manim/mobject/graphing/number_line.py for this PR?

The change to opengl_mobject.py also triggers these two mypy errors.

manim/mobject/opengl/opengl_mobject.py:2694: error: Item "tuple[float, ...]" of "Any | tuple[float, float, float]" has no attribute "shape"  [union-attr]
manim/mobject/opengl/opengl_mobject.py:2698: error: Item "tuple[float, ...]" of "Any | tuple[float, float, float]" has no attribute "copy"  [union-attr]
Found 2 errors in 1 file (checked 166 source files)

I would suggest you to ignore these errors for now, by inserting this comment at the end of the two lines.

# type: ignore[union-attr]

Your changes solve the reported issue #4240 and passes the suite of unit tests, so they looks good to me.

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.

next_section(skip_animation = True) doesn't work with opengl renderer

2 participants