PySide: Passing User Prompts

The past few months I’ve been working on a Maya toolset comprised mostly of animation related tools, though that will change in the future. I use Google Code¬†for source control and made the project freely available to everyone. The only caveat being that it is a work in progress and various bits are actively being deprecated. For those who don’t want to wade through lines of code I’ve decided to start posting useful snippets.

Here’s the story. We have a main dialog with a button. When the user clicks the button a prompt appears where they need to input a string. That string then needs to be fed back to the main dialog for later use. So how do we do this with PySide in Maya?

First the complete code snippet.

Now lets break it down.

In MainWindow() __init__ we declare the variable self.userInput, which is used later on to store the string from the user input.

Also in MainWindow() is the function doPopup(). This is where the prompt window is instantiated. Note that we are passing into PopupWindow a keyword argument called parent. Since PopupWindow is a prompt being called from MainWindow we pass in the instance of MainWindow.

On to the popup window class.

In the __init__() function we assign the parent keyword to the variable self.parent. This is done so we can access the instance of our MainWindow class.

Passing the user string back to MainWindow is done in doApply(). I put a couple examples in here to show the possibilities with this approach. The first shows how to directly alter the parent window. In this case I access a label in MainWindow and change its text.

The second is setting a variable within the instance of MainWindow. Here I set MainWindow’s userInput variable to the value of the prompt window’s lineEdit QWidget.

That’s it! Just a simple way to pass data from a prompt to a main window. There is one down side though. In this case the prompt references by name variables in it’s parent. Which means this approach won’t work if you wanted the prompt to be generic and usable in other UIs. Though you could always use the same variable names in the other UIs.