The purpose of this tutorial is to show how to build Maya GUI , using PyMEL. As an example I will use my own script, that helps to automate image based lighting setup for Mental Ray.
You can find and download this free script from CreativeCrash. The name is - Asset Lighting Maya GUI.
With this GUI you can create lighting setup - (IBL node and set of lights) and point it to the chosen camera.
There are two presets - for outdoor lighting (IBL and directional light) and for indoor (IBL and standard three area lights setup).
You can pick your own HDR image and individual colors for lights.
The position and scale of lights based on dimensions of visible objects in the scene.
There are also few controls to manage selected objects. You can:
- hide unselected objects
- "frame" selection - move center of selected objects to zero point, preserving parenting
-create turntable animation with specified duration
-reset any of the above
This tutorial requires some basic knowledge of PyMEL. If you want to know more about it – visit official Autodesk page - https://autodesk.com/us/maya/2011help/PyMel/index.html
To download PyMEL – https://code.google.com/p/pymel/
GUI was tested in Maya 2011, and should work for versions 2010 and 2012 as well.
Lets get started.
I will show just certain parts of code, breaking for comments, skipping repeated and unimportant.
You can download the script to see all code.Basically, we are defining new class – AssetLightWindow:
It worth noting following:
The class method showUI is a special method that allows us to call it without creating the instance of class.
__init__ method is used to keep data attributes. Python automatically call it as soon as instance is created. I am skipping all definitions in this method. They are unimportant for this tutorial.
Create method will be called at the moment of creating the instance of class.At the beginning we delete window if it already exists.
There are many layout commands or controls can be used to build your GUI.
FormLayout() is a very flexible way to organize them.
It gives you a numbers of positioning options.
AttachForm flag allows you to pin control’s edges to edges of layout form.
AttachPosition – allows to pin control’s edges to specific coordinates in layout form coordinate space(dimension of which was passed as argument to FormLayout() command)
AttachControl –allows you to pin control’s edges to another control.FormLayout() is the basis you can put various controls on. Every control, defined after FormLayout() will be automatically parented to it. To change current parent, you can use setParent() method.
As far as there are three sections in GUI, three additional form layouts created. First of them -
modeGrpForm is defined above. Then, new option menu control is created. It has all possible lighting mode options, defined in __init__ method in the list listOfLightModes.Method on_mode_change() mentioned as a response to action of choosing new option.
There is new control created – option menu for existing cameras in the scene. Method cam_option_list() searching for cameras and fills up option list.
textFieldButtonGrp () – new control, showing current HDR image path. It has button, that opens open file dialog.
ColorSliderGrp()- color picker control, defining color of the light.
Two button controls, calling relevant methods that create and delete lighting rig.
I am skipping the part of code for another two sections of GUI controls.
They are using the same controls and methods mentioned above.
The last statement of create() method shows new window -
I am skipping description of all methods, except one, that builds lighting rig.
They are all pretty easy for understanding, and performs quite basic operations.
First two if clauses check if Mental Ray is loaded. And if it does – if mentalrayGlobals is active.
MentalrayGlobals is a node that contains all important mental ray attributes. We need to connect IBL node to MentalrayGlobals to have image based lighting works.
It worth mentioning that Maya keeps mentalrayGlobals inactive until you start configuring options in Features or Indirect Lighting tabs. MEL command miCreateDefaultNodes force the creation of all the mental ray nodes in the scene.
In order to scale and position lighting elements properly, dimensions of all visible objects is calculating.
The code above creates IBL sphere and connect it to appropriate HDR image.
This section creates lights, based on chosen option of lighting mode. For “Outdoor” mode only one directional light is created. In most cases its combination with IBL node is sufficient to light the scene.
In case of “Indoor” mode the standard three point lighting setup is created.
Lights are distributed and scaled based on dimensions of visible objects in the scene.
Code above groups all lights and IBL sphere and points it to render camera chosen in option menu. Constraints are using temporary and have deleted, after aiming is completed.
At the end it fills up list of all lighting objects. It will be used in the following method to delete lighting objects in the scene:
After defining class and all methods, class method showUI() is called to create window:
That’s all. Thanks for your interest.I hope you’ve got something new for yourself.
Author: Alex Khan
Submitted: 2013-02-23 20:10:18 UTC
Tags: GUI, Mental Ray, pymel, and Python
Mass Instancer for Maya 1.6.0 (maya plugin)
Baby Walker Cartoon 01 3D Model
Baby Walker Cartoon 02 3D Model
Baby Walker Cartoon 03 3D Model
Cessna TTx 400 3D Model
Bucker Bu131 Jungmann 3D Model
Tree Cartoon 3D Model
Diamond DA62 3D Model
Generic Future Helicopter 3D Model