Creating dynamic tabs in UI

364 views
Skip to first unread message

flaye

unread,
Feb 13, 2014, 12:18:14 PM2/13/14
to python_in...@googlegroups.com
Hi,

I'm trying to build a UI class that will generate dynamic tabs based on a variable in the __init__ method.
The idea is this:
  1. set the initial variable number in __init__ (can be changed later on the instanced class)
  2. in the createUI() method, query the variable, and set it to dynamically create a tab in the formLayout
  3. Each tab will have common properties (distance from UI window, common buttons (which are in a commonButtons() method), as well as the possibility of customButtons())
  4. ...and that's it - for now.

I understand also that for this to work, the tabs themselves should be dictionaries, in order to get the dynamic creation happening.

I get the logic, but unfortunately the Python syntax still eludes me somewhat.

Here's the link to the code:

http://pastebin.com/KHFJF1jX

Much appreciated!

Justin Israel

unread,
Feb 13, 2014, 1:21:48 PM2/13/14
to python_in...@googlegroups.com

Did you mean to keep the second half of your layout code indented in the for loop or is that a typo? Starting with

    self.mainForm=mc.formLayout()

That happens once for every loop, replacing the references from the last iteration.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/81b82071-9f42-4c72-9517-92e10f068844%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

flaye

unread,
Feb 16, 2014, 11:19:30 AM2/16/14
to python_in...@googlegroups.com
Hi,

Thanks for the comment, Justin. I forgot the key rule of always simplifying first  to make sure it works, and then add the bells and whistles. It works now. You have to specify the number of tabs in the  constructor and then name each tab in the self.name variable.

http://pastebin.com/pWgfWtvT

The next step is to set it up so each tab has both the common buttons ("Create" & "Cancel") as well as its own unique custom buttons.

Is there an efficient way of making the custom buttons method different for each tab dynamically, based on a preset of established properties, or will they have to be "hardcoded" in the class instance?

I find using the class methodology very useful, but sometimes I wonder if I'm trying to over-complicate things by trying to create a catch-all framework for all possible scenarios...

Comments and suggestions always welcome.

p.s. the common buttons are not properly laid out yet. Setting up the the formLayout to arrange that now.

Justin Israel

unread,
Feb 16, 2014, 2:44:24 PM2/16/14
to python_in...@googlegroups.com
Sure you can make the tabs build their functionality dynamically. It just depends on what criteria they base their decisions. If you have N number of "types" of tabs then you can make different builder functions for each, and map them to their "type" in a dict. Or if it is even more dynamic, like being given any kind of object and it needs to create a form for its properties, then really you only need one function that knows how to inspect the object.

You may need to give an example of a couple "types" it can be given, and how you would psuedo build your result for them.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

flaye

unread,
Feb 17, 2014, 5:11:05 PM2/17/14
to python_in...@googlegroups.com
Hi,

Here's the updated working code so far: http://pastebin.com/WeHMMewB

It creates N number of tabs as per the variables in the init constructor. When the create button is pressed, it properly identifies the tab it's on.

For this specific example, I'm writing a script to create mechanical parts (gears, springs, pistons). Each tab will represent one of the parts. Obviously, each part will have it's own unique functionality (which will be created in the custom buttons), but all have a Create and Cancel button.

In addition, springs and pistons are interconnected (pistons will have the option to have a spring between the shafts).

Here's the pseudo-build:

  • import base UI as ui
  • in the __init__ define # of tabs, names of tabs
  • populate each tab with a unique geo generating class
  • create custom buttons from the ui class to interact with specific functions from each tab content
  • common buttons will create or cancel the relevant function in the tab


Also, if I create a new ui class that inherits the baseUI() class i.e.:

from file import BaseUI as UI

#create UI class

class NewUI(UI):
    def __init__(self):

    createUI(self)  #this method exists in the original BaseUI

    def createUI(self):
        ..change a single variable but leave everything else as is...


The createUI() method doesn't execute in the sense that in the original BaseUI class, it had the window/showWindow commands. If I modify only one variable in it, shouldn't it display the window along with the updated variable?
I hope that makes sense.

Thanks!

Justin Israel

unread,
Feb 18, 2014, 2:06:25 PM2/18/14
to python_in...@googlegroups.com

I'm not sure what you mean, but you could just create a couple different functions that take the parent layout as an argument. Or whatever is needed for each function to build its dynamic layout for its type.
Then your main layout can loop over a list and create each custom tab using each custom function. The main UI doesn't really have to know about the contents of the tab as each function sets itself up.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages