…Computer Programming…. Does that sound scary and yet inviting all at the same time?
Are you ever curious to dive into programming and Maya? Are you a power user of Maya, or maybe interested in customizing or automating work flows? Are looking to take your work to the next level and challenge yourself, but not sure where to start… Here I will provide you with some places to start so you can get familiar with scripting so that you are more comfortable and less overwhelmed.
"Maya User, I would like to introduce you to the Script Editor", it is found here in the UI, with a default install it is placed at the bottom-right of the Maya application:
When you click on the Script Editor you see this UI pop-up:
The Script Editor has numerous features and functionality, the documentation covers what each button does, so look into the help documentation for more information if you are curious to know this UI (User Interface) inside and out.
We like to think of the Script Editor as having two halves, top half (Command History) and bottom half (Command Editor). The key now it making these two halves work for you, to help you learn how to script.
Step One: Monitoring the Command History.
Something that Maya does that is pretty special is for almost all actions that you perform in the UI; Maya outputs the equivalent MEL command into the Command History pane of the Script Editor. Super Cool!!
In addition the Command History pane, displays any feedback such as results, warnings, or errors returned by Maya, which can be helpful not only when learning scripting!
So in other words, for every work flow in the UI, it tells you the MEL solution. In time, you will figure out how we get from Point A (Maya Action) to Point B (MEL Command for the Maya Action), knowing where you are going is half the battle, doing it on your own is the other half the battle.
If you don’t see the MEL Commands executed you need to make sure you have not suppressed them and that the History Output is selected for both MEL and Python.
Step Two: Piecing together code in the Command Editor.
This is where the famous copy and paste comes in handy, copy from the top panel and paste in the bottom panel to piece together your custom automation work flow.
However this is the part where you need to be curious, and what I am mean by curious is that you have to want to look up all the parts of the command so that you can start to learn what commands do what and what the flag and flag parameter syntax is about. This is the fundamental structure of scripting, so it’s important to understand it. To look up the different commands you can find these in the Help Documentation, hot-key F1.
No one is expecting you to memorize anything, you don't need to pull out of your head a MEL command at the drop of a hat, that's what command docs are for, there over 600 Maya commands so don't stress about learning them, the key is learning how to use the documentation efficiently.
Once you get more comfortable with using Maya Commands and the syntax you can start diving into programming basics such as data types, variables, for loops, etc. With these programming structures you can create scripts that are what we like to call more intelligent, because they can do different things based on what is happening in the scene. But we will leave that for another day.
Step Three: Saving your beautiful scripts for Later.
Once you have put together a script, you can drag it into the shelf inside the Maya UI to save it for later and re-use it over and over. To complete this highlight your new script:
Press the 'Alt' key while dragging your highlighted script into the Shelf tab of your choice, you are then prompted with whether the code is MEL or Python:
The final result now is your code in the Shelf; press the new button to execute it:
Real World Example: Applying Step One through Three.
An example of a real life work flow could be to set up a scene the way you want it every time, so all you have to do when you come into Maya is click one button to get all the default things you want in your scene setup.
If we want to ensure nothing is in the scene, let’s do a file new, and we see this line in the Script Editor, let’s copy it ‘file -f -new;’, transpose it into Python, we need to make sure we import the maya.cmds module:
import maya.cmds as cmds
cmds.file(f=True, new=True)
Now we want to create two spheres, so we copy those MEL commands and transpose them into Python:
cmds.polySphere (ch=True, o=True, r=27.080953)
cmds.polySphere (ch=True, o=True, r=5.580194)
Full Script:
import maya.cmds as cmds
cmds.file(f=True, new=True)
cmds.polySphere (ch=True, o=True, r=27.080953)
cmds.polySphere (ch=True, o=True, r=5.580194)
I realize this sample is somewhat easy, but starting to script can be this easy, just remember be curious always look up flags, for example, what is the flag 'ch on the command PolySphere, what type of value does it take, etc.
Enjoy
Kristine
Comments
You can follow this conversation by subscribing to the comment feed for this post.