-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathgui.cpp
More file actions
2008 lines (1939 loc) · 104 KB
/
gui.cpp
File metadata and controls
2008 lines (1939 loc) · 104 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
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
// ----------------------------------------------------------------------------
// - Open3D: www.open3d.org -
// ----------------------------------------------------------------------------
// Copyright (c) 2018-2024 www.open3d.org
// SPDX-License-Identifier: MIT
// ----------------------------------------------------------------------------
#include "pybind/visualization/gui/gui.h"
#include "open3d/camera/PinholeCameraIntrinsic.h"
#include "open3d/geometry/Image.h"
#include "open3d/t/geometry/Image.h"
#include "open3d/utility/FileSystem.h"
#include "open3d/visualization/gui/Application.h"
#include "open3d/visualization/gui/Button.h"
#include "open3d/visualization/gui/Checkbox.h"
#include "open3d/visualization/gui/Color.h"
#include "open3d/visualization/gui/ColorEdit.h"
#include "open3d/visualization/gui/Combobox.h"
#include "open3d/visualization/gui/Dialog.h"
#include "open3d/visualization/gui/FileDialog.h"
#include "open3d/visualization/gui/Gui.h"
#include "open3d/visualization/gui/ImageWidget.h"
#include "open3d/visualization/gui/Label.h"
#include "open3d/visualization/gui/Label3D.h"
#include "open3d/visualization/gui/Layout.h"
#include "open3d/visualization/gui/ListView.h"
#include "open3d/visualization/gui/NumberEdit.h"
#include "open3d/visualization/gui/ProgressBar.h"
#include "open3d/visualization/gui/RadioButton.h"
#include "open3d/visualization/gui/SceneWidget.h"
#include "open3d/visualization/gui/Slider.h"
#include "open3d/visualization/gui/StackedWidget.h"
#include "open3d/visualization/gui/TabControl.h"
#include "open3d/visualization/gui/TextEdit.h"
#include "open3d/visualization/gui/Theme.h"
#include "open3d/visualization/gui/ToggleSwitch.h"
#include "open3d/visualization/gui/TreeView.h"
#include "open3d/visualization/gui/VectorEdit.h"
#include "open3d/visualization/gui/Widget.h"
#include "open3d/visualization/gui/WidgetProxy.h"
#include "open3d/visualization/gui/WidgetStack.h"
#include "open3d/visualization/gui/Window.h"
#include "open3d/visualization/rendering/Open3DScene.h"
#include "open3d/visualization/rendering/Renderer.h"
#include "open3d/visualization/rendering/Scene.h"
#include "open3d/visualization/rendering/filament/FilamentEngine.h"
#include "open3d/visualization/rendering/filament/FilamentRenderToBuffer.h"
#include "pybind/docstring.h"
#include "pybind/open3d_pybind.h"
#include "pybind/visualization/visualization.h"
#include "pybind11/functional.h"
namespace open3d {
namespace visualization {
namespace gui {
class PythonUnlocker : public Application::EnvUnlocker {
public:
PythonUnlocker() { unlocker_ = nullptr; }
~PythonUnlocker() {
if (unlocker_) { // paranoia; this shouldn't happen
delete unlocker_;
}
}
void unlock() { unlocker_ = new py::gil_scoped_release(); }
void relock() {
delete unlocker_;
unlocker_ = nullptr;
}
private:
py::gil_scoped_release *unlocker_;
};
class PyWindow : public Window {
using Super = Window;
public:
explicit PyWindow(const std::string &title, int flags = 0)
: Super(title, flags) {}
PyWindow(const std::string &title, int width, int height, int flags = 0)
: Super(title, width, height, flags) {}
PyWindow(const std::string &title,
int x,
int y,
int width,
int height,
int flags = 0)
: Super(title, x, y, width, height, flags) {}
std::function<void(const LayoutContext &)> on_layout_;
protected:
void Layout(const LayoutContext &context) override {
if (on_layout_) {
// the Python callback sizes the children
on_layout_(context);
// and then we need to layout the children
for (auto child : GetChildren()) {
child->Layout(context);
}
} else {
Super::Layout(context);
}
}
};
// atexit: Filament crashes if the engine was not destroyed before exit().
// As far as I can tell, the bluegl mutex, which is a static variable,
// gets destroyed before the render thread gets around to calling
// bluegl::unbind(), thus crashing. So, we need to make sure Filament gets
// cleaned up before C++ starts cleaning up static variables. But we don't want
// to clean up this way unless something catastrophic happens (e.g. the Python
// interpreter is exiting due to a fatal exception). Some cases we need to
// consider:
// 1) exception before calling Application.instance.run()
// 2) exception during Application.instance.run(), namely within a UI callback
// 3) exception after Application.instance.run() successfully finishes
// If Python is exiting normally, then Application::Run() should have already
// cleaned up Filament. So if we still need to clean up Filament at exit(),
// we must be panicking. It is a little difficult to check this, though, but
// Application::OnTerminate() should work even if we've already cleaned up,
// it will just end up being a no-op.
bool g_installed_atexit = false;
void cleanup_filament_atexit() { Application::GetInstance().OnTerminate(); }
void install_cleanup_atexit() {
if (!g_installed_atexit) {
atexit(cleanup_filament_atexit);
}
}
void InitializeForPython(std::string resource_path /*= ""*/,
bool headless /*= false*/) {
if (resource_path.empty()) {
// We need to find the resources directory. Fortunately,
// Python knows where the module lives (open3d.__file__
// is the path to
// __init__.py), so we can use that to find the
// resources included in the wheel.
py::object o3d = py::module::import("open3d");
auto o3d_init_path = o3d.attr("__file__").cast<std::string>();
auto module_path =
utility::filesystem::GetFileParentDirectory(o3d_init_path);
resource_path = module_path + "/resources";
}
Application::GetInstance().Initialize(resource_path.c_str());
// NOTE: The PyOffscreenRenderer takes care of cleaning itself up so the
// atext is not necessary
if (!headless) {
install_cleanup_atexit();
}
}
std::shared_ptr<geometry::Image> RenderToImageWithoutWindow(
rendering::Open3DScene *scene, int width, int height) {
return Application::GetInstance().RenderToImage(
scene->GetRenderer(), scene->GetView(), scene->GetScene(), width,
height);
}
std::shared_ptr<geometry::Image> RenderToDepthImageWithoutWindow(
rendering::Open3DScene *scene,
int width,
int height,
bool z_in_view_space /* = false */) {
return Application::GetInstance().RenderToDepthImage(
scene->GetRenderer(), scene->GetView(), scene->GetScene(), width,
height, z_in_view_space);
}
enum class EventCallbackResult { IGNORED = 0, HANDLED, CONSUMED };
class PyImageWidget : public ImageWidget {
using Super = ImageWidget;
public:
PyImageWidget() : Super() {}
/// Uses image from the specified path. Each ImageWidget will use one
/// draw call.
explicit PyImageWidget(const char *image_path) : Super(image_path) {}
/// Uses existing image. Each ImageWidget will use one draw call.
explicit PyImageWidget(std::shared_ptr<open3d::geometry::Image> image)
: Super(image) {}
/// Uses existing image. Each ImageWidget will use one draw call.
explicit PyImageWidget(std::shared_ptr<open3d::t::geometry::Image> image)
: Super(image) {}
/// Uses an existing texture, using texture coordinates
/// (u0, v0) to (u1, v1). Does not deallocate texture on destruction.
/// This is useful for using an icon atlas to reduce draw calls.
explicit PyImageWidget(
open3d::visualization::rendering::TextureHandle texture_id,
float u0 = 0.0f,
float v0 = 0.0f,
float u1 = 1.0f,
float v1 = 1.0f)
: Super(texture_id, u0, v0, u1, v1) {}
~PyImageWidget() = default;
void SetOnMouse(std::function<int(const MouseEvent &)> f) { on_mouse_ = f; }
void SetOnKey(std::function<int(const KeyEvent &)> f) { on_key_ = f; }
Widget::EventResult Mouse(const MouseEvent &e) override {
if (on_mouse_) {
switch (EventCallbackResult(on_mouse_(e))) {
case EventCallbackResult::CONSUMED:
return Widget::EventResult::CONSUMED;
case EventCallbackResult::HANDLED: {
auto result = Super::Mouse(e);
if (result == Widget::EventResult::IGNORED) {
result = Widget::EventResult::CONSUMED;
}
return result;
}
case EventCallbackResult::IGNORED:
default:
return Super::Mouse(e);
}
} else {
return Super::Mouse(e);
}
}
Widget::EventResult Key(const KeyEvent &e) override {
if (on_key_) {
switch (EventCallbackResult(on_key_(e))) {
case EventCallbackResult::CONSUMED:
return Widget::EventResult::CONSUMED;
case EventCallbackResult::HANDLED: {
auto result = Super::Key(e);
if (result == Widget::EventResult::IGNORED) {
result = Widget::EventResult::CONSUMED;
}
return result;
}
case EventCallbackResult::IGNORED:
default:
return Super::Key(e);
}
} else {
return Super::Key(e);
}
}
private:
std::function<int(const MouseEvent &)> on_mouse_;
std::function<int(const KeyEvent &)> on_key_;
};
class PySceneWidget : public SceneWidget {
using Super = SceneWidget;
public:
void SetOnMouse(std::function<int(const MouseEvent &)> f) { on_mouse_ = f; }
void SetOnKey(std::function<int(const KeyEvent &)> f) { on_key_ = f; }
Widget::EventResult Mouse(const MouseEvent &e) override {
if (on_mouse_) {
switch (EventCallbackResult(on_mouse_(e))) {
case EventCallbackResult::CONSUMED:
return Widget::EventResult::CONSUMED;
case EventCallbackResult::HANDLED: {
auto result = Super::Mouse(e);
if (result == Widget::EventResult::IGNORED) {
result = Widget::EventResult::CONSUMED;
}
return result;
}
case EventCallbackResult::IGNORED:
default:
return Super::Mouse(e);
}
} else {
return Super::Mouse(e);
}
}
Widget::EventResult Key(const KeyEvent &e) override {
if (on_key_) {
switch (EventCallbackResult(on_key_(e))) {
case EventCallbackResult::CONSUMED:
return Widget::EventResult::CONSUMED;
case EventCallbackResult::HANDLED: {
auto result = Super::Key(e);
if (result == Widget::EventResult::IGNORED) {
result = Widget::EventResult::CONSUMED;
}
return result;
}
case EventCallbackResult::IGNORED:
default:
return Super::Key(e);
}
} else {
return Super::Key(e);
}
}
private:
std::function<int(const MouseEvent &)> on_mouse_;
std::function<int(const KeyEvent &)> on_key_;
};
void pybind_gui_declarations(py::module &m) {
py::module m_gui = m.def_submodule("gui");
pybind_gui_events_declarations(m_gui);
py::native_enum<FontStyle>(m_gui, "FontStyle", "enum.Enum", "Font style")
.value("NORMAL", FontStyle::NORMAL)
.value("BOLD", FontStyle::BOLD)
.value("ITALIC", FontStyle::ITALIC)
.value("BOLD_ITALIC", FontStyle::BOLD_ITALIC)
.finalize();
py::class_<FontDescription> fd(m_gui, "FontDescription",
"Class to describe a custom font");
py::class_<Application> application(m_gui, "Application",
"Global application singleton. This "
"owns the menubar, windows, and event "
"loop");
py::class_<LayoutContext> lc(
m_gui, "LayoutContext",
"Context passed to Window's on_layout callback");
// Pybind appears to need to know about the base class. It doesn't have
// to be named the same as the C++ class, though. The holder object cannot
// be a shared_ptr or we can crash (see comment for UnownedPointer).
py::class_<Window, UnownedPointer<Window>> window_base(
m_gui, "WindowBase", "Application window");
py::class_<PyWindow, UnownedPointer<PyWindow>, Window> window(
m_gui, "Window",
"Application window. Create with "
"Application.instance.create_window().");
py::class_<Menu, UnownedPointer<Menu>> menu(m_gui, "Menu",
"A menu, possibly a menu tree");
py::class_<Color> color(m_gui, "Color", "Stores color for gui classes");
py::class_<Theme> theme(m_gui, "Theme",
"Theme parameters such as colors used for drawing "
"widgets (read-only)");
py::class_<Rect> rect(m_gui, "Rect", "Represents a widget frame");
py::class_<Size> size(m_gui, "Size", "Size object");
py::class_<Widget, UnownedPointer<Widget>> widget(m_gui, "Widget",
"Base widget class");
py::native_enum<EventCallbackResult>(widget, "EventCallbackResult",
"enum.IntEnum",
"Returned by event handlers")
.value("IGNORED", EventCallbackResult::IGNORED,
"Event handler ignored the event, widget will "
"handle event normally")
.value("HANDLED", EventCallbackResult::HANDLED,
"Event handler handled the event, but widget "
"will still handle the event normally. This is "
"useful when you are augmenting base "
"functionality")
.value("CONSUMED", EventCallbackResult::CONSUMED,
"Event handler consumed the event, event "
"handling stops, widget will not handle the "
"event. This is useful when you are replacing "
"functionality")
.export_values()
.finalize();
py::class_<Widget::Constraints> constraints(
widget, "Constraints",
"Constraints object for Widget.calc_preferred_size()");
py::class_<WidgetProxy, UnownedPointer<WidgetProxy>, Widget> widgetProxy(
m_gui, "WidgetProxy",
"Widget container to delegate any widget dynamically."
" Widget can not be managed dynamically. Although it is allowed"
" to add more child widgets, it's impossible to replace some child"
" with new on or remove children. WidgetProxy is designed to solve"
" this problem."
" When WidgetProxy is created, it's invisible and disabled, so it"
" won't be drawn or layout, seeming like it does not exist. When"
" a widget is set by set_widget, all Widget's APIs will be"
" conducted to that child widget. It looks like WidgetProxy is"
" that widget."
" At any time, a new widget could be set, to replace the old one."
" and the old widget will be destroyed."
" Due to the content changing after a new widget is set or cleared,"
" a relayout of Window might be called after set_widget."
" The delegated widget could be retrieved by get_widget in case"
" you need to access it directly, like get check status of a"
" CheckBox. API other than set_widget and get_widget has"
" completely same functions as Widget.");
py::class_<WidgetStack, UnownedPointer<WidgetStack>, WidgetProxy>
widget_stack(m_gui, "WidgetStack",
"A widget stack saves all widgets pushed into by "
"push_widget and always shows the top one. The "
"WidgetStack is a subclass of WidgetProxy, in other"
"words, the topmost widget will delegate itself to "
"WidgetStack. pop_widget will remove the topmost "
"widget and callback set by set_on_top taking the "
"new topmost widget will be called. The WidgetStack "
"disappears in GUI if there is no widget in stack.");
py::class_<Button, UnownedPointer<Button>, Widget> button(m_gui, "Button",
"Button");
py::class_<Checkbox, UnownedPointer<Checkbox>, Widget> checkbox(
m_gui, "Checkbox", "Checkbox");
py::class_<ColorEdit, UnownedPointer<ColorEdit>, Widget> coloredit(
m_gui, "ColorEdit", "Color picker");
py::class_<Combobox, UnownedPointer<Combobox>, Widget> combobox(
m_gui, "Combobox", "Exclusive selection from a pull-down menu");
py::class_<RadioButton, UnownedPointer<RadioButton>, Widget> radiobtn(
m_gui, "RadioButton", "Exclusive selection from radio button list");
py::native_enum<RadioButton::Type>(radiobtn, "Type", "enum.Enum",
"Enum class for RadioButton types.")
.value("VERT", RadioButton::Type::VERT)
.value("HORIZ", RadioButton::Type::HORIZ)
.export_values()
.finalize();
py::class_<UIImage, UnownedPointer<UIImage>> uiimage(
m_gui, "UIImage",
"A bitmap suitable for displaying with ImageWidget");
py::native_enum<UIImage::Scaling>(uiimage, "Scaling", "enum.Enum",
"Enum class for UIImage scaling modes.")
.value("NONE", UIImage::Scaling::NONE)
.value("ANY", UIImage::Scaling::ANY)
.value("ASPECT", UIImage::Scaling::ASPECT)
.finalize();
py::class_<PyImageWidget, UnownedPointer<PyImageWidget>, Widget>
imagewidget(m_gui, "ImageWidget", "Displays a bitmap");
py::class_<Label, UnownedPointer<Label>, Widget> label(m_gui, "Label",
"Displays text");
py::class_<Label3D, UnownedPointer<Label3D>> label3d(
m_gui, "Label3D", "Displays text in a 3D scene");
py::class_<ListView, UnownedPointer<ListView>, Widget> listview(
m_gui, "ListView", "Displays a list of text");
py::class_<NumberEdit, UnownedPointer<NumberEdit>, Widget> numedit(
m_gui, "NumberEdit", "Allows the user to enter a number.");
py::native_enum<NumberEdit::Type>(numedit, "Type", "enum.Enum",
"Enum class for NumberEdit types.")
.value("INT", NumberEdit::Type::INT)
.value("DOUBLE", NumberEdit::Type::DOUBLE)
.export_values()
.finalize();
py::class_<ProgressBar, UnownedPointer<ProgressBar>, Widget> progress(
m_gui, "ProgressBar", "Displays a progress bar");
py::class_<PySceneWidget, UnownedPointer<PySceneWidget>, Widget> scene(
m_gui, "SceneWidget", "Displays 3D content");
py::native_enum<SceneWidget::Controls>(
scene, "Controls", "enum.Enum",
"Enum class describing mouse interaction.")
.value("ROTATE_CAMERA", SceneWidget::Controls::ROTATE_CAMERA)
.value("ROTATE_CAMERA_SPHERE",
SceneWidget::Controls::ROTATE_CAMERA_SPHERE)
.value("FLY", SceneWidget::Controls::FLY)
.value("ROTATE_SUN", SceneWidget::Controls::ROTATE_SUN)
.value("ROTATE_IBL", SceneWidget::Controls::ROTATE_IBL)
.value("ROTATE_MODEL", SceneWidget::Controls::ROTATE_MODEL)
.value("PICK_POINTS", SceneWidget::Controls::PICK_POINTS)
.export_values()
.finalize();
py::class_<Slider, UnownedPointer<Slider>, Widget> slider(
m_gui, "Slider", "A slider widget for visually selecting numbers");
py::native_enum<Slider::Type>(slider, "Type", "enum.Enum",
"Enum class for Slider types.")
.value("INT", Slider::Type::INT)
.value("DOUBLE", Slider::Type::DOUBLE)
.export_values()
.finalize();
py::class_<StackedWidget, UnownedPointer<StackedWidget>, Widget> stacked(
m_gui, "StackedWidget", "Like a TabControl but without the tabs");
py::class_<TabControl, UnownedPointer<TabControl>, Widget> tabctrl(
m_gui, "TabControl", "Tab control");
py::class_<TextEdit, UnownedPointer<TextEdit>, Widget> textedit(
m_gui, "TextEdit", "Allows the user to enter or modify text");
py::class_<ToggleSwitch, UnownedPointer<ToggleSwitch>, Widget> toggle(
m_gui, "ToggleSwitch", "ToggleSwitch");
py::class_<TreeView, UnownedPointer<TreeView>, Widget> treeview(
m_gui, "TreeView", "Hierarchical list");
py::class_<CheckableTextTreeCell, UnownedPointer<CheckableTextTreeCell>,
Widget>
checkable_cell(m_gui, "CheckableTextTreeCell",
"TreeView cell with a checkbox and text");
py::class_<LUTTreeCell, UnownedPointer<LUTTreeCell>, Widget> lut_cell(
m_gui, "LUTTreeCell",
"TreeView cell with checkbox, text, and color edit");
py::class_<ColormapTreeCell, UnownedPointer<ColormapTreeCell>, Widget>
colormap_cell(m_gui, "ColormapTreeCell",
"TreeView cell with a number edit and color edit");
py::class_<VectorEdit, UnownedPointer<VectorEdit>, Widget> vectoredit(
m_gui, "VectorEdit", "Allows the user to edit a 3-space vector");
py::class_<Margins, UnownedPointer<Margins>> margins(m_gui, "Margins",
"Margins for layouts");
py::class_<Layout1D, UnownedPointer<Layout1D>, Widget> layout1d(
m_gui, "Layout1D", "Layout base class");
py::class_<Vert, UnownedPointer<Vert>, Layout1D> vlayout(m_gui, "Vert",
"Vertical layout");
py::class_<CollapsableVert, UnownedPointer<CollapsableVert>, Vert>
collapsable(m_gui, "CollapsableVert",
"Vertical layout with title, whose contents are "
"collapsable");
py::class_<ScrollableVert, UnownedPointer<ScrollableVert>, Vert> slayout(
m_gui, "ScrollableVert", "Scrollable vertical layout");
py::class_<Horiz, UnownedPointer<Horiz>, Layout1D> hlayout(
m_gui, "Horiz", "Horizontal layout");
py::class_<VGrid, UnownedPointer<VGrid>, Widget> vgrid(m_gui, "VGrid",
"Grid layout");
py::class_<Dialog, UnownedPointer<Dialog>, Widget> dialog(m_gui, "Dialog",
"Dialog");
py::class_<FileDialog, UnownedPointer<FileDialog>, Dialog> filedlg(
m_gui, "FileDialog", "File picker dialog");
py::native_enum<FileDialog::Mode>(filedlg, "Mode", "enum.Enum",
"Enum class for FileDialog modes.")
.value("OPEN", FileDialog::Mode::OPEN)
.value("SAVE", FileDialog::Mode::SAVE)
.value("OPEN_DIR", FileDialog::Mode::OPEN_DIR)
.export_values()
.finalize();
}
void pybind_gui_definitions(py::module &m) {
auto m_gui = static_cast<py::module>(m.attr("gui"));
pybind_gui_events_definitions(m_gui);
// ---- FontDescription ----
auto fd = static_cast<py::class_<FontDescription>>(
m_gui.attr("FontDescription"));
fd.attr("SANS_SERIF") = FontDescription::SANS_SERIF;
fd.attr("MONOSPACE") = FontDescription::MONOSPACE;
fd.def(py::init<const char *, FontStyle, int>(),
"typeface"_a = FontDescription::SANS_SERIF,
py::arg_v("style", FontStyle::NORMAL, "open3d.gui.FontStyle.NORMAL"),
"point_size"_a = 0,
"Creates a FontDescription. 'typeface' is a path to a "
"TrueType (.ttf), TrueType Collection (.ttc), or "
"OpenType (.otf) file, or it is the name of the font, "
"in which case the system font paths will be searched "
"to find the font file. This typeface will be used for "
"roman characters (Extended Latin, that is, European "
"languages")
.def("add_typeface_for_language",
&FontDescription::AddTypefaceForLanguage,
"Adds code points outside Extended Latin from the specified "
"typeface. Supported languages are:\n"
" 'ja' (Japanese)\n"
" 'ko' (Korean)\n"
" 'th' (Thai)\n"
" 'vi' (Vietnamese)\n"
" 'zh' (Chinese, 2500 most common characters, 50 MB per "
"window)\n"
" 'zh_all' (Chinese, all characters, ~200 MB per window)\n"
"All other languages will be assumed to be Cyrillic. "
"Note that generally fonts do not have CJK glyphs unless they "
"are specifically a CJK font, although operating systems "
"generally use a CJK font for you. We do not have the "
"information necessary to do this, so you will need to "
"provide a font that has the glyphs you need. In particular, "
"common fonts like 'Arial', 'Helvetica', and SANS_SERIF do "
"not contain CJK glyphs.")
.def("add_typeface_for_code_points",
&FontDescription::AddTypefaceForCodePoints,
"Adds specific code points from the typeface. This is useful "
"for selectively adding glyphs, for example, from an icon "
"font.");
// ---- Application ----
auto application =
static_cast<py::class_<Application>>(m_gui.attr("Application"));
application.attr("DEFAULT_FONT_ID") = Application::DEFAULT_FONT_ID;
application
.def("__repr__",
[](const Application &app) {
return std::string("Application singleton instance");
})
.def_property_readonly_static(
"instance",
// Seems like we ought to be able to specify
// &Application::GetInstance but that gives runtime errors
// about number of arguments. It seems that property calls
// are made from an object, so that object needs to be in
// the function signature.
[](py::object) -> Application & {
return Application::GetInstance();
},
py::return_value_policy::reference,
"Gets the Application singleton (read-only)")
.def(
"initialize",
[](Application &instance) { InitializeForPython(); },
"Initializes the application, using the resources included "
"in the wheel. One of the `initialize` functions _must_ be "
"called prior to using anything in the gui module")
.def(
"initialize",
[](Application &instance, const char *resource_dir) {
InitializeForPython(resource_dir);
},
"Initializes the application with location of the "
"resources provided by the caller. One of the `initialize` "
"functions _must_ be called prior to using anything in the "
"gui module")
.def("add_font", &Application::AddFont,
"Adds a font. Must be called after initialize() and before "
"a window is created. Returns the font id, which can be used "
"to change the font in widgets such as Label which support "
"custom fonts.")
.def("set_font", &Application::SetFont,
"Changes the contents of an existing font, for instance, the "
"default font.")
.def(
"create_window",
[](Application &instance, const std::string &title,
int width, int height, int x, int y, int flags) {
std::shared_ptr<PyWindow> w;
if (x < 0 && y < 0 && width < 0 && height < 0) {
w.reset(new PyWindow(title, flags));
} else if (x < 0 && y < 0) {
w.reset(new PyWindow(title, width, height, flags));
} else {
w.reset(new PyWindow(title, x, y, width, height,
flags));
}
instance.AddWindow(w);
return w.get();
},
"title"_a = std::string(), "width"_a = -1, "height"_a = -1,
"x"_a = -1, "y"_a = -1, "flags"_a = 0,
"Creates a window and adds it to the application. "
"To programmatically destroy the window do window.close()."
"Usage: create_window(title, width, height, x, y, flags). "
"x, y, and flags are optional.")
// We need to tell RunOneTick() not to cleanup. Run() and
// RunOneTick() assume a C++ desktop application approach of
// init -> run -> cleanup. More problematic for us is that Filament
// crashes if we don't cleanup. Also, we don't want to force Python
// script writers to remember to cleanup; this is Python, not C++
// where you expect to need to thinking about cleanup up after
// yourself. So as a Python-script-app, the cleanup happens atexit.
// (Init is still required of the script writer.) This means that
// run() should NOT cleanup, as it might be called several times
// to run a UI to visualize the result of a computation.
.def(
"run",
[](Application &instance) {
PythonUnlocker unlocker;
while (instance.RunOneTick(unlocker, false)) {
// Enable Ctrl-C to kill Python
if (PyErr_CheckSignals() != 0) {
throw py::error_already_set();
}
}
},
"Runs the event loop. After this finishes, all windows and "
"widgets should be considered uninitialized, even if they "
"are still held by Python variables. Using them is unsafe, "
"even if run() is called again.")
.def(
"run_one_tick",
[](Application &instance) {
PythonUnlocker unlocker;
auto result = instance.RunOneTick(unlocker, false);
// Enable Ctrl-C to kill Python
if (PyErr_CheckSignals() != 0) {
throw py::error_already_set();
}
return result;
},
"Runs the event loop once, returns True if the app is "
"still running, or False if all the windows have closed "
"or quit() has been called.")
.def(
"render_to_image",
[](Application &instance, rendering::Open3DScene *scene,
int width, int height) {
return RenderToImageWithoutWindow(scene, width, height);
},
"Renders a scene to an image and returns the image. If you "
"are rendering without a visible window you should use "
"open3d.visualization.rendering.RenderToImage instead")
.def(
"quit", [](Application &instance) { instance.Quit(); },
"Closes all the windows, exiting as a result")
.def(
"add_window",
// Q: Why not just use &Application::AddWindow here?
// A: Because then AddWindow gets passed a shared_ptr with
// a use_count of 0 (but with the correct value for
// .get()), so it never gets freed, and then Filament
// doesn't clean up correctly. TakeOwnership() will
// create the shared_ptr properly.
[](Application &instance, UnownedPointer<Window> window) {
instance.AddWindow(TakeOwnership(window));
},
"Adds a window to the application. This is only necessary "
"when creating an object that is a Window directly, rather "
"than with create_window")
.def("run_in_thread", &Application::RunInThread,
"Runs function in a separate thread. Do not call GUI "
"functions on this thread, call post_to_main_thread() if "
"this thread needs to change the GUI.")
.def("post_to_main_thread", &Application::PostToMainThread,
py::call_guard<py::gil_scoped_release>(),
"Runs the provided function on the main thread. This can "
"be used to execute UI-related code at a safe point in "
"time. If the UI changes, you will need to manually "
"request a redraw of the window with w.post_redraw()")
.def_property("menubar", &Application::GetMenubar,
&Application::SetMenubar,
"The Menu for the application (initially None)")
.def_property_readonly("now", &Application::Now,
"Returns current time in seconds")
// Note: we cannot export AddWindow and RemoveWindow
.def_property_readonly("resource_path",
&Application::GetResourcePath,
"Returns a string with the path to the "
"resources directory");
// ---- LayoutContext ----
auto lc =
static_cast<py::class_<LayoutContext>>(m_gui.attr("LayoutContext"));
// lc.def_readonly("theme", &LayoutContext::theme);
// Pybind can't return a reference (since Theme is a unique_ptr), so
// return a copy instead.
lc.def_property_readonly("theme",
[](const LayoutContext &context) -> Theme {
return context.theme;
});
// ---- Window ----
auto window =
static_cast<py::class_<PyWindow, UnownedPointer<PyWindow>, Window>>(
m_gui.attr("Window"));
window.def("__repr__",
[](const PyWindow &w) { return "Application window"; })
.def(
"add_child",
[](PyWindow &w, UnownedPointer<Widget> widget) {
w.AddChild(TakeOwnership<Widget>(widget));
},
"Adds a widget to the window")
.def_property("os_frame", &PyWindow::GetOSFrame,
&PyWindow::SetOSFrame,
"Window rect in OS coords, not device pixels")
.def_property("title", &PyWindow::GetTitle, &PyWindow::SetTitle,
"Returns the title of the window")
.def("size_to_fit", &PyWindow::SizeToFit,
"Sets the width and height of window to its preferred size")
.def_property("size", &PyWindow::GetSize, &PyWindow::SetSize,
"The size of the window in device pixels, including "
"menubar (except on macOS)")
.def_property_readonly(
"content_rect", &PyWindow::GetContentRect,
"Returns the frame in device pixels, relative "
" to the window, which is available for widgets "
"(read-only)")
.def_property_readonly(
"scaling", &PyWindow::GetScaling,
"Returns the scaling factor between OS pixels "
"and device pixels (read-only)")
.def_property_readonly("is_visible", &PyWindow::IsVisible,
"True if window is visible (read-only)")
.def("set_on_key", &PyWindow::SetOnKeyEvent,
"Sets a callback for key events. This callback is passed "
"a KeyEvent object. The callback must return "
"True to stop more dispatching or False to dispatch"
"to focused widget")
.def("show", &PyWindow::Show, "Shows or hides the window")
.def("close", &PyWindow::Close,
"Closes the window and destroys it, unless an on_close "
"callback cancels the close.")
.def("set_needs_layout", &PyWindow::SetNeedsLayout,
"Flags window to re-layout")
.def("post_redraw", &PyWindow::PostRedraw,
"Sends a redraw message to the OS message queue")
.def_property_readonly("is_active_window",
&PyWindow::IsActiveWindow,
"True if the window is currently the active "
"window (read-only)")
.def("set_focus_widget", &PyWindow::SetFocusWidget,
"Makes specified widget have text focus")
.def("set_on_menu_item_activated",
&PyWindow::SetOnMenuItemActivated,
"Sets callback function for menu item: callback()")
.def("set_on_tick_event", &PyWindow::SetOnTickEvent,
"Sets callback for tick event. Callback takes no arguments "
"and must return True if a redraw is needed (that is, if "
"any widget has changed in any fashion) or False if nothing "
"has changed")
.def("set_on_close", &PyWindow::SetOnClose,
"Sets a callback that will be called when the window is "
"closed. The callback is given no arguments and should return "
"True to continue closing the window or False to cancel the "
"close")
.def(
"set_on_layout",
[](PyWindow *w,
std::function<void(const LayoutContext &)> f) {
w->on_layout_ = f;
},
"Sets a callback function that manually sets the frames of "
"children of the window. Callback function will be called "
"with one argument: gui.LayoutContext")
.def_property_readonly("theme", &PyWindow::GetTheme,
"Get's window's theme info")
.def(
"show_dialog",
[](PyWindow &w, UnownedPointer<Dialog> dlg) {
w.ShowDialog(TakeOwnership<Dialog>(dlg));
},
"Displays the dialog")
.def("close_dialog", &PyWindow::CloseDialog,
"Closes the current dialog")
.def("show_message_box", &PyWindow::ShowMessageBox,
"Displays a simple dialog with a title and message and okay "
"button")
.def("show_menu", &PyWindow::ShowMenu,
"show_menu(show): shows or hides the menu in the window, "
"except on macOS since the menubar is not in the window "
"and all applications must have a menubar.")
.def_property_readonly(
"renderer", &PyWindow::GetRenderer,
"Gets the rendering.Renderer object for the Window");
// ---- Menu ----
auto menu = static_cast<py::class_<Menu, UnownedPointer<Menu>>>(
m_gui.attr("Menu"));
menu.def(py::init<>())
.def(
"add_item",
[](UnownedPointer<Menu> menu, const char *text,
int item_id) { menu->AddItem(text, item_id); },
"Adds a menu item with id to the menu")
.def(
"add_menu",
[](UnownedPointer<Menu> menu, const char *text,
UnownedPointer<Menu> submenu) {
menu->AddMenu(text, TakeOwnership<Menu>(submenu));
},
"Adds a submenu to the menu")
.def("add_separator", &Menu::AddSeparator,
"Adds a separator to the menu")
.def(
"set_enabled",
[](UnownedPointer<Menu> menu, int item_id, bool enabled) {
menu->SetEnabled(item_id, enabled);
},
"Sets menu item enabled or disabled")
.def(
"is_checked",
[](UnownedPointer<Menu> menu, int item_id) -> bool {
return menu->IsChecked(item_id);
},
"Returns True if menu item is checked")
.def(
"set_checked",
[](UnownedPointer<Menu> menu, int item_id, bool checked) {
menu->SetChecked(item_id, checked);
},
"Sets menu item (un)checked");
// ---- Color ----
auto color = static_cast<py::class_<Color>>(m_gui.attr("Color"));
color.def(py::init([](float r, float g, float b, float a) {
return new Color(r, g, b, a);
}),
"r"_a = 1.0, "g"_a = 1.0, "b"_a = 1.0, "a"_a = 1.0)
.def_property_readonly(
"red", &Color::GetRed,
"Returns red channel in the range [0.0, 1.0] "
"(read-only)")
.def_property_readonly(
"green", &Color::GetGreen,
"Returns green channel in the range [0.0, 1.0] "
"(read-only)")
.def_property_readonly(
"blue", &Color::GetBlue,
"Returns blue channel in the range [0.0, 1.0] "
"(read-only)")
.def_property_readonly(
"alpha", &Color::GetAlpha,
"Returns alpha channel in the range [0.0, 1.0] "
"(read-only)")
.def("set_color", &Color::SetColor,
"Sets red, green, blue, and alpha channels, (range: [0.0, "
"1.0])",
"r"_a, "g"_a, "b"_a, "a"_a = 1.0);
// ---- Theme ----
// Note: no constructor because themes are created by Open3D
auto theme = static_cast<py::class_<Theme>>(m_gui.attr("Theme"));
theme.def_readonly("font_size", &Theme::font_size,
"Font size (which is also the conventional size of the "
"em unit) (read-only)")
.def_readonly("default_margin", &Theme::default_margin,
"Good default value for margins, useful for layouts "
"(read-only)")
.def_readonly("default_layout_spacing",
&Theme::default_layout_spacing,
"Good value for the spacing parameter in layouts "
"(read-only)");
// ---- Rect ----
auto rect = static_cast<py::class_<Rect>>(m_gui.attr("Rect"));
rect.def(py::init<>())
.def(py::init<int, int, int, int>())
.def(py::init([](float x, float y, float w, float h) {
return Rect(int(std::round(x)), int(std::round(y)),
int(std::round(w)), int(std::round(h)));
}))
.def("__repr__",
[](const Rect &r) {
std::stringstream s;
s << "Rect (" << r.x << ", " << r.y << "), " << r.width
<< " x " << r.height;
return s.str();
})
.def_readwrite("x", &Rect::x)
.def_readwrite("y", &Rect::y)
.def_readwrite("width", &Rect::width)
.def_readwrite("height", &Rect::height)
.def("get_left", &Rect::GetLeft)
.def("get_right", &Rect::GetRight)
.def("get_top", &Rect::GetTop)
.def("get_bottom", &Rect::GetBottom);
// ---- Size ----
auto size = static_cast<py::class_<Size>>(m_gui.attr("Size"));
size.def(py::init<>())
.def(py::init<int, int>())
.def(py::init([](float w, float h) {
return Size(int(std::round(w)), int(std::round(h)));
}))
.def("__repr__",
[](const Size &sz) {
std::stringstream s;
s << "Size (" << sz.width << ", " << sz.height << ")";
return s.str();
})
.def_readwrite("width", &Size::width)
.def_readwrite("height", &Size::height);
// ---- Widget ----
// The holder for Widget and all derived classes is UnownedPointer because
// a Widget may own Filament resources, so we cannot have Python holding
// on to a shared_ptr after we cleanup Filament. The object is initially
// "leaked" (as in, Python will not clean it up), but it will be claimed
// by the object it is added to. There are two consequences to this:
// 1) adding an object to multiple objects will cause multiple shared_ptrs
// to think they own it, leading to a double-free and crash, and
// 2) if the object is never added, the memory will be leaked.
auto widget = static_cast<py::class_<Widget, UnownedPointer<Widget>>>(
m_gui.attr("Widget"));
auto constraints = static_cast<py::class_<Widget::Constraints>>(
widget.attr("Constraints"));
constraints.def(py::init<>())
.def_readwrite("width", &Widget::Constraints::width)
.def_readwrite("height", &Widget::Constraints::height);
widget.def(py::init<>())
.def("__repr__",
[](const Widget &w) {
std::stringstream s;
s << "Widget (" << w.GetFrame().x << ", " << w.GetFrame().y
<< "), " << w.GetFrame().width << " x "
<< w.GetFrame().height;
return s.str();
})
.def(
"add_child",
[](Widget &w, UnownedPointer<Widget> child) {
w.AddChild(TakeOwnership<Widget>(child));
},
"Adds a child widget")
.def("get_children", &Widget::GetChildren,
"Returns the array of children. Do not modify.")
.def_property("frame", &Widget::GetFrame, &Widget::SetFrame,
"The widget's frame. Setting this value will be "
"overridden if the frame is within a layout.")
.def_property("visible", &Widget::IsVisible, &Widget::SetVisible,
"True if widget is visible, False otherwise")
.def_property("enabled", &Widget::IsEnabled, &Widget::SetEnabled,
"True if widget is enabled, False if disabled")
.def_property("background_color", &Widget::GetBackgroundColor,
&Widget::SetBackgroundColor,
"Background color of the widget")
.def_property("tooltip", &Widget::GetTooltip, &Widget::SetTooltip,
"Widget's tooltip that is displayed on mouseover")
.def("calc_preferred_size", &Widget::CalcPreferredSize,
"Returns the preferred size of the widget. This is intended "
"to be called only during layout, although it will also work "
"during drawing. Calling it at other times will not work, as "
"it requires some internal setup in order to function "
"properly");
// ---- WidgetProxy ----
auto widgetProxy = static_cast<
py::class_<WidgetProxy, UnownedPointer<WidgetProxy>, Widget>>(
m_gui.attr("WidgetProxy"));
widgetProxy.def(py::init<>(), "Creates a widget proxy")
.def("__repr__",
[](const WidgetProxy &c) {
std::stringstream s;
s << "Proxy (" << c.GetFrame().x << ", " << c.GetFrame().y
<< "), " << c.GetFrame().width << " x "
<< c.GetFrame().height;
return s.str();
})
.def(
"set_widget",
[](WidgetProxy &w, UnownedPointer<Widget> proxy) {
w.SetWidget(TakeOwnership<Widget>(proxy));
},