Skip to content

Allow adding "computed submobjects" when they are stored as attributes of the parent mobjects #4339

@behackl

Description

@behackl

Description of problem

Sometimes we store child mobjects of some composed mobject as attributes, while also .add-ing them to the self.submobjects list directly.

This is fine in principle, but has the odd side effect of producing "mismatches" when a user updates the attribute by reassinging it, because the actually for rendering relevant self.submobjects list still holds a reference to the former attribute value.

As an example:

sq = Square()
b = BraceText(sq, "hello")  # this stores the label in `b.label`
b.label = Text("new value")  # and here we reassign the label ...

This example still renders as a brace with label "hello", because the reference in the self.submobjects list is never updated.

Proposed new feature

I think it would be a good idea to add some sort of mechanism that allows adding a "computed reference" to child objects when they are stored as attributes of the parent object. I wouldn't implement it as such, but think of

brace_object.add("label")

where internally just the attribute name is stored, but effectively getattr(brace_object, "label") is called whenever the actual mobject is used. (We'd probably need to reimplement the submobjects list as a computed property...)

I'm also not exactly sure of what the best interface for something like this would be. I don't like a fully general

brace_object.add(lambda: brace_object.label)

but perhaps something along the lines of

Mobject.add_computed_from_attribute(self, attribute_name: str)

would make sense?

Thoughts are very welcome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    new featureEnhancement specifically adding a new feature (feature request should be used for issues instead)

    Type

    No type

    Projects

    Status

    🆕 New

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions