This is my initial example for UI programming in Python.
I'm using the one-shot format for this example, a future example will use the Class Based form.
And, while I ran it it Modeler, it should work in Layout as well.
Feel free to point out where I've made mistakes as well.
- - - - - - - - - -
So, You want to program a UI for a Lightwave Python Script?
(Notes are current as of Lightwave 2020.0.1)
So you have this neat idea for a Lightwave script, and you know Python, and it requires a UI.
When you go flipping through the Lightwave Python documentation, you find that there is absolutely no documentation on UI programming in the Python SDK documents, and when you ask on the Lightwave forums, they tell you to use the documentation for the C SDK, which is not very helpful, since they also tell you it's almost a one for one match, but neglect to tell you what doesn't match up.
I hope to alleviate this lack of knowledge.
A simple UI that requires only parameter entry isn't that difficult, it turns out, its just made harder to do because the documentation doesn't exist (and has been reported as such twice, and no action has been taken yet).
First, like any Python plugin for Lightwave, you'll need to import the lwsdk module.
Next, you'll need to do this:
Code:
#Your UI code starts here
ui = lwsdk.LWPanels()
panel = ui.create('Your Script's Name')
#These are optional
panel.setw(400)
panel.seth(300)
This creates the object for your panel, and allows you to access the methods to define your controls on the panel. If you do not specify a panel width or height, the UI module will automatically handle it. Otherwise, using the .setw(int Width) and .seth(int Height) methods allows you to set the panel's width and height, respectively.
To add controls, you do the following:
Code:
my_control = panel.popup_ctl("Control Label",['Choice 1','Choice 2', 'Choice 3'])
#This is optional
my_control.move(5,5)
This adds a popup control to the form, and moves it to position (5,5) from the upper left corner of the panel. If you do not specify a control position (the .move method), the UI module will handle it later.
To have the UI toolkit handle your control position and alignment, you'll need this:
Code:
#Vertically Align Input areas
panel.align_controls_vertical([my_control])
#Resize panel to hold all the controls, plus margins
panel.size_to_layout(5,5)
If you are handling the positioning of your controls via the .move method, you do not need these two lines of code. If you are letting the .align_controls_vertical method handle your alignment, you need to pass it a list containing all your controls. You can either create the list as shown here, or append your control to the list using the .append method (mylist.append(my_control)), and pass the list name as your argument. Do not use .extend to add your controls to the list, this will cause a runtime error.
To display your panel, you need the following:
Code:
if panel.open(lwsdk.PANF_BLOCKING | lwsdk.PANF_CANCEL) == 0:
#The Cancel button has been pressed, Abort Execution...
ui.destroy(panel)
my_value = 0
else:
my_value = my_control.get_int() + 1
You need to supply flags to the panel.open() function, these determine if the panel is modal, non modal, and what buttons are displayed at the bottom of the panel; In this case, "OK" and "Cancel" buttons. In a Class style plugin, you would want to pass a return value to lightwave. In a one shot plugin, you need to handle it in another fashion. Usually, an else: statement.
Code:
else:
my_value = my_control.get_int() + 1
This gets the value of the control, in this case, which choice we made. Because Python lists start at 0 instead of 1, we're adding a 1 to the output. We're using 0 as the no choice made value for this example.
unlike LScript, which uses a generic getvalue(control name) function, getting values for a control in Python requires a .get_(type) method for the specific type the control handles. Since the popup_ctl returns an integer index, you'll need to use the .get_int() method to get the value and store it in a variable.
And that's a simple UI in Lightwave's Python SDK.
Now, onto the actual details...
----------
Here is the complete one-shot format script of the above...
Code:
import lwsdk
"""
A one-shot Lightwave UI Example
27 June 2020, by Steven Pettit
"""
#Your UI code starts here
ui = lwsdk.LWPanels()
panel = ui.create('Your Script\'s Name')
#These are optional
panel.setw(400)
panel.seth(300)
#Add a pop-up control
my_control = panel.popup_ctl("Control Label",['Choice 1','Choice 2', 'Choice 3'])
#This is optional
my_control.move(5,5)
"""
If you want Lightwave to handle the panel layout,
comment out the optional sections above, and uncomment
The section below.
"""
"""
#Vertically Align Input areas
panel.align_controls_vertical([my_control])
#Resize panel to hold all the controls, plus margins
panel.size_to_layout(5,5)
"""
#Display the Panel, with Buttons
if panel.open(lwsdk.PANF_BLOCKING | lwsdk.PANF_CANCEL) == 0:
#The Cancel button has been pressed, Abort Execution...
ui.destroy(panel)
my_value = 0
print("You pressed the Cancel Button")
else:
my_value = my_control.get_int() + 1
print("You chose choice ", my_value)
(Yeah, I'm aware this isn't much of a plugin, but it does work as an example.)
Bookmarks