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 creating scene objects without context #1213

Merged
merged 2 commits into from
Aug 1, 2023
Merged

Conversation

falkoschindler
Copy link
Contributor

@falkoschindler falkoschindler commented Jul 21, 2023

There has been confusion about creating scene objects (#1203).

Normally we always need to enter the scene context like this

with ui.scene() as scene:
    scene.sphere()

or like this (after importing the class Sphere from nicegui.elements.scene_objects):

with ui.scene():
    Sphere()

But in case we already have a scene, it was unclear why we can't write

scene.sphere()

The reason is that scene.sphere is a static attribute. When sphere() 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 use current_scene to get its own scene.

To summarize, these are the possible methods to place objects in a scene:

with ui.scene() as scene:
    scene.box()

with ui.scene():
    ui.scene.box()

with ui.scene():
    Box()

scene = ui.scene()
scene.box()

@falkoschindler falkoschindler added this to the 1.3.7 milestone Jul 27, 2023
@falkoschindler falkoschindler added the enhancement New feature or request label Jul 27, 2023
Copy link
Member

@rodja rodja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😎

@falkoschindler falkoschindler merged commit 9f8268c into main Aug 1, 2023
5 checks passed
@falkoschindler falkoschindler deleted the scene-objects branch August 1, 2023 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants