nmwsharp / polyscope

A C++ & Python viewer for 3D data like meshes and point clouds
https://polyscope.run
MIT License
1.8k stars 196 forks source link

Addition to callbacks & custom UIs - Python #279

Open JPLost opened 4 months ago

JPLost commented 4 months ago

https://polyscope.run/py/features/callbacks_and_UIs/#creating-custom-uis

#If we want to use local variables & assign to them in the UI code below, 
# we need to mark them as nonlocal. This is because of how Python scoping 
# rules work, not anything particular about Polyscope or ImGui.
# Of course, you can also use any other kind of python variable as a controllable 
# value in the UI, such as a value from a dictionary, or a class member. Just be 
# sure to assign the result of the ImGui call to the value, as in the examples below.
# 
# If these variables are defined at the top level of a Python script file (i.e., not
# inside any method), you will need to use the `global` keyword instead of nonlocal`.

If you want to avoid using declarations global or nonlocal, you can also use different module to store these values and call it on main module as below

variables.py is_true: bool = False

main.py

import variables as vr

def callback()
        change, vr.is_true = psim.Checkbox("Selection box", vr.is_true)