3DCoat Python API
The 3DCoat Python API documentation.
Loading...
Searching...
No Matches
dialogs.py

Running the UI commands.

Running the UI commands

1# This example demonstrates the power of the dialogs and UI. It describes how to register the elements into the UI.
2import coat
3import pickle
4#import debugpy
5
6# This example demonstrates the dialogs power. What we do?
7# Try to load previously stored settings, show dialog, save settings if user pressed Ok
8
9# let's define the class. Then we create the ui function that exposes the ui elements list
10
11class MyClass :
12 def __init__(self) :
13 self.Integer = 0
14 self.IntSlider = 0
15 self.Float = 0.0
16 self.FloatSlider = 0.5
17 self.Checkbox = False
18 self.Radio1 = True
19 self.Radio2 = False
20 self.Droplist = 0
21 self.String = "text"
22 self.Coordinate = coat.vec3(1, 2, 3)
23 self.Color = 'FF0000' # red
24 self.String = ''
25 self.OpenFilepath = ''
26 self.SaveFilepath = ''
27 self.Droplist = "Case2"
28 self.idx=1
29 self.items=[]
30
31 def One(self) :
32 self.Coordinate = coat.vec3(1)
33
34 def Two(self) :
35 self.Coordinate = coat.vec3(2)
36
37 def ZeroCoordinate(self) :
38 self.Coordinate = coat.vec3(0)
39
40 def add(self):
41 self.items.append(Item(self.idx))
42 self.idx+=1
43
44 # this section used for the class serialization and visual presentation in the UI
45 # pay attention that visualization and serialization may be separated if need.
46 def ui(self) :
47 # there you define the list of variables and functions to show in the dialog.
48 # This list is dynamic, this function is constantly called, any changes in list
49 # will be exposed immediately
50 return [
51 "#image:data/StartMenu/images/header.png", # the image example
52 "#Just centered text message there", # centered text
53 "#*Left-aligned text", # left-aligned text
54 "Integer", # we write there the name of variable we want to expose to the UI
55 "IntSlider,[0,100]", # this is the integer slider definition
56 "Float, Rename variable in UI if need", # we may use different name for the variable in UI
57 "FloatSlider,[-1,1]", # we defined range there
58 "Checkbox", # boolean variables visible as checkboxes
59 "Radio1, group1", # the radio button, we set the group there
60 "Radio2, group1", # same group for radio button
61 "Droplist,[Case1|Case2|Case3]",
62 "Color, color", # the color value editor
63 "OpenFilepath,load:*.tif;*.tiff;*.exr;*.tga;*.bmp;*.png", # the open file dialog
64 "SaveFilepath,save:*.tif;*.tiff;*.exr;*.tga;*.bmp;*.png", # save file dialog
65 "[1 []]", # the layout control for the next UI items, the number is relative width of each item, the [] means automatical width
66 "Coordinate", # the vec3 representation
67 "ZeroCoordinate,'{maticon close}'", # the X icon. You may refer icons in text using {maticon icon_name}. Look for icons at data/meterial.io/icons/black/
68 "[1 1]", # define 2 columns
69 "One", # set 1 to the vector
70 "Two", # set 2 to the vector
71 "---",
72 "items",
73 "add,'{maticon add}'"
74 ]
75# this chass is example of the list of clases within the parent class. If you prees the add button, the item will appear in the list.
76# Pay attention, the Item.__init__(self) should be callable without additional parameters to be abloe to be savel/loaded as json correctly.
77class Item:
78 def __init__(self, i = 0):
79 self.point = coat.vec3.RandNormal()
80 self.index = i
81 # remove the element
82 def X(self):
83 p.items.remove(self)
84 # the list of UI elements for the item
85 def ui(self):
86 return [
87 "[[%30] 1 []]",
88 "index,''",
89 "point,''",
90 "X,'{maticon close}'"
91 ]
92
93p = MyClass()
94# restore the object from json if the file exists
95coat.io.fromJsonFile(p,"dialogs.json")
96# The on_press called each time user press Ok or other button on the dialog's bottom
97def on_press(button):
98 print("pressed: ", button)
99 if(button == 1) : #Ok pressed
100 # save the settings into the Documents/3DCoat/dialogs.json
101 coat.io.toJson(p,"dialogs.json")
102def process_fn():
103 # do some action within the dialog
104 # for exmple, you may click over some controls within the dialog
105 # like coat.ui.cmd("$BaseClass::Integer")
106 p.Float+=1
107
108
109# show the dialog
110coat.dialog().ok().cancel().params(p).caption("caption").process(process_fn).onPress(on_press).show(); # the on_press will be called when used press ok(1) or cancel(2)
Definition coat.py:3433
fromJsonFile(obj=Any, filename=str)
Restore the object from the json file.
Definition coat.py:3847
str toJson(obj=Any, filename=str)
Store the object to the file or string as json.
Definition coat.py:3842
Definition coat.py:148
vec3 RandNormal()
Definition coat.py:274