Skip to content
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

Allow passing iterables to Scene.add() and improve comments for flatten_iterable_parameters() #4190

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

irvanalhaq9
Copy link
Contributor

@irvanalhaq9 irvanalhaq9 commented Mar 14, 2025

Overview: What does this pull request change?

This PR allows Scene.add() to accept iterable inputs directly, without requiring explicit unpacking. It also improves comments for flatten_iterable_parameters() in Scene.compile_animations() to clarify its purpose.

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

Currently, Scene.add() requires unpacking when passing an iterable of mobjects (e.g., self.add(*mobjects)). This PR modifies Scene.add() to use flatten_iterable_parameters(), allowing users to pass iterables directly (e.g., self.add(mobjects)). This improves consistency with Scene.play(), which already supports this behavior.
Additionally, comments for flatten_iterable_parameters() have been improved to clarify that it allows passing iterables—including generators—to self.play() without explicit unpacking.

self.play() can already accept both unpacked and non-unpacked iterables because it utilizes function flatten_iterable_parameters(). This PR applies the same approach to self.add(), allowing it to accept iterables directly without requiring unpacking.

Enhancement: Allow self.add() to accept iterables without unpacking

Current Behavior

Before this PR, self.play() already accepts both unpacked and non-unpacked iterables:

With unpacking:

class IterableWithSelfPlay(Scene): 
    def construct(self):
        st, sq, c = Star(), Square().shift(RIGHT*3), Circle().shift(LEFT*3)
        animations = [Create(st), Write(sq), FadeIn(c)]
        self.play(*animations, run_time=2)

Without unpacking:

class IterableWithSelfPlay(Scene): 
    def construct(self):
        st, sq, c = Star(), Square().shift(RIGHT*3), Circle().shift(LEFT*3)
        animations = [Create(st), Write(sq), FadeIn(c)]
        self.play(animations, run_time=2)

Both approaches work as expected.

However, self.add() requires unpacking:

class IterableWithSelfAdd(Scene): 
    def construct(self):
        grp = [Star(), Square().shift(RIGHT*3), Circle().shift(LEFT*3)]
        self.add(*grp)  # Unpacking is required

New Behavior

With this PR, self.add() can now accept iterables without unpacking:

class IterableWithSelfAdd(Scene): 
    def construct(self):
        grp = [Star(), Square().shift(RIGHT*3), Circle().shift(LEFT*3)]
        self.add(grp)  # Now works without unpacking

This change makes self.add() consistent with self.play() in handling iterables.

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

@irvanalhaq9 irvanalhaq9 marked this pull request as draft March 14, 2025 04:17
@irvanalhaq9 irvanalhaq9 marked this pull request as ready for review March 14, 2025 13:02
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