pyui first dialog - pytha-3d-cad/pytha-lua-api GitHub Wiki
This is a small example showcasing, how easy it is to create a small plugin with an interactive dialog. The sample below is supposed to create a box of arbitrary size with a user-defined name.
Of course, we need to write a main function for our plugin:
function main()
local data = {size=500., name="Box"}
recreate_geometry(data)We need somewhere to keep track of the currently entered data values and we find it useful to bundle them in one lua table (data). We have also created the initial version of the geometry, so that the user has a direct visual feedback (see below for the content of recreate_geometry).
Now we create and display the modal dialog where we allow the user to enter the size of the box and to give it a (part-) name.
pyui.run_modal_dialog(cube_dialog, data)
endWhen we call pyui.run_modal_dialog, we pass the dialog initialization function and all other parameters, that it requires.
Now comes the definition of the dialog function. It will be responsible for creating the controls and hooking them up with the geometry creation logics.
Note that it receives all other arguments that we had passed to pyui.run_modal_dialog (here: data).
function cube_dialog(dialog, data)
dialog:set_window_title("My Cube")
Usually, you want to set a custom window title.
Now we create the controls:
local label1 = dialog:create_label(1, pyloc "Size")
local size = dialog:create_text_box(2, pyui.format_length(data.size))
local label2 = dialog:create_label({1,2}, pyloc "Name")
local name = dialog:create_text_box({1,2}, data.name)
local ok = dialog:create_ok_button(1)
local cancel = dialog:create_cancel_button(2)Note that we prefix all strings that will appear in the user interface with pyloc so that the plugin may be translated later on.
The resulting dialog layout looks like this...

Now we connect the control handlers. We react interactively to changes in the user input, because this gives a much superior user experience compared to evaluating the form only when the user presses Ok.
size:set_on_change_handler(function(text)
data.size = pyui.parse_length(text)
recreate_geometry(data)
end)
name:set_on_change_handler(function(text)
data.name = text
recreate_geometry(data)
end)And that was it for the dialog initialization function.
endNote also, that we could have placed it in a separate *.lua file if we had wanted to structure our code that way.
What is still missing is the geometry creation function. It's not the focus of this tutorial, but for completeness, we list it now:
function recreate_geometry(data)
if data.current_element ~= nil then
pytha.delete_element(data.current_element)
end
data.current_element = pytha.create_block(data.size, data.size, data.size, {0, 0, 0})
pytha.set_element_attributes(data.current_element, {name = data.name})
endWe recommend that you take a look at some of the samples where you can find more elaborate dialogs.