Using rehash for MEL scripts and import for Python scripts in Maya

I’ve only put up one instructional video and already I need to make a correction.  In my previous post I said that the rehash MEL command would load any scripts that have been dropped into Maya’s scripts folder.  It turns out, that’s only half true.  It works for MEL scripts, but not Python scripts.

If you’re loading a MEL script, or several, just put them in the correct folder, type ‘rehash’ into the MEL command line or script editor:
MEL script(s) will now be sourced by Maya, meaning you only have to follow the instructions that come with the individual scripts to run them.

With Python it is a little trickier, but not bad.  First you need to import.  Switch to the Python command line or go to a Python tab in the script editor, then type “import <filename>”  In this case, or test file is  You can leave the .py off, so your input should look like this:

Once your script is imported, you can then run commands from it.  This time you’ll type <filename>.<command>.  For our test, they are the same, so the command is pythonTest.pythonTest().  Note that you’ll need the parentheses for this one, but you don’t in MEL.

One bothersome thing about the Python scripts is that they have to be reloaded each time you start Maya.  This is easy to work with, though.  If you have a script you use a lot, just make a button that has the import command on one line and the function you want to call on the second line.  Here is what it looks like for my example.
Now just middle-mouse-drag that text to the shelf and you’ll have yourself a button that will import and run the command any time you want.


How to run scripts, source scripts, and make buttons in Maya

This is an extremely introductory lesson. If you’re familiar with using scripts already, you probably don’t need to read or watch.

I’m starting with a fresh install of Maya 2017 so you can see in these lessons how I set it up to accommodate my workflow.  First I’m going to take care of a couple of quick things that I always forget.  I’m going to turn on autokey:

And I’m going to change my undo queue in settings from the default 50 to infinite:

Alright, now lets talk about scripts.  There are two types we’re going to deal with, code snippets that run inside Maya, and sourced scripts that are stored externally.

This button will bring up your script editor:script-editor-icon

Once open, you’ll see tabs on the bottom half of the window, one for mel one for maya.  When you download a script you’ll know which is which because the file name will end in ‘.mel’ or .’py’.  Right now we don’t have to worry about it.  You can see I’ve entered a line of code into the mel window.  To run this code, highlight it and then click the execute button (it’s under the mouse and looks like a play button) or hit ctrl+enter.

Another way to run a line of code is to enter it into the command line at the lower left.  Right now it is primed to accept mel commands, but if you click where it says ‘MEL’, it will toggle between mel and python.  Enter your command and hit enter.  If you click so your cursor is in the text field, you can also use the up arrow to navigate back to previous commands.

To save some code as a shelf button, highlight it and middle-mouse drag it to the shelf.

To source scripts, you need to put them in your Maya folder.  By default it can be found here (according to my own experience or Google.  If it’s not there on a Max or Linux machine, you might have to look around:
Windows – C:\Users\<username>\Documents\maya
Mac – /Users/Shared/Autodesk/Maya
Linux – /usr/people/<username>/maya

From there you can put a script into the scripts folder if you’d like it to be accessible to all versions of Maya you have installed.

If you want to have the script only accessible to one version of Maya, click on the folder corresponding to the version you’re using and put it in the scripts folder there, or prefs/scripts.   Either should work. For example:

A downloaded script will usually have a readme with instructions or instructions embedded in the code.  Open it with any code editor and look at the top for instructions.  For this example I’ve highlighted the pertinent information.

For your newly placed script to be visible to Maya, you either need to close and restart the program or type the command ‘rehash’ into the MEL command line.*   After that just follow the instructions to get things started.  In this case type ‘arctracker108’ into the command line and the scripts GUI should appear.  If you highlight and middle-mouse drag that text to a shelf, you now have a button that will load the arctracker108 script any time you need it.

You can try this out with any script, but if you want to use the same specific one, you can find it here:

That’s it for today.  The last major way to run scripts is with hotkeys, which I’ll be going over next time.

*UPDATE: the ‘rehash’ command only works to refresh MEL scripts.  Python scripts must be imported differently and that is covered in the next lesson