Simple Maya Progress Window using Python
_
I
thought I'd write a basic, beginners, tutorial for someone who is a
Mel, or more aptly a procedural scripter, who want's a nice little
introduction to some object oriented concepts. It took me a
while to get my head around this when I was migrating over, from mel
to python, so hopefully this will help someone.
So here's how to wrap a progress window into a neat little class. Something like this can really speed up adding these to a script. If you are a Mel scripter and are wondering about why use to python, this should give you a good, basic example of an object oriented approach to a simple task and why it is a big advantage for the language over Mel.
Let's breakdown what we might want to do with a progress window:
1. create it
2. update it
3. reset it
4. kill it
While we can do this via commands to the cmds.progressWindow, or whatever, it involves unintuitive syntax... Ok, in this example its not really that bad, but mel and cmds commands can get flag heavy!
So it would be nice to be able to create a window, then call really simple, remember-able, calls on that window. So, we need a class, and some methods.
Here's some python based on maya.cmds code.
So here's how to wrap a progress window into a neat little class. Something like this can really speed up adding these to a script. If you are a Mel scripter and are wondering about why use to python, this should give you a good, basic example of an object oriented approach to a simple task and why it is a big advantage for the language over Mel.
Let's breakdown what we might want to do with a progress window:
1. create it
2. update it
3. reset it
4. kill it
While we can do this via commands to the cmds.progressWindow, or whatever, it involves unintuitive syntax... Ok, in this example its not really that bad, but mel and cmds commands can get flag heavy!
So it would be nice to be able to create a window, then call really simple, remember-able, calls on that window. So, we need a class, and some methods.
Here's some python based on maya.cmds code.
|
import maya.cmds as cmds
import maya.mel as mel class alt_progressWin (object): """ Simple wrapper class that will build a basic progress window. The initialation can take up to three arguments, title, display and a maximum value. Methods: -> update( amount ): Increases the progress bar display by the given amount -> kill( ): Closes the window -> reset( displayStr ): Sets the progess bar back to zero, and allows you to pass in a new display string """ WIN_NAME = "" PROGRESS_BAR_UI = "" TEXT_UI = "" WIN_TITLE = "Progress Window" WIN_DISPLAY = "Progress:" MAX_VALUE = 100 VALUE = 0 WIN_WIDTH = 260 WIN_HEIGHT = 80 def __init__(self, winTitle = "Progress Window", displayText = "Progress:", maxValue = 100 ): self.WIN_TITLE = winTitle self.WIN_DISPLAY = displayText self.MAX_VALUE = maxValue self.WIN_NAME = cmds.window(title = self.WIN_TITLE) cmds.columnLayout(width=(self.WIN_WIDTH)) self.TEXT_UI = cmds.text(l=self.WIN_DISPLAY) self.PROGRESS_BAR_UI = cmds.progressBar(maxValue=self.MAX_VALUE, width=(self.WIN_WIDTH-30)) cmds.showWindow( self.WIN_NAME ) cmds.window(self.WIN_NAME,e=True, width=self.WIN_WIDTH, height=self.WIN_HEIGHT ) def update (self, amount=1): """Increases the progress bar display by the given amount""" if cmds.window(self.WIN_NAME, q=True, ex=True): if cmds.progressBar(self.PROGRESS_BAR_UI, q=True, ex=True): self.VALUE += amount cmds.progressBar(self.PROGRESS_BAR_UI, edit=True, progress=self.VALUE) cmds.text(self.TEXT_UI, e=True, l= (self.WIN_DISPLAY + " %" + `self.VALUE` ) ) if self.VALUE >= self.MAX_VALUE: self.kill() def reset(self, displayText = "Progress:"): """Sets the progess bar back to zero, and allows you to pass in a new display string""" if cmds.window(self.WIN_NAME, q=True, ex=True): if cmds.progressBar(self.PROGRESS_BAR_UI, q=True, ex=True): self.WIN_DISPLAY = displayText self.VALUE = 0 cmds.progressBar(self.PROGRESS_BAR_UI, edit=True, progress=self.VALUE) cmds.text(self.TEXT_UI, e=True, l= (self.WIN_DISPLAY + " %" + `self.VALUE` ) ) def kill(self): """Closes the window""" if cmds.window(self.WIN_NAME, q=True, ex=True): cmds.deleteUI( self.WIN_NAME ) |
_
With this code we can now simply create a progress window, in Maya, by initializing an object of this class:
pWin = alt_progressWin()
The window is built on object initialization (__init__), so we can now access the methods of the class, through 'pWin' variable, and change the progress status of the window. To change the progress amount of that instance you can use the update method:
pWin.update(2)
So you can just recall the update() method, in a loop or whatever, to increase it by a factor. Also you can reset the progress bar, this allow you to reset the display text of the window and resets the progress to zero.
pWin.reset("Doing something else now!!")
The reset method is handy as you don't need to kill the window and rebuild it with a new text string. And finally you can 'kill' the instance of the window.
pWin.kil()
That's it. Really, really simple but shows how using a class structure for things really allows clean usage.
I hope somebody finds this useful.
With this code we can now simply create a progress window, in Maya, by initializing an object of this class:
pWin = alt_progressWin()
The window is built on object initialization (__init__), so we can now access the methods of the class, through 'pWin' variable, and change the progress status of the window. To change the progress amount of that instance you can use the update method:
pWin.update(2)
So you can just recall the update() method, in a loop or whatever, to increase it by a factor. Also you can reset the progress bar, this allow you to reset the display text of the window and resets the progress to zero.
pWin.reset("Doing something else now!!")
The reset method is handy as you don't need to kill the window and rebuild it with a new text string. And finally you can 'kill' the instance of the window.
pWin.kil()
That's it. Really, really simple but shows how using a class structure for things really allows clean usage.
I hope somebody finds this useful.