Maya Utilities Part IV
March 30th, 2011
In the last of this short series about my Maya Utilities code, I want to look
at how it all comes together, how I use it on a daily basis, and share the
code with you.
So, putting it all together, I have a folder called mayaUtils,
containing four files:
__init__.pyfiles.pymodeling.pynodes.py
Button
decorator) in the three submodules:
files.py
@Button("") def openLast(arg_not_used):
@Button("") def revertToSaved(arg_not_used):
@Button("") def savePlus(arg_not_used):
@Button("Project") def project(arg_not_used):
modeling.py@Button("Edges") def selectEdges(arg_not_used):
@Button("Edges") def setAngle(arg_not_used):
@Button("Normals") def toggleAllNormals(arg_not_used):
@Button("Normals") def toggleNormals(arg_not_used):
nodes.py@Button("") def finder(arg_not_used):
@Button("") def swapInstance(arg_not_used):
For comparison, the image on the right shows what the UI generated by this code looks like. Please note, that the order in which the functions are defined in the files is irrelevant, as all entries are sorted alphabetically before the UI elements are generated.
As you can see, the top level of the window is a collabsible frame called “Utilities”. The reason for that, is that the window can be made really small by collapsing this frame, to get it out of the way. Inside this frame, there is one frame for every submodule. The name of each frame corresponds to the name of the submodule, and like all names in the UI, is nicely spaced and capitalised. Inside each of these groups, there are buttons and groups. You can see how buttons that were assigned to the “” group are shown straight in the frame of their files, while the others are grouped in the respective subframe. Now, there are two ways one could use this code. One way, would be making it a Maya plugin, and load it through the plugin manager. I have chosen not to do it this way for a simple reason: a plugin will be saved as a requirement for a given scene, and Maya will moan if it cannot find the plugin when opening such a scene. Now, since the functions in my Maya Utilities don’t really contain anything that a scene could depend on, this is more of a nuisance than a feature. On top of that, as I mentioned before, I am currently doing most of my Maya work on a portable hard drive, but not all of it, and the Maya Utilities happen to live on that hard drive. So it can happen, that I will start up Maya without the hard drive being plugged in, and I don’t want that to hamper me. So, I opted for another solution: theuserSetup.py file. Being
the pythonic counterpart of the userSetup.mel file, this file is
executed every time one loads up Maya. All I want it to do, is to check,
whether the path under which the hard drive is usually mounted is available at
startup, and if so, add it to the Python path and import
mayaUtils. Here’s what the code in my userSetup.py
looks like:
import os
if os.path.exists("/path/to/mayaUtils"):
import sys
sys.path.append("/path/to")
import pymel.core as pm
pm.evalDeferred("import mayaUtils")
Notice how the check on line 2 contains the path up to and including
the mayaUtils directory, while the path that is appended to
sys.path on line 4 is only the path up to and excluding
the mayaUtils directory.
Also, since the code in the Maya Utilities uses a lot of PyMEL, I am importing
it already as well, so it is in the main namespace in the script editor and
command line. I could as well do this outside of the if branch.
And that’s it, really. Should you find this anyhow interesting or useful,
feel free to grab
a copy of my code and have a play. And should you end up
writing some code with this framework and feel inclined to share it, please
drop me an email.
Leave a Reply