69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803 | class Viewer:
"""PyGFX 3D viewer.
Parameters
----------
offscreen : bool, optional
If True, will use an offscreen Canvas. Useful if you only
want a screenshot.
title : str, optional
Title of the viewer window.
max_fps : int, optional
Maximum frames per second to render.
size : tuple, optional
Size of the viewer window.
camera : "ortho" | "perspective", optional
Type of camera to use. Defaults to "ortho". Note you can always
change the camera type by adjust the `Viewer.camera.fov` attribute
(0 = ortho, >0 = perspective).
control : "trackball" | "panzoom" | "fly" | "orbit"
Controller type to use. Defaults to "trackball".
show : bool
Whether to immediately show the viewer. A few notes:
1. When running in a non-interactive script or REPL, you have to also start
the event loop manually. See the `Viewer.show()` method for more information.
**kwargs
Keyword arguments are passed through to ``WgpuCanvas``.
"""
# Palette used for assigning colors to objects
palette = "seaborn:tab10"
def __init__(
self,
offscreen=False,
title="Octarine Viewer",
max_fps=30,
camera="ortho",
control="trackball",
size=None,
show=True,
**kwargs,
):
# We need to import WgpuCanvas before we (potentially) start the event loop
# If we don't we get a segfault.
if not offscreen:
from wgpu.gui.auto import WgpuCanvas
# Check if we're running in an IPython environment
if utils._type_of_script() == "ipython":
ip = get_ipython() # noqa: F821
if not ip.active_eventloop:
if AUTOSTART_EVENT_LOOP:
try:
ip.enable_gui("qt6")
logger.debug(
"Looks like you're running in an IPython environment but haven't "
"started a GUI event loop. We've started one for you using the "
"Qt6 backend."
)
except ModuleNotFoundError:
raise ValueError(
"Looks like you're running in an IPython environment but haven't "
"started a GUI event loop. We tried to start one for you using the "
"Qt6 backend (via %gui qt6) but that failed. You may have to start "
"the event loop manually. See "
"https://ipython.readthedocs.io/en/stable/config/eventloops.html"
"for details."
)
else:
raise ValueError(
'IPython event loop not running. Please use e.g. "%gui qt6" to hook into the event loop.'
)
self._title = title
# Update some defaults as necessary
defaults = {"title": title, "max_fps": max_fps, "size": size}
defaults.update(kwargs)
# If we're running in headless mode (primarily for tests on CI) we will
# simply not initialize the gfx objects. Not ideal but it turns
# out to be very annoying to correctly setup on Github Actions.
if getattr(config, "HEADLESS", False):
return
if not offscreen:
self.canvas = WgpuCanvas(**defaults)
else:
self.canvas = WgpuCanvasOffscreen(**defaults)
# There is a bug in pygfx 0.1.18 that causes the renderer to crash
# when using a Jupyter canvas without explicitly setting the pixel_ratio.
# This is already fixed in main but for now:
if self._is_jupyter:
self.renderer = gfx.renderers.WgpuRenderer(
self.canvas, show_fps=False, pixel_ratio=2
)
else:
self.renderer = gfx.renderers.WgpuRenderer(self.canvas, show_fps=False)
# Set up a default scene
self.scene = gfx.Scene()
# A minor ambient light
self.scene.add(gfx.AmbientLight(intensity=0.5))
# A strong point light form front/top/left
self.scene.add(gfx.PointLight(intensity=4))
self.scene.children[-1].shadow.bias = 0.0000005 # this helps with shadow acne
self.scene.children[-1].local.x = -1000000 # move to the left
self.scene.children[-1].local.y = -1000000 # move up
self.scene.children[-1].local.z = -1000000 # move light forward
# A weaker point light from the back
self.scene.add(gfx.PointLight(intensity=1))
self.scene.children[-1].shadow.bias = 0.0000005 # this helps with shadow acne
self.scene.children[-1].local.x = 1000000 # move to the left
self.scene.children[-1].local.y = 1000000 # move up
self.scene.children[-1].local.z = 1000000 # move light forward
# Set up a default background
self._background = gfx.BackgroundMaterial((0, 0, 0))
self.scene.add(gfx.Background(None, self._background))
# Add camera
if camera == "ortho":
self.camera = gfx.OrthographicCamera()
elif camera == "perspective":
self.camera = gfx.PerspectiveCamera()
else:
raise ValueError(f"Unknown camera type: {camera}")
# Add controller
controller = {
"trackball": gfx.TrackballController,
"panzoom": gfx.PanZoomController,
"fly": gfx.FlyController,
"orbit": gfx.OrbitController,
}.get(control, None)
if controller is None:
raise ValueError(f"Unknown controller type: {control}")
self.controller = controller(self.camera, register_events=self.renderer)
# Setup overlay
self.overlay_camera = gfx.NDCCamera()
self.overlay_scene = gfx.Scene()
# Stats
self.stats = gfx.Stats(self.renderer)
self._show_fps = False
# Setup key events
self._key_events = {}
self._key_events["1"] = lambda: self.set_view("XY")
self._key_events["2"] = lambda: self.set_view("XZ")
self._key_events["3"] = lambda: self.set_view("YZ")
self._key_events["f"] = lambda: self._toggle_fps()
self._key_events["c"] = lambda: self._toggle_controls()
def _keydown(event):
"""Handle key presses."""
if not event.modifiers:
if event.key in self._key_events:
self._key_events[event.key]()
else:
tup = (event.key, tuple(event.modifiers))
if tup in self._key_events:
self._key_events[tup]()
# Register events
self.renderer.add_event_handler(_keydown, "key_down")
# Finally, setting some variables
self._show_bounds = False
self._shadows = False
self._animations = {}
self._animations_flagged_for_removal = []
self._on_double_click = None
self._on_hover = None
self._objects_pickable = False
self._selected = []
viewers.append(self)
# This starts the animation loop
if show and not self._is_jupyter:
self.show(start_loop=show == "start_loop")
def _animate(self):
"""Run the rendering loop."""
# First run the user animations
# Note: we're iterating over the list because the user might add / remove
# animations during the loop
for i, (func, on_error) in enumerate(list(self._animations.items())):
try:
func()
except BaseException as e:
if on_error == "raise":
raise e
elif on_error == "remove":
logger.error(
f"Removing animation function '{func}' because of error: {e}"
)
# Flag animation for removal
self._animations_flagged_for_removal.append(i)
# Check if any animations need to be removed
for f in self._animations_flagged_for_removal[::-1]:
self._animations.pop(f)
self._animations_flagged_for_removal = []
# Now render the scene
if self._show_fps:
with self.stats:
self.renderer.render(self.scene, self.camera, flush=False)
self.renderer.render(
self.overlay_scene, self.overlay_camera, flush=False
)
self.stats.render()
else:
self.renderer.render(self.scene, self.camera, flush=False)
self.renderer.render(self.overlay_scene, self.overlay_camera)
self.canvas.request_draw()
def _next_color(self):
"""Return next color in the colormap."""
# Cache the full palette. N.B. that ordering of colors in cmap depends on
# the number of colors requested - i.e. we can't just grab the last color.
if not hasattr(self, "_cached_palette") or self.palette != self._cached_palette:
self._cached_colors = list(cmap.Colormap(self.palette).iter_colors())
self._cached_palette = self.palette
if not hasattr(self, "_palette_index"):
self._palette_index = -1
self._palette_index += 1
return self._cached_colors[self._palette_index % len(self._cached_colors)]
def _next_label(self, prefix="Object"):
"""Return next label."""
existing = [o for o in self.objects if str(o).startswith(prefix)]
if len(existing) == 0:
return prefix
return f"{prefix}.{len(existing) + 1:03}"
def __getitem__(self, key):
"""Get item."""
return self.objects[key]
def __contains__(self, key):
"""Check if object is on canvas."""
return key in self.objects
def __len__(self):
"""Return number of objects on canvas."""
return len(self._object_ids)
@property
def blend_mode(self):
"""Render blend mode.
This is a simple shortcut, see `Viewer.renderer.blend_mode` for more information.
"""
return self.renderer.blend_mode
@blend_mode.setter
def blend_mode(self, mode):
self.renderer.blend_mode = mode
@property
def controls(self):
"""Return the controls widget."""
return getattr(self, "_controls", None)
@property
def visible(self):
"""List IDs of currently visible objects."""
objects = self.objects # grab this only once to speed things up
return [s for s in objects if objects[s][0].visible]
@property
def invisible(self):
"""List IDs of currently visible objects."""
objects = self.objects # grab this only once to speed things up
return [s for s in objects if not objects[s][0].visible]
@property
def pinned(self):
"""List IDs of currently pinned objects."""
objects = self.objects # grab this only once to speed things up
return [s for s in objects if getattr(objects[s][0], "_pinned", False)]
@property
def selected(self):
"""Return IDs of or set selected objects."""
return self._selected
@selected.setter
def selected(self, val):
val = utils.make_iterable(val)
objects = self.objects # grab once to speed things up
logger.debug(f"{len(val)} objects selected ({len(self.selected)} previously)")
# First un-highlight neurons no more selected
for s in [s for s in self._selected if s not in val]:
for v in objects[s]:
if isinstance(v, gfx.Mesh):
v.color = v._stored_color
else:
v.set_data(color=v._stored_color)
# Highlight new additions
for s in val:
if s not in self._selected:
for v in objects[s]:
# Keep track of old colour
v.unfreeze()
v._stored_color = v.color
v.freeze()
if isinstance(v, gfx.Mesh):
v.color = self.highlight_color
else:
v.set_data(color=self.highlight_color)
self._selected = list(val)
# Update legend
if self.show_legend:
self.update_legend()
# Update data text
# Currently only the development version of vispy supports escape
# character (e.g. \n)
t = "| ".join([f"{objects[s][0]._name} - #{s}" for s in self._selected])
self._data_text.text = t
@property
def size(self):
"""Return size of the canvas."""
return self.canvas.get_logical_size()
@size.setter
def size(self, size):
"""Set size of the canvas."""
assert len(size) == 2
self.canvas.set_logical_size(*size)
@property
def shadows(self):
"""Return shadow state."""
return self._shadows
@shadows.setter
def shadows(self, v):
"""Set shadow state."""
if not isinstance(v, bool):
raise TypeError(f"Expected bool, got {type(v)}")
def set_shadow(obj, state):
if hasattr(obj, "cast_shadow"):
obj.cast_shadow = state
if hasattr(obj, "receive_shadow"):
obj.receive_shadow = state
if v != self._shadows:
self._shadows = v
for vis in self.visuals:
set_shadow(vis, v)
for ch in self.scene.children:
if isinstance(ch, gfx.PointLight):
ch.cast_shadow = v
# self.scene.traverse(lambda x: set_shadow(x, v))
@property
def visuals(self):
"""List of all visuals on this canvas."""
return [c for c in self.scene.children if hasattr(c, "_object_id")]
@property
def bounds(self):
"""Bounds of all currently visuals (visible and invisible)."""
bounds = []
for vis in self.visuals:
# Skip the bounding box itself
if getattr(vis, "_object_id", "") == "boundingbox":
continue
try:
bounds.append(vis._bounds)
except BaseException:
pass
if not bounds:
return None
bounds = np.dstack(bounds)
mn = bounds[:, 0, :].min(axis=1)
mx = bounds[:, 1, :].max(axis=1)
return np.vstack((mn, mx)).T
@property
def max_fps(self):
"""Maximum frames per second to render."""
return self.canvas._subwidget._max_fps
@max_fps.setter
def max_fps(self, v):
assert isinstance(v, int)
self.canvas._subwidget._max_fps = v
@property
def _is_jupyter(self):
"""Check if Viewer is using Jupyter canvas."""
return "JupyterWgpuCanvas" in str(type(self.canvas))
@property
def _is_offscreen(self):
"""Check if Viewer is using offscreen canvas."""
return isinstance(self.canvas, WgpuCanvasOffscreen)
@property
def _object_ids(self):
"""All object IDs on this canvas in order of addition."""
obj_ids = []
for v in self.visuals:
if hasattr(v, "_object_id"):
obj_ids.append(v._object_id)
return sorted(set(obj_ids), key=lambda x: obj_ids.index(x))
@property
def objects(self):
return self._objects()
@lru_cache(maxsize=1)
def _objects(self):
"""Ordered dictionary {name->[visuals]} of all objects in order of addition."""
objects = OrderedDict()
for v in self.visuals:
if hasattr(v, "_object_id"):
if v._object_id in objects:
objects[v._object_id].append(v)
else:
objects[v._object_id] = [v]
return objects
@property
def objects_pickable(self):
return self._objects_pickable
@objects_pickable.setter
def objects_pickable(self, v):
if not isinstance(v, bool):
raise TypeError(f"Expected bool, got {type(v)}")
# No need to do anything if the value is the same
if v == self._objects_pickable:
return
self._objects_pickable = v
# Set pick_write to new value for all materials
for objects in self.objects.values():
for ob in objects:
try:
ob.material.pick_write = v
except AttributeError:
pass
@property
def highlighted(self):
"""Return IDs of currently highlighted objects."""
highlighted = []
for obj in self.objects:
if any([getattr(v, "_highlighted", False) for v in self.objects[obj]]):
highlighted.append(obj)
return highlighted
@property
def on_hover(self):
"""Determines what to do when hovering over objects.
Can be set to:
- `None`: do nothing
- "highlight": hide object
"""
return self._on_hover
@on_hover.setter
def on_hover(self, v):
valid = (None, "highlight")
if v not in valid:
raise ValueError(
f"Unknown value for on_hover: {v}. Must be one of {valid}."
)
# No need to do anything if the value is the same
if v == self._on_hover:
return
if v:
# Make objects pickable
self.objects_pickable = True
# Add the event handler
self.scene.add_event_handler(self._highlight_on_hover_event, "pointer_move")
else:
self.scene.remove_event_handler(
self._highlight_on_hover_event, "pointer_move"
)
current_hover = getattr(self, "_current_hover_object", None)
# Make sure to unhighlight the current hover object
if current_hover:
self.unhighlight_objects(current_hover)
self._current_hover_object = None
self._on_hover = v
def _highlight_on_hover_event(self, event):
"""This is the event callback for highlighting objects on hover."""
# If any buttons are pressed (e.g. mouse left during panning) ignore the event
if event.buttons:
return
# Parse the current object
new_hover = event.pick_info["world_object"]
current_hover = getattr(self, "_current_hover_object", None)
# Break early if there is nothing to do
if new_hover is None and current_hover is None:
# print(" No hover")
return
new_hover_id = [k for k, v in self.objects.items() if new_hover in v]
new_hover_id = new_hover_id[0] if new_hover_id else None
# See if we need to de-highlight the current hover object
if current_hover:
# If the new object is the same as the current one, we don't need to do anything
if current_hover == new_hover_id:
return
if current_hover in self.objects:
self.unhighlight_objects(current_hover)
self._current_hover_object = None
# Highlight the new object
if new_hover_id:
self.highlight_objects(
new_hover_id, color=getattr(self, "_highlight_on_hover_color", 0.2)
)
self._current_hover_object = new_hover_id
@property
def on_double_click(self):
"""Determines what to do when double clicking on objects.
Can be set to:
- `None`: do nothing
- "hide": hide object
- "remove": remove object
- "select": select object
"""
return self._on_double_click
@on_double_click.setter
def on_double_click(self, v):
valid = (None, "hide", "remove", "select")
if v not in valid:
raise ValueError(
f"Unknown value for on_double_click: {v}. Must be one of {valid}."
)
# No need to do anything if the value is the same
if v == self._on_double_click:
return
# First try to remove the current event handler for double clicks
try:
self.scene.remove_event_handler(
getattr(self, "_on_double_click_func", None), "double_click"
)
except KeyError:
pass
if v:
# Make objects pickable
self.objects_pickable = True
# Now add the new event handler
func = partial(handle_object_event, viewer=self, actions=(v,))
self.scene.add_event_handler(func, "double_click")
self.__on_double_click_func = func
self._on_double_click = v
def add_animation(self, x, on_error="remove"):
"""Add animation function to the Viewer.
Parameters
----------
x : callable
Function to add to the animation loop.
on_error : "remove" | "ignore" | "raise"
What to do if the function throws an error. If "remove",
the function will be removed from the animation loop. If
"ignore", the error will be ignored and the function will
continue to be called.
"""
if not callable(x):
raise TypeError(f"Expected callable, got {type(x)}")
assert on_error in ["remove", "ignore", "raise"]
self._animations[x] = on_error
def remove_animation(self, x):
"""Remove animation function from the Viewer.
Parameters
----------
x : callable | int
Either the function itself or its index
in the list of animations.
"""
if callable(x):
self._animations_flagged_for_removal.append(x)
elif isinstance(x, int):
self._animations_flagged_for_removal.append(
list(self._animations.keys())[x]
)
else:
raise TypeError(f"Expected callable or index (int), got {type(x)}")
def show(self, use_sidecar=False, toolbar=False, start_loop=False):
"""Show viewer.
Parameters
----------
For Jupyter lab only:
use_sidecar : bool
If True, will use the Sidecar extension to display the
viewer outside the notebooks. Will throw an error if
Sidecar is not installed.
toolbar : bool
If True, will show a toolbar. You can always show/hide
the toolbar with ``viewer.show_controls()`` and
``viewer.hide_controls()``, or the `c` hotkey.
For scripts & standard REPL:
start_loop : bool
If True, will start the blocking (!) event loop. This is
the recommended way to show the viewer when using it in a script.
From an interactive REPL such as IPython you should be able to
just call ``Viewer.show()`` and the interactive viewer will appear
while still allowing you to interact with the REPL.
"""
# This is for e.g. headless testing
if getattr(config, "HEADLESS", False):
logger.info("Viewer widget not shown - running in headless mode.")
return
# Start the animation loop
self.canvas.request_draw(self._animate)
# If this is an offscreen canvas, we don't need to do anything
if isinstance(self.canvas, WgpuCanvasOffscreen):
return
# In terminal we can just show the window
if not self._is_jupyter:
# Not all backends have a show method
if hasattr(self.canvas, "show"):
self.canvas.show()
if start_loop:
from wgpu.gui.auto import run
run()
elif utils._type_of_script() in ("terminal", "script"):
logger.warning(
"Running in a (potentially) non-interactive terminal or script "
"environment. You may have to manually start the event loop "
"for the canvas to render:\n\n"
" >>> v = octarine.Viewer(show=False)\n"
" >>> ... # setup your viewer\n"
" >>> v.show(start_loop=True)\n\n"
"Alternatively, use WGPU's run() function:\n\n"
" >>> from wgpu.gui.auto import run\n"
" >>> ... # setup your viewer\n"
" >>> v.show()\n"
" >>> run()\n\n" # do not remove the \n\n here
)
else:
# if not hasattr(self, 'widget'):
from .jupyter import JupyterOutput
from IPython.display import display
# Construct the widget
if not hasattr(self, "widget"):
self.widget = JupyterOutput(
self,
use_sidecar=use_sidecar,
toolbar=toolbar,
sidecar_kwargs={"title": self._title},
)
# This will display the viewer right here and there
display(self.widget)
def show_message(
self, message, position="top-right", font_size=20, color=None, duration=None
):
"""Show message on canvas.
Parameters
----------
message : str | None
Message to show. Set to `None` to remove the existing message.
position : "top-left" | "top-right" | "bottom-left" | "bottom-right" | "center"
Position of the message on the canvas.
font_size : int, optional
Font size of the message.
color : str | tuple, optional
Color of the message. If `None`, will use white.
duration : int, optional
Number of seconds after which to fade the message.
"""
if message is None and hasattr(self, "_message_text"):
if self._message_text.parent:
self.overlay_scene.remove(self._message_text)
del self._message_text
return
_positions = {
"top-left": (-0.95, 0.95, 0),
"top-right": (0.95, 0.95, 0),
"bottom-left": (-0.95, -0.95, 0),
"bottom-right": (0.95, -0.95, 0),
"center": (0, 0, 0),
}
if position not in _positions:
raise ValueError(f"Unknown position: {position}")
if not hasattr(self, "_message_text"):
self._message_text = text2gfx(
message, color="white", font_size=font_size, screen_space=True
)
# Make sure the text is in the scene
if self._message_text not in self.overlay_scene.children:
self.overlay_scene.add(self._message_text)
self._message_text.geometry.set_text(message)
self._message_text.geometry.font_size = font_size
self._message_text.geometry.anchor = position
if color is not None:
self._message_text.material.color = cmap.Color(color).rgba
self._message_text.material.opacity = 1
self._message_text.local.position = _positions[position]
# When do we need to start fading out?
if duration:
self._fade_out_time = time.time() + duration
def _fade_message():
if not hasattr(self, "_message_text"):
self.remove_animation(_fade_message)
else:
if time.time() > self._fade_out_time:
# This means the text will fade fade over 1/0.02 = 50 frames
self._message_text.material.opacity = max(
self._message_text.material.opacity - 0.02, 0
)
if self._message_text.material.opacity <= 0:
if self._message_text.parent:
self.overlay_scene.remove(self._message_text)
self.remove_animation(_fade_message)
self.add_animation(_fade_message)
def show_controls(self):
"""Show controls."""
if self._is_jupyter:
if self.widget.toolbar:
self.widget.toolbar.show()
else:
if not hasattr(self, "_controls"):
from .controls import Controls
self._controls = Controls(self)
self._controls.show()
def hide_controls(self):
"""Hide controls."""
if self._is_jupyter:
if self.widget.toolbar:
self.widget.toolbar.hide()
else:
if hasattr(self, "_controls"):
self._controls.hide()
def _toggle_controls(self):
"""Switch controls on and off."""
if self._is_jupyter:
if self.widget.toolbar:
self.widget.toolbar.toggle()
else:
if not hasattr(self, "_controls"):
self.show_controls()
elif self._controls.isVisible():
self.hide_controls()
else:
self.show_controls()
@update_viewer(legend=True, bounds=True)
def clear(self):
"""Clear canvas of objects (expects lights and background)."""
# Skip if running in headless mode
if getattr(config, "HEADLESS", False):
return
# Remove everything but the lights and backgrounds
self.scene.remove(*self.visuals)
@update_viewer(legend=True, bounds=True)
def remove_objects(self, to_remove):
"""Remove given neurons/visuals from canvas."""
to_remove = utils.make_iterable(to_remove)
for vis in self.scene.children:
if vis in to_remove:
self.scene.remove(vis)
elif hasattr(vis, "_object_id"):
if vis._object_id in to_remove:
self.scene.remove(vis)
@update_viewer(legend=True, bounds=True)
def pop(self, N=1):
"""Remove the most recently added N visuals."""
for vis in list(self.objects.values())[-N:]:
self.remove_objects(vis)
@property
def show_bounds(self):
"""Set to ``True`` to show bounding box."""
return self._show_bounds
def toggle_bounds(self):
"""Toggle bounding box."""
self.show_bounds = not self.show_bounds
@show_bounds.setter
def show_bounds(self, v):
if not isinstance(v, bool):
raise TypeError(f"Need bool, got {type(v)}")
self._show_bounds = v
if self.show_bounds:
self.update_bounds()
else:
self.remove_bounds()
def remove_bounds(self):
"""Remove bounding box visual."""
self._show_bounds = False
for v in self.visuals:
if getattr(v, "_object_type", "") == "boundingbox":
self.remove_objects(v)
def resize(self, size):
"""Resize canvas.
Parameters
----------
size : (width, height) tuple
New size of the canvas.
"""
assert len(size) == 2
self.canvas.set_logical_size(*size)
def update_bounds(self, color="w", width=1):
"""Update bounding box visual."""
# Remove any existing visual
self.remove_bounds()
self._show_bounds = True
# Skip if no visual on canvas
bounds = self.scene.get_bounding_box()
if isinstance(bounds, type(None)):
return
# Create box visual
box = gfx.BoxHelper()
box.set_transform_by_aabb(bounds)
# Add custom attributes
box._object_type = "boundingbox"
box._object_id = uuid.uuid4()
self.scene.add(box)
def center_camera(self):
"""Center camera on visuals."""
if len(self):
self.camera.show_object(
self.scene, scale=1, view_dir=(0.0, 0.0, 1.0), up=(0.0, -1.0, 0.0)
)
@update_viewer(legend=True, bounds=True)
def add(self, x, name=None, center=True, clear=False, **kwargs):
"""Add object to canvas.
This function is a general entry point for adding objects to the canvas.
It will look at the type of the input and try to find an appropriate
function to convert the input to visuals.
Use `octarine.register_converter` to add custom converters.
Parameters
----------
x
Object(s) to add to the canvas.
name : str, optional
Name for the visual(s).
center : bool, optional
If True, re-center camera to all objects on canvas.
clear : bool, optional
If True, clear canvas before adding new objects.
**kwargs
Keyword arguments passed to the conversion functions when
generating visuals.
Returns
-------
None
"""
if clear:
self.clear()
if utils.is_iterable(x) and not isinstance(x, np.ndarray):
for xx in x:
self.add(xx, center=False, clear=False, name=name, **kwargs)
if center:
self.center_camera()
return
converter = get_converter(x, raise_missing=False)
if converter is None:
raise NotImplementedError(f"No converter found for {x} ({type(x)})")
# Check if we have to provide a color
if "color" not in kwargs and "color" in inspect.signature(converter).parameters:
kwargs["color"] = tuple(self._next_color().rgba)
visuals = utils.make_iterable(converter(x, **kwargs))
for v in visuals:
# If we have a name, assign it to the visual
if name is not None:
v._object_id = name
# If not we either use existing ID or generate a new one
else:
# Give visuals an _object_id if they don't already have one
if not hasattr(v, "_object_id"):
new_id = self._next_label("Object")
for v2 in visuals:
v._object_id = new_id
elif not isinstance(v._object_id, str):
v._object_id = str(v._object_id)
self.scene.add(v)
if center:
self.center_camera()
@update_viewer(legend=True, bounds=True)
def _add_to_scene(self, visual, center=True):
"""Add visual to scene.
This is just a convenient collection point for us to trigger a bunch of updates in one go,
"""
# If we need objects to be pickable, set the material accordingly
if self.objects_pickable:
try:
visual.material.pick_write = True
except AttributeError:
pass
self.scene.add(visual)
if center:
self.center_camera()
def add_mesh(self, mesh, name=None, color=None, alpha=None, center=True):
"""Add mesh to canvas.
Parameters
----------
mesh : Mesh-like
Mesh to plot. If this is a pygfx.Mesh, it will be added
directly to the scene without modification (i.e. `color`,
`alpha`, etc. will be ignored).
name : str, optional
Name for the visual.
color : str | tuple, optional
Color to use for plotting. If multiple colors,
must be a list of colors with the same length as
the number of faces or vertices.
alpha : float, optional
Opacity value [0-1]. If provided, will override
the alpha channel of the color.
center : bool, optional
If True, re-center camera to all objects on canvas.
"""
if not utils.is_mesh_like(mesh):
raise TypeError(f"Expected mesh-like object, got {type(mesh)}")
if color is None:
color = self._next_color()
if name is None:
name = self._next_label("Mesh")
elif not isinstance(name, str):
name = str(name)
if not isinstance(mesh, gfx.Mesh):
visual = mesh2gfx(mesh, color=color, alpha=alpha)
else:
visual = mesh
visual._object_id = name if name else uuid.uuid4()
self._add_to_scene(visual, center)
def add_points(
self,
points,
name=None,
color=None,
marker=None,
size=2,
size_space="screen",
center=True,
):
"""Add points plot to canvas.
Parameters
----------
points : (N, 3) array
Points to plot.
name : str, optional
Name for the visual.
color : str | tuple, optional
Color to use for plotting. Can be the name of
a colormap or a single color.
marker : str, optional
Marker to use for plotting. By default (None), will
use a point. Other options include e.g. "circle", "ring"
or "diamond". See `pygfx.MarkerShape` for the definitive
list of options. Please note that you may have to
increase the size of the marker to see some of the shapes.
size : int | float
Marker size. Can be a single value or an array of
sizes for each point.
size_space : "screen" | "world" | "model", optional
Units to use for the marker size. "screen" (default)
will keep the line width constant on the screen, while
"world" and "model" will keep it constant in world and
model coordinates, respectively. In the latter two cases,
`size` corresponds to the diameter (not radius) of the
marker!
center : bool, optional
If True, re-center camera to all objects on canvas.
"""
if not isinstance(points, np.ndarray):
raise TypeError(f"Expected numpy array, got {type(points)}")
if points.ndim != 2 or points.shape[1] != 3:
raise ValueError(f"Expected (N, 3) array, got {points.shape}")
if color is None:
color = self._next_color()
if name is None:
name = self._next_label("Scatter")
elif not isinstance(name, str):
name = str(name)
visual = points2gfx(
points, color=color, size=size, size_space=size_space, marker=marker
)
visual._object_id = name if name else uuid.uuid4()
self._add_to_scene(visual, center)
def add_lines(
self,
lines,
name=None,
color=None,
linewidth=1,
linewidth_space="screen",
linestyle="solid",
center=True,
):
"""Add lines to canvas.
Parameters
----------
lines : list of (N, 3) arrays | (N, 3) array
Lines to plot. If a list of arrays, each array
represents a separate line. If a single array,
each row represents a point in the line. You can
introduce breaks in the line by inserting NaNs.
name : str, optional
Name for the visual.
color : str | tuple, optional
Color to use for plotting. Can be a single color
or one for every point in the line(s).
linewidth : float, optional
Line width.
linewidth_space : "screen" | "world" | "model", optional
Units to use for the line width. "screen" (default)
will keep the line width constant on the screen, while
"world" and "model" will keep it constant in world and
model coordinates, respectively.
linestyle : "solid" | "dashed" | "dotted" | "dashdot" | tuple, optional
Line style to use. If a tuple, must define the on/off
sequence.
center : bool, optional
If True, re-center camera to all objects on canvas.
"""
# TODO:
# - allow providing a tuple of (positions, edges) for lines
if isinstance(lines, np.ndarray):
if lines.ndim != 2 or lines.shape[1] != 3:
raise ValueError(f"Expected (N, 3) array, got {lines.shape}")
elif isinstance(lines, list):
if not all([l.ndim == 2 and l.shape[1] == 3 for l in lines]):
raise ValueError("Expected list of (N, 3) arrays.")
else:
raise TypeError(f"Expected numpy array or list, got {type(lines)}")
if color is None:
color = self._next_color()
if name is None:
name = self._next_label("Lines")
elif not isinstance(name, str):
name = str(name)
visual = lines2gfx(
lines,
linewidth=linewidth,
linewidth_space=linewidth_space,
color=color,
dash_pattern=linestyle,
)
visual._object_id = name if name else uuid.uuid4()
self._add_to_scene(visual, center)
def add_volume(
self,
volume,
spacing=(1, 1, 1),
name=None,
color=None,
opacity=1.0,
offset=(0, 0, 0),
clim="data",
slice=False,
interpolation="linear",
hide_zero=True,
center=True,
):
"""Add image volume to canvas.
Note that the default blend mode for the renderer may cause objects
behind or inside the volume to look funny. You can change the blend
mode by setting e.g. `viewer.blend_mode='additive'`.
Parameters
----------
volume : (N, M, K) array
Volume to plot.
spacing : tuple
Spacing between voxels.
name : str, optional
Name for the visual.
color : color | list of colors | pygfx.Texture, optional
Colormap to render the volume. This can be:
- name of a colormap (e.g. "viridis" or "magma")
- a single color (name, hex, rgb, rgba)
- a list of colors
- a 1D pygfx.Texture
Note that single colors typically don't look good and
it's better to define at least two colors. For example,
instead of "red" use ["red", "yellow"]. If `None` will
use one of the built-in pygfx colormaps.
opacity : float, optional
Overall opacity of the volume. Must be between 0 and 1.
offset : tuple, optional
(x, y, z) offset for the volume. If None, will use (0, 0, 0).
clim : "data" | "datatype" | tuple, optional
The contrast limits to scale the data values with.
- "data" (default) will use the min/max of the data
- "datatype" will use (0, theoretical max of data type)
for integer data, e.g. (0, 255) for int8 and uint8,
and (0, 1) for float data assuming the data has been
normalized
- tuple of min/max values or combination of "data" and
"datatype" strings
slice : bool | tuple, optional
Render volume slices instead of the full volume:
- True: render slices along all three dimensions
- tuple of bools, e.g. `(True, True, False)`: render slices
in the respective dimensions
- tuple of floats, e.g. `(0.5, 0.5, 0.5)`: render slices
at the respective positions (relative to the volume size)
interpolation : "linear" | "nearest"
Interpolation to use when rendering the volume. "linear"
(default) looks better but is slower.
hide_zero : bool
If True, will hide voxels with lowest value according to `cmin`.
center : bool, optional
If True, re-center camera to all objects on canvas.
"""
if not isinstance(volume, np.ndarray):
raise TypeError(f"Expected numpy array, got {type(volume)}")
if volume.ndim != 3:
raise ValueError(f"Expected 3D array, got {volume.ndim}")
if name is None:
name = self._next_label("Volume")
elif not isinstance(name, str):
name = str(name)
visuals = volume2gfx(
volume,
spacing=spacing,
offset=offset,
color=color,
opacity=opacity,
clim=clim,
slice=slice,
interpolation=interpolation,
hide_zero=hide_zero,
)
name = name if name else uuid.uuid4()
for vis in visuals:
vis._object_id = name if name else uuid.uuid4()
self._add_to_scene(vis, center)
def close(self):
"""Close the viewer."""
# Skip if this is headless mode
if getattr(config, "HEADLESS", False):
return
# Clear first to free all visuals
self.clear()
# Remove from config if this is the primary viewer
if self == getattr(config, "PRIMARY_VIEWER", None):
del config.PRIMARY_VIEWER
# Close if not already closed
if not self.canvas.is_closed():
self.canvas.close()
if hasattr(self, "_controls"):
self._controls.close()
# Close the Jupyter widget
if hasattr(self, "widget") and not getattr(self.widget, "_is_closed", False):
self.widget.close(close_viewer=False)
try:
viewers.remove(self)
except ValueError:
pass
@update_viewer(legend=True, bounds=True)
def hide_objects(self, obj):
"""Hide given object(s).
Parameters
----------
obj : str | list
Object(s) to hide.
"""
objects = self.objects # grab once to speed things up
for ob in utils.make_iterable(obj):
if ob not in objects:
logger.warning(f'Object "{ob}" not found on canvas.')
continue
for v in objects[ob]:
if getattr(v, "_pinned", False):
continue
if v.visible:
v.visible = False
@update_viewer(legend=True, bounds=True)
def hide_selected(self):
"""Hide currently selected object(s)."""
self.hide_neurons(self.selected)
@update_viewer(legend=True, bounds=True)
def unhide_objects(self, obj=None):
"""Unhide given object(s).
Parameters
----------
obj : str | list | None
Object(s) to unhide. If None, will unhide all objects.
"""
objects = self.objects # grab once to speed things up
if obj is not None:
ids = utils.make_iterable(obj)
else:
ids = list(objects.keys())
for ob in ids:
if ob not in objects:
logger.warning(f"Object {ob} not found on canvas.")
continue
for v in objects[ob]:
if getattr(v, "_pinned", False):
continue
if not v.visible:
v.visible = True
def highlight_objects(self, obj, color=0.2):
"""Highlight given object(s) by increasing their brightness.
Parameters
----------
obj : str | int | list | visual
Object(s) to highlight. Can be the name(s) or ID(s) of
the object(s), their index(es) in the list of visuals,
or the visual(s) themselves. Objects already highlighted
will be silently ignored.
color : float | tuple
Color to use for highlighting. If a float, will change
the HSV value of the current color. If a tuple, will
use the RGB(A) color.
See Also
--------
Viewer.unhighlight_objects
Use to remove highlights.
"""
if not utils.is_iterable(obj):
objects = [obj]
else:
objects = obj
all_objects = self.objects # grab once to speed things up
for ob in objects:
if ob in all_objects:
list_ = all_objects[ob]
elif isinstance(ob, int):
list_ = list(self.objects.values())[ob]
elif isinstance(ob, gfx.WorldObject):
list_ = [ob]
else:
raise TypeError(f"Unknown object type: {type(ob)}")
for o in list_:
# Skip if object is pinned
if getattr(o, "_pinned", False):
continue
# Skip if object is already highlighted
if getattr(o, "_highlighted", False):
continue
if isinstance(color, (float, int)):
# Work in HSL space
h, s, l = o.material.color.to_hsl()
# If the value is not maxed yet, increase it
if l < 1:
l = min(l + color, 1)
else:
l = max(l - color, 0)
new_color = gfx.Color.from_hsl(h, s, l)
else:
# See if pygfx can handle the color
new_color = gfx.Color(color)
o.material._original_color = o.material.color
o.material.color = new_color
o._highlighted = True
def unhighlight_objects(self, obj=None):
"""Unhighlight given object(s).
Parameters
----------
obj : str | int | list | visual
Object(s) to unhighlight. Can be the name(s) or ID(s) of
the object(s), their index(es) in the list of visuals,
or the visual(s) themselves. If None, will unhighlight all
objects. Objects that aren't highlighted will be silently
ignored.
See Also
--------
Viewer.highlight_objects
Use to highlight objects
"""
# Important note: it looks like any attribute we added previously
# will (at some point) have been silently renamed to "_Viewer{attribute}"
if obj is None:
obj = [v for v in self.visuals if getattr(v, "_highlighted", False)]
if not utils.is_iterable(obj):
objects = [obj]
else:
objects = obj
all_objects = self.objects # grab once to speed things up
for ob in objects:
if ob in all_objects:
list_ = all_objects[ob]
elif isinstance(ob, int):
list_ = list(self.visuals.values())[ob]
elif isinstance(ob, gfx.WorldObject):
list_ = [ob]
else:
raise TypeError(f"Unknown object type: {type(ob)}")
for o in list_:
# Skip if object is pinned
if getattr(o, "_pinned", False):
continue
# Skip if object isn't actually highlighed
if not getattr(o, "_highlighted", False):
continue
o.material.color = o.material._original_color
del o.material._original_color
del o._highlighted
def pin_objects(self, obj):
"""Pin given object(s).
Changes to the color or visibility of pinned neurons are silently
ignored. You can use this to keep specific neurons visible while
cycling through the rest - useful for comparisons.
"""
obj = utils.make_iterable(obj)
objects = self.objects # grab only once to speed things up
for ob in obj:
if ob not in objects:
logger.warning(f"Object {ob} not found on canvas.")
continue
for v in objects[ob]:
v._pinned = True
def unpin_objects(self, obj=None):
"""Unpin given object(s).
Use ``obj`` to unhide specific neurons.
"""
objects = self.objects # grab once to speed things up
if obj is None:
obj = objects
else:
obj = utils.make_iterable(obj)
for ob in obj:
if ob not in objects:
logger.warning(f"Object {ob} not found on canvas.")
continue
for v in objects[ob]:
v._pinned = False
@update_viewer(legend=True, bounds=False)
def set_colors(self, c):
"""Set object color.
Parameters
----------
c : tuple | dict
RGB color(s) to apply. Values must be 0-1. Accepted:
1. Tuple of single color. Applied to all visible objects.
2. Dictionary names/IDs to colors.
"""
objects = self.objects # grab once to speed things up
if isinstance(c, (tuple, list, np.ndarray, str)):
cmap = {s: c for s in objects}
elif isinstance(c, dict):
cmap = c
else:
raise TypeError(f'Unable to use colors of type "{type(c)}"')
for n in objects:
if n in cmap:
for v in objects[n]:
if getattr(v, "_pinned", False):
continue
if not hasattr(v, "material"):
continue
# Note: there is currently a bug where removing or adding an alpha
# channel from a color will break the rendering pipeline
if len(v.material.color) == 4:
new_c = gfx.Color(cmap[n]).rgba
else:
new_c = gfx.Color(cmap[n]).rgb
v.material.color = gfx.Color(new_c)
def colorize(self, palette="seaborn:tab10", objects=None, randomize=True):
"""Colorize objects using a color palette.
Parameters
----------
palette : str | cmap Colormap
Name of the `cmap` palette to use. See
https://cmap-docs.readthedocs.io/en/latest/catalog/#colormaps-by-category
for available options.
objects : list, optional
Objects to colorize. If None, will colorize all objects.
randomize : bool
If True (default), will randomly shuffle the colors.
"""
if objects is None:
objects = self.objects # grab once to speed things up
if not isinstance(palette, cmap._colormap.Colormap):
palette = cmap.Colormap(palette)
if randomize:
# Note: can't use numpy here because it claims array is not 1d
colors = random.choices(list(palette.iter_colors()), k=len(objects))
else:
colors = list(palette.iter_colors(len(objects)))
colormap = {s: tuple(colors[i].rgba) for i, s in enumerate(objects)}
self.set_colors(colormap)
def set_bgcolor(self, c):
"""Set background color.
Parameters
----------
c : tuple | str
RGB(A) color to use for the background.
"""
self._background.set_colors(gfx.Color(c).rgba)
def _toggle_fps(self):
"""Switch FPS measurement on and off."""
self._show_fps = not self._show_fps
def screenshot(
self, filename="screenshot.png", size=None, pixel_ratio=None, alpha=True
):
"""Save a screenshot of the canvas.
Parameters
----------
filename : str, optional
Filename to save to. If ``None``, will return image array.
Note that this will always save a PNG file, no matter
the extension.
size : tuple, optional
Size of the screenshot. If provided, will temporarily
change the canvas size.
pixel_ratio : int, optional
Factor by which to scale canvas. Determines image
dimensions. Note that this seems to have no effect
on offscreen canvases.
alpha : bool, optional
If True, will export transparent background.
"""
im = self._screenshot(alpha=alpha, size=size, pixel_ratio=pixel_ratio)
if filename:
if not filename.endswith(".png"):
filename += ".png"
png.from_array(
im.reshape(im.shape[0], im.shape[1] * im.shape[2]), mode="RGBA"
).save(filename)
else:
return im
def _screenshot(self, alpha=True, size=None, pixel_ratio=None):
"""Return image array for screenshot."""
if alpha:
oc = [
self._background.color_bottom_left,
self._background.color_bottom_right,
self._background.color_top_left,
self._background.color_top_right,
]
self._background.set_colors((0, 0, 0, 0))
op = self._background.opacity
self._background.opacity = 0
if size:
os = self.size
self.size = size
if pixel_ratio:
opr = self.renderer.pixel_ratio
self.renderer.pixel_ratio = pixel_ratio
# If this is an offscreen canvas, we need to manually trigger a draw first
# Note: this has to happen _after_ adjust parameters!
if isinstance(self.canvas, WgpuCanvasOffscreen):
self.canvas.draw()
else:
# This is a bit of a hack to make sure a new frame with the (potentially)
# updated size, pixel ratio, etc. is drawn before taking the screenshot.
with warnings.catch_warnings():
warnings.simplefilter("ignore")
self.canvas.draw_frame()
try:
im = self.renderer.snapshot()
except BaseException:
raise
finally:
if alpha:
self._background.set_colors(*oc)
self._background.opacity = op
if size:
self.size = os
if pixel_ratio:
self.renderer.pixel_ratio = opr
return im
def set_view(self, view):
"""(Re-)set camera position.
Parameters
----------
view : XY | XZ | YZ | dict
View to set. If a dictionary, should describe the
state of the camera. Typically, this is obtained
by calling `viewer.get_view()`.
"""
if view == "XY":
self.camera.show_object(
self.scene, view_dir=(0.0, 0.0, 1.0), up=(0.0, -1.0, 0.0)
)
elif view == "XZ":
self.camera.show_object(
self.scene, scale=1, view_dir=(0.0, 1.0, 0.0), up=(0.0, 0.0, 1.0)
)
elif view == "YZ":
self.camera.show_object(
self.scene, scale=1, view_dir=(-1.0, 0.0, 0.0), up=(0.0, -1.0, 0.0)
)
elif isinstance(view, dict):
self.camera.set_state(view)
else:
raise TypeError(f"Unable to set view from {type(view)}")
def get_view(self):
"""Get current camera position."""
return self.camera.get_state()
def bind_key(self, key, func, modifiers=None):
"""Bind a function to a key press.
Note that any existing keybindings for `key` + `modifiers` will be
silently overwritten.
Parameters
----------
key : str
Key to bind to. Can be any key on the keyboard.
func : callable
Function to call when key is pressed.
modifiers : str | list thereof, optional
Modifier(s) to use with the key. Can be "Shift", "Control",
"Alt" or "Meta".
"""
if not callable(func):
raise TypeError("`func` needs to be callable")
if not isinstance(key, str):
raise TypeError(f"Expected `key` to be a string, got {type(key)}")
if modifiers is None:
self._key_events[key] = func
else:
# We need to make `modifiers` is hashable
if isinstance(key, str):
key = (key,)
elif isinstance(key, (set, list)):
key = tuple(key)
if not isinstance(modifiers, tuple):
raise TypeError(
f"Unexpected datatype for `modifiers`: {type(modifiers)}"
)
self._key_events[(key, modifiers)] = func
|