Allow creating scene objects without context #1213
Merged
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.
There has been confusion about creating scene objects (#1203).
Normally we always need to enter the scene context like this
or like this (after importing the class
Sphere
fromnicegui.elements.scene_objects
):But in case we already have a
scene
, it was unclear why we can't writeThe reason is that
scene.sphere
is a static attribute. Whensphere()
is called, it looks into the slot stack to find its scene. But if we didn't enter a scene context, the sphere can't find it.Of course, we could simply implement non-static factory methods that instantiate objects. But this would require lots of code duplication to mirror the class signatures and possibly docstrings.
This PR solves this problem by storing the
current_scene
when a scene is entered as well as when__getattribute__
is called for one of the static 3D object classes. Now the object initializer can simply usecurrent_scene
to get its own scene.To summarize, these are the possible methods to place objects in a scene: