Tutorial 1: Basics

bob-white edited this page Jul 24, 2017 · 3 revisions

This page assumes you've followed the instructions on the Getting Started page. If you haven't installed mGui, you should follow the instructions there first

Creating a window and messing with properties

Windows in mGui are basically indentical to their maya.cmds counterparts. In your listener, try this:

from mGui.gui import *
     
# create a window
test_window = Window(title = 'test window', width = 256, height = 256)
test_window.show()

That'll pop up a blank window. The first point of interest is that the window has a built in show() method; in vanilla commands you'd have to call showWindow() on the window to make it visible. As an experiment, try this in the listener:

test_window.hide()

which will make the window disappear, and

test_window.show()

to make it reappear.

You can play with the window's properties using mGui property access. Like all mGui controls, the window uses the same control flags as the underlying maya command -- in this case, cmds.window. So you can try some variations on these settings:

test_window.backgroundColor = (1,0,1)
test_window.width = 300
test_window.height = 32
test_window.title = "a slender purple window"

MGui objects vs Maya widgets

Once you're satisfied with playing with properties, try closing the window using the close box. As expected, the window disappears. Now type this into the listener, select it, and hit enter:

test_window

What you should see is a printout like this:

# Result: <deleted UI element <class 'mGui.core.Window'>> # 

You can also try this:

if test_window:
    print "it's alive!"
else:
    print "buh bye!"

It should print out 'buh bye!' What you're seeing is that the mGui window object still exists -- but the window that it was created to manage has been deleted. If you need to know whether a particular widget is still around you can just truth-test it's mGui object; if the the widget exists the mGui object will evaluate as True, and if not it will evaluate as False. So it's a common idiom to try something like

if my_window:
    my_window.show()
else:
    my_window = Window(....)
    my_window.show()

when you're not sure if a user might have deleted my_window or any other mGui object.

As an aside, if you want to know what the underlying Maya widget really is -- that is, if you're looking at for the string that maya.cmds would use when accessing a window, a button or whatnot -- it's stored in the mGui object in a field called widget. If you ever need to pass an mGui widget to something like an old MEL script, you can just pass its widget field and it'll work like any other piece of Maya UI.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.