-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
gui_subprocess.py
87 lines (63 loc) · 2.15 KB
/
gui_subprocess.py
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
"""
An example showing that with WGPU you can draw to the window of another
process. Just a proof of concept, this is far from perfect yet:
* It works if I run it in Pyzo, but not if I run it from the terminal.
* I only tried it on Windows.
* You'll want to let the proxy know about size changes.
* The request_draw should invoke a draw (in asyncio?), not draw directly.
* Properly handling closing the figure (from both ends).
"""
# run_example = false
import sys
import json
import time
import subprocess
from wgpu.gui import WgpuCanvasBase
# Import the function that we must call to run the visualization
from triangle import setup_drawing_sync
# from cube import setup_drawing_sync
code = """
import sys
import json
from PySide6 import QtWidgets # Use either PySide6 or PyQt6
from wgpu.gui.qt import WgpuCanvas
app = QtWidgets.QApplication([])
canvas = WgpuCanvas(title="wgpu triangle in Qt subprocess")
print(json.dumps(canvas.get_present_methods()))
print(canvas.get_physical_size())
sys.stdout.flush()
app.exec_()
"""
class ProxyCanvas(WgpuCanvasBase):
def __init__(self):
super().__init__()
self._present_methods = json.loads(p.stdout.readline().decode())
self._psize = tuple(
int(x) for x in p.stdout.readline().decode().strip().strip("()").split(",")
)
print(self._psize)
time.sleep(0.2)
def get_present_methods(self):
return self._present_methods
def get_physical_size(self):
return self._psize
def get_pixel_ratio(self):
return 1
def get_logical_size(self):
return self._psize
def set_logical_size(self, width, height):
pass
def close(self):
p.kill()
def is_closed(self):
raise NotImplementedError()
def _request_draw(self):
self.draw_frame()
# Create subprocess
p = subprocess.Popen([sys.executable, "-c", code], stdout=subprocess.PIPE)
# Create a canvas that maps to the window of that subprocess
canvas = ProxyCanvas()
# Go!
draw_frame = setup_drawing_sync(canvas)
canvas.request_draw(draw_frame)
time.sleep(3)