-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy path_utils.py
More file actions
90 lines (71 loc) · 2.37 KB
/
_utils.py
File metadata and controls
90 lines (71 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from typing import *
from pygfx import WgpuRenderer, Texture
# default auto-determined canvas
from wgpu.gui.auto import WgpuCanvas
from wgpu.gui.base import WgpuCanvasBase
# TODO: this determination can be better
try:
from wgpu.gui.jupyter import JupyterWgpuCanvas
except ImportError:
JupyterWgpuCanvas = False
try:
import PyQt6
from wgpu.gui.qt import QWgpuCanvas
except ImportError:
QWgpuCanvas = False
try:
from wgpu.gui.glfw import GlfwWgpuCanvas
except ImportError:
GlfwWgpuCanvas = False
CANVAS_OPTIONS = ["jupyter", "glfw", "qt"]
CANVAS_OPTIONS_AVAILABLE = {
"jupyter": JupyterWgpuCanvas,
"glfw": GlfwWgpuCanvas,
"qt": QWgpuCanvas,
}
def auto_determine_canvas():
try:
ip = get_ipython()
if ip.has_trait("kernel"):
if hasattr(ip.kernel, "app"):
if ip.kernel.app.__class__.__name__ == "QApplication":
return QWgpuCanvas
else:
return JupyterWgpuCanvas
except NameError:
pass
else:
if CANVAS_OPTIONS_AVAILABLE["qt"]:
return QWgpuCanvas
elif CANVAS_OPTIONS_AVAILABLE["glfw"]:
return GlfwWgpuCanvas
# We go with the wgpu auto guess
# for example, offscreen canvas etc.
return WgpuCanvas
def make_canvas_and_renderer(
canvas: Union[str, WgpuCanvas, Texture, None], renderer: [WgpuRenderer, None]
):
"""
Parses arguments and returns the appropriate canvas and renderer instances
as a tuple (canvas, renderer)
"""
if canvas is None:
Canvas = auto_determine_canvas()
canvas = Canvas(max_fps=60)
elif isinstance(canvas, str):
if canvas not in CANVAS_OPTIONS:
raise ValueError(f"str canvas argument must be one of: {CANVAS_OPTIONS}")
elif not CANVAS_OPTIONS_AVAILABLE[canvas]:
raise ImportError(
f"The {canvas} framework is not installed for using this canvas"
)
else:
canvas = CANVAS_OPTIONS_AVAILABLE[canvas](max_fps=60)
elif not isinstance(canvas, (WgpuCanvasBase, Texture)):
raise ValueError(
f"canvas option must either be a valid WgpuCanvas implementation, a pygfx Texture"
f" or a str from the following options: {CANVAS_OPTIONS}"
)
if renderer is None:
renderer = WgpuRenderer(canvas)
return canvas, renderer