Dynamic re-usable GUI class with PyMel

Hey guys :slight_smile:

I’ve been searching around for examples of a reusable GUI class written in PyMel, I’m wondering if there’s any examples of this online? I’m still pretty fresh on OOP with Python, and I thought maybe that would be something I could learn a lot from.

Could you be more specific about what you’re looking for? An OOP oriented dialog box, or something like that?

I’m sorry, I’ll try :slight_smile: The issue I want to address is that when I’m creating GUIs for my scripts, it adds up to thousands of lines of code combined, and much of the UI-stuff I write is kinda similar. To be honest I’m not sure specifically what I’m asking, but I thought maybe that people created a class with small functions to populate the UI, so whenever they create a new script that they need a UI for I imagined that they could build that off from a template.

In my head, a pseudo example of creating a new UI based on a template would be something like this:

class mYnewUi(templateUI):
    #UI: Define the name and the title of the window
    def templateUI.window():
	    winName = 'NameOfWindow'
	    title = 'TitleOfWindow'
    #UI: Building the information-form of the window
    def templateUI.infoForm():
    	iTab = 'Information'
    	iText[0] = 'This is line one of the text in to info-form'
    	iText[1] = 'I would be able to write X number of line, and the templateUI would contain'
    	iText[2] = 'functions to align everything for me regardless of how many lines I write here'
    #UI: Building the action-form of the window
    def templateUI.actionForm():
    	aTab = 'Rig Character'
    	aText[0] = 'Make sure the character is pointing forward in Z'
    	aButton[0] = 'Create Skeleton' -command createSkeleton
    	separator
    	aText[1] = 'Make sure the joints have frozen transforms before before clicking Apply'
    	aButton[1] = 'Rig Character' -command rigCharacter

    #UI: Gather the commands for creating the UI
    windowCode = templateUI.window()
    infoUiCode = templateUI.infoForm()
    actionCode = templateUI.actionForm()

    #UI: Build the actual UI
    myWindow = templateUI.buildUI(windowCode, infoUiCode, actionCode)

I’m probably waaay off on my pseudo code, but I guess what I’m looking for is just way to ease the UI-creation and automate the alignment of all of the forms on each window I create.

ya but that means you spend a massive amount of time creating stuff you might never use, or it wont be flexible enough for everything. Also if you were to implement such a thing, your better off implementing parts of the UI as there own classes, instead of 1 classes with a boatload of methods, since this way you can reuse method, and attributes names for new UI sections instead of needed unique names for everything, which is easier to work with in the end, and just create a way better API for it.

here is one of my older scripts with a more advanced UI pbdDo i pretty much just built a UI like normal with pymel, than implemented all parts that i know will be repeating with there own classes or parts that i know are dynamically generated such as the matUI section.

Also took advantage of pymels usage of the with statement to better organize my layout in code form, and remove the need to manually set parent kwargs.

I just see problems coming up from your idea since if we look at a simple part of the maya UI the tools options use they all consist of the same general design, window name of tool for title, a few frameLayouts, than apply and cancel buttons at the bottom. Your idea does all of that fine, but than falls apart, since not all of these frameLayouts contain the same kind of UI elements, so you would need to create tons of commands for populating them with various kinda of UI elements, which at that point you might as well just use the basic pymel UI commands instead of making your own methods for it.

If your making lots of UI with similar layouts i really think your better off just creating code snippets to use in your code editor, that setup the basic structure for you, and let you go in and fill in the vars, and add too.

Here are a couple of small things you can do to make yourself more productive w/o reinventing the whole way to do maya gui…

  1. Write a wrapper for formLayout. It’s the most useful and flexbile layout but also the biggest pain in the ass to write for. So, code up a little class that wraps a formlayout with some syntax sugar and commonly used cases.

  2. Learn Maya UI templates. A lot of what you want in terms of simplification and avoiding boilerplates can be gotten with templates.

  3. Study up on the Model-View-Controller (MVC) and Model-View-ViewModel (MVVM) patterns. They are common in things like QT or WPF apps, not so much in Python although our own @yasin has a tutorial on youtube on this very subject. A lot of complex UI boils down to picking the right display template for a piece of data. In our world the template is just a function that takes a data object and a produces some UI that works on that object – you can mass produce them for lists or have them be one-offs; the surrounding code won’t care. With an MVC framework in place you can split your display code and your functional code more cleanly than the normal maya style, and noodle on either end without disturbing the other.

If after all that it still looks like too much, maybe consider getting PyQT designer and doing your UI declaratively that way; then “all” you have to do is hook up the events and go.

Of course, all GUI programming is a pain in the ass. If there is a magic way around it I’d sure like to know too :slight_smile:

[QUOTE=Theodox;22495]
3) Study up on the Model-View-Controller (MVC) and Model-View-ViewModel (MVVM) patterns.
[/QUOTE]

I have long wondered where to find more a generalized “theory” approaches to tool scripting.
I never thought that there would be a set of software design patterns focused specifically on UI stuff.
It make sense in hindsight.

Make that “Dynamic Re-usable GUI class with PyQt” and you’re already half-way there.

Any specific reason you prefer the GUI tools provided by Maya?