Maya: From Rhino To Maya Workflow

March 18, 2012
by admin
  • ScriptEditor-1

After the few Rhino Python tutorials that I made I though it would be nice to do the same in Maya.  The difference in the two programs is pretty big, and the workflow is very different.  Depending on the school, it seems to me that Maya and Rhino are some of the more common programs used in Architecture.  I think it is important for people to learn both programs as there are pros and cons to each.  Just as in Rhino, you can use python in Maya to access its API.  One difference is that Maya has had this capability for a few years, and going even further back has had MEL (Maya Embedded Language) and their OpenMaya C++ API.  When you get further into Maya it can be very confusing as you can use all the APIs within python.  For example, you call maya commands as import maya.cmds as cmds …. cmds.DoSomething() .  You can also import all of the MEL scripts (which are located in your maya folder and are interesting to look at if your learning to code for 3D) as import maya.mel as mel … mel.DoSomething() .

Another useful thing about python in Maya is the ease of importing external libraries.  I will make a quick tutorial later on how to do this, but it is really just changing one file.  The ScriptEditor is a very useful tool.  If you watch it, all of the commands you do show up.  So if there is something you dont know how to script, watch the ScriptEditor.  It is also really useful for doing some manipulation or creation, and then using the script to see the programming logic.

Script Editor in Maya.  The output window from user commands can be used  as reference in creating python code.

Before I move on to this Maya lofting tutorial through python, I want to quickly explain the Maya workflow.  In Rhino, most of the programming is done with creating a script, run the script, then prompt the user for selections.  In Maya, the opposite happens.  The common workflow in Maya is, user selects something, then run the script.  At first this may be confusing switching between the two languages, but it shows an important difference and reason why you might use one over the other.  The way Maya is able to have the user select something before running a script is due to its selection stack.  Maya keeps track of the order in which you select something.  So when you run a script it can look at different selection sets and you can call whichever order or index in the set you want.  When it comes to larger programs and user interfaces the game changes again.  Maya has PyQt built in, and has a pretty easy user interface API.  Most of the UI building creates callbacks to functions, so you are then able to have the user select something after starting a script.  This is a little more advanced and saving for later…

For the people who are intent on working in Maya like they do in Rhino (maybe the Rhino setup makes sense to you but you like the power of Maya tools) I will demonstrate this through a tutorial with the Loft command.  I chose the Loft command because it is a commonly used tool in Rhino for surface creation.  In Maya, the loft command can do some pretty cool things by keeping a history of how things are created.  This is the setup, but the next post will show off the tools capabilities.

First we need to get setup, so after opening Maya notice the bottom right has a split box, this is the Script Editor, so open it.  Once that is open, go to the Curves tab and start drawing some curves.

Arrange your curves in a way for them to be lofted.  For this demonstration I just put them on top of each other.

Before we get into the code part, make sure you are familiar with the Help tab in the Script Editor.  The python command reference is a great place to find out how to do things.  It gives you a list of commands and how you can call them from python, usually with some examples at the end.  The MEL reference may also be useful, as you can usually figure out a way to call those commands from python.

Now for some code:

The first step when using python and trying to call commands is to import maya.cmds:

import maya.cmds as cmds 

 As I explained earlier, Maya has a different workflow than Rhino, however, you can force it to do what you want.  In order to run the script and then ask the user to select something we will create our own tool.  the scriptCtx() command allows us to create a tool that will store selections and then run a command (or function).  Most of this code consists of strings that are output for the user, the string and their commands are colored yellow.  The next part to look at is fcs= .  what this is doing is sending a MEL style command to Maya.  This can be compared to calling a command line function in Rhino Python.  From here it is mostly understanding MEL commands.  First it takes the selection set 1, referenced by the variable $Selection1.  The semi colon represents going to the next line in MEL.  Then there is a ‘loft’.  You write this the same way that the MEL reference says.  The other parts of the code to notice is the setSelectionCount=2, which is how many things someone will select.  Another thing to notice is that curve=True, meaning the selection will be curves.  This is important as if this is set to something like points, your loft command will be upset receiving points and not curves.  The last thing is the variable this script is set to (ctx).

ctx=cmds.scriptCtx( title=’Attach Curve’, totalSelectionSets=1, fcs= “select -r $Selection1; loft -ch 1 -u 1 -c 1 -ar 1 -d 3 -ss 1 -rn 0 -po 0 -rsn true $Selection1[0] $Selection1[1] ;”, cumulativeLists=True,
expandSelectionList=True, setNoSelectionPrompt=’Select two curves close to the attachment points’, setSelectionPrompt=’Select a second curve close to the attachment point’,
setDoneSelectionPrompt=’Never used because setAutoComplete is set’, setAutoToggleSelection=True, setSelectionCount=2, setAutoComplete=True, curve=True ) 
 So after we figured out how to send MEL commands through a tool in python, we want to call our tool.  We do this by setting our current tool to the one that we just created.  
We can now run our script and select the curves. 
Depending on your maya setup the default may be set to wireframe.  I like to work in a solid color in order to see the effects more clearly.  If you are following this tutorial with no knowledge of Maya (maybe came straight from Rhino), this is where you can change the representation.
Onto Part Two

Leave a Comment