8
8
import traceback
9
9
10
10
from pygfx import Scene , OrthographicCamera , PanZoomController , OrbitController , \
11
- AxesHelper , GridHelper , WgpuRenderer
11
+ AxesHelper , GridHelper , WgpuRenderer , Texture
12
12
from wgpu .gui .auto import WgpuCanvas
13
+ from wgpu .gui .base import WgpuCanvasBase
14
+
15
+
16
+ # TODO: this determination can be better
17
+ try :
18
+ from wgpu .gui .jupyter import JupyterWgpuCanvas
19
+ except ImportError :
20
+ JupyterWgpuCanvas = False
21
+
22
+ try :
23
+ from wgpu .gui .qt import QWgpuCanvas
24
+ except ImportError :
25
+ QWgpuCanvas = False
26
+
27
+ try :
28
+ from wgpu .gui .glfw import GlfwWgpuCanvas
29
+ except ImportError :
30
+ GlfwWgpuCanvas = False
31
+
32
+
33
+ CANVAS_OPTIONS = ["jupyter" , "glfw" , "qt" ]
34
+ CANVAS_OPTIONS_AVAILABLE = {
35
+ "jupyter" : JupyterWgpuCanvas ,
36
+ "glfw" : GlfwWgpuCanvas ,
37
+ "qt" : QWgpuCanvas
38
+ }
13
39
14
40
from ._base import PlotArea
15
41
from .. import graphics
@@ -24,7 +50,7 @@ def __init__(
24
50
parent_dims : Tuple [int , int ] = None ,
25
51
camera : str = '2d' ,
26
52
controller : Union [PanZoomController , OrbitController ] = None ,
27
- canvas : WgpuCanvas = None ,
53
+ canvas : Union [ str , WgpuCanvas , Texture ] = None ,
28
54
renderer : WgpuRenderer = None ,
29
55
name : str = None ,
30
56
** kwargs
@@ -45,8 +71,10 @@ def __init__(
45
71
controller: PanZoomController or OrbitOrthoController, optional
46
72
``PanZoomController`` type is used for 2D pan-zoom camera control and ``OrbitController`` type is used for
47
73
rotating the camera around a center position, used to control the camera
48
- canvas: WgpuCanvas, optional
49
- provides surface on which a scene will be rendered
74
+ canvas: WgpuCanvas, Texture, or one of "jupyter", "glfw", "qt", optional
75
+ Provides surface on which a scene will be rendered. Can optionally provide a WgpuCanvas instance or a str
76
+ to force the PlotArea to use a specific canvas from one of the following options: "jupyter", "glfw", "qt".
77
+ Can also provide a pygfx Texture to render to.
50
78
renderer: WgpuRenderer, optional
51
79
object used to render scenes using wgpu
52
80
name: str, optional
@@ -55,6 +83,24 @@ def __init__(
55
83
if canvas is None :
56
84
canvas = WgpuCanvas ()
57
85
86
+ elif isinstance (canvas , str ):
87
+ if canvas not in CANVAS_OPTIONS :
88
+ raise ValueError (
89
+ f"str canvas argument must be one of: { CANVAS_OPTIONS } "
90
+ )
91
+ elif not CANVAS_OPTIONS_AVAILABLE [canvas ]:
92
+ raise ImportError (
93
+ f"The { canvas } framework is not installed for using this canvas"
94
+ )
95
+ else :
96
+ canvas = CANVAS_OPTIONS_AVAILABLE [canvas ]()
97
+
98
+ elif not isinstance (canvas , (WgpuCanvasBase , Texture )):
99
+ raise ValueError (
100
+ f"canvas option must either be a valid WgpuCanvas implementation, a pygfx Texture"
101
+ f" or a str from the following options: { CANVAS_OPTIONS } "
102
+ )
103
+
58
104
if renderer is None :
59
105
renderer = WgpuRenderer (canvas )
60
106
0 commit comments