Sunday 10 January 2016

Python (Maya UI) - just finishing up the window

Ok - I was tossing up whether to post a final few very basic tips of UI advice here, just to finish up for now on working with UI's in general.  But hey, I think just for new users, it doesn't hurt to add those little tweaks and answer some very simple questions I'd been asked in the past....



In this article, I just want to look a little at getting those windows sized right and how we can control how much the window can be tweaked.  Obviously if you have a really sexy layout, the last thing you want is the user to stretch it out and have half your buttons look like they're uncentered and that nice top banner no longer look like a banner, but a floating graphic inside a window...

For those who have jumped to this article, read it and have no idea what wID is in the code examples, this simply refers to a variable that I would set up as a string that stores a name (or ID) for the window.  If that makes no sense what-so-ever, then you probably should look at my initial getting started article here.  In that previous article, I use the name winID, but variables can be called whatever - so here I'm just using wID...  Ok.  Probably babbling too much here, so lets move on.

Sizing the window properly

You probably noticed (or not if you were lucky) that you can set the size of a window when you call the cmds.window() command using two arguments, w and h, or for those who like to type more - width and height.   However you probably also noticed that this may not have worked every time (or at all) and that sexy title banner you painstakingly created to give your UI some pizazz is sitting on one side of your window.

The reason for this is that Maya remembers the size of the last window that it displayed, and in most cases it may not be the same as the new window you're generating...  Even if you set it when you create it, Maya just ignores it... Thanks Maya.

You can, however, get around this somewhat frustrating issue by simply forcing a window to be resized after its created (seems like double-handling... but it works) by calling cmds.window() again, specifying the arguments edit=True (e for short) and setting the and arguments rather then in the initial creation of the window.

cmds.window(wID, e=True, h=300, w=300)

Preventing a window being resized

Unless you specify it, a window will automatically be resizable by the user.  To lock that down, set the argument sizeable (s) to False

cmds.window(wID, s=False)

Titlebars

By default the title of your window will always be the windowID you use (if you use one).  You can display a new title using the title (t) argument instead.

cmds.window(wID, t='My AWESOME interface')

Keeping it on screen.
Using a normal titlebar always has the maximise and minimise options available in the top right.  Sometimes we don't want the user to minimise the interface, and lucky for us, we can disable these in a couple of ways.  The minimizeButton (mnb) argument is our first option.

cmds.window(wID, mnb=False)

It won't hide the button, but it will disable it.  You can do the same with the maximiseButton (mxb).  While maximise won't change a non-sizeable window, it does force it to the top-left of the screen...  Sometimes its nicer just to not offer that option.

Making it clean and minimalistic
If you'd rather just have a nice clean titlebar with perhaps no minimise or maximise buttons, then Maya also offers a toolbox (tlb) style title bar.

Smaller then the normal title bar, and tidier in appearance (well, at least to me)

This is a smaller titlebar, with just one close button.  Its pretty nice to use, and makes Interfaces look much slicker.

cmds.window(wID, tlb=True)

Hiding it - completely...
If you're creating a UI where you simply want it to be a floating window (a box without top title) then you can remove the title bar using the argument titleBar(or tb for short) and set it to False.

cmds.window(wID, tb=False)

However a BIG thing to consider with this is how the window will be closed - no title bar means no close button (or minimise/maximise either) so you'll need some way to get rid of the UI.

Super tidy - nothing, nada, zilch in terms of titlebar

As mentioned in one of my previous articles, you should always test if your window exists first using the cmds.window(wID, exists=True), and then delete the window using a called to cmds.deleteUI(wID).

That will take care of removing the window if you were to run the script again.  However you don't want to repeatitively call a script that removes a window and then recreates it.  You'll never get it closed that way, and you'll be stuck in an eternal loop until you quit Maya.

Adding a close button that calls the deleteUI() command is something you'll want to consider adding here.

cmds.button(label='Close Window',command="cmds.deleteUI('%s')"%wID)

In conclusion...

Well, at this point I think there's enough material across my blog articles to get you up to speed with the core basics.  Like so much in Maya, there is way more to learn - but part of the fun in coding is the discovery.

At some stage I will look at explaining some more advanced things like custom-making shelves and so forth, but that's something for another day.

Until next article, hope this has been useful and we'll see you soon...

2 comments :

  1. Thanks for the post. I played with those options a couple days ago and they definitely help.

    My workaround for the window size problem was creating first a row based layout, and assigning a height, then as you did on your basic UI post, used columns and specified width.

    But your workaround is faster and I'll use it form now on.

    ReplyDelete
  2. Thanks Beto! I'm glad to hear this stuff came in handy. In fact, I'm always happy to hear when some of my nonsense I post on the blog helps someone out (or at least offers some advice that came in useful). :)

    ReplyDelete