Archived entries for MEL

timed Maya Hotkeys

I like the functionality of the space bar in Maya, hit it quickly and it swaps between single and quad view, but hold it and it shows you the hotbox and release and it leaves the panel views the same. I wanted to replicate this timed nature in some of my own hotkeys. It was actually pretty simple; using the PRESS and RELEASE hotkey settings and a very simple timer check you can change the command depending on the length of time the hotkey was held for.

Below is a sample function using this idea, you would just need to rename some stuff and fill in the parts that actually do something when the key is pressed or held. I’ve used this idea to create some extra functionality for hiding/showing, and also a modified fit to panel command that allows different levels of zoom when holding the button.

global proc timedHotkeyFunction()
	global float $timedHotkey_timer;

	//Use -1 to reset the timer on release
	if ( $timedHotkey_timer < 0 )
		$timedHotkey_timer = `timerX`;
		float $heldTime = `timerX -startTime $timedHotkey_timer`;
		$timedHotkey_timer = -1;

		//Resets the timer if the time is too long, this is to guard against
		//cases where the press and release get mixed round.
		if ( $heldTime > 10 )
			$timedHotkey_timer = `timerX`;
			print "\nReseting Hotkey timer - held for too long!";
		//Time to define a press or a hold, 0.3 seems to work quite well
		else if ( $heldTime < 0.3 )
			print "\nPressed!";
			//Code for pressed function here.
			print "\nHeld!";
			//Code for hold function here.


//Define the runTimeCommand that will show up in the hotkey editor, this command
//needs to be assigned to both the PRESS and RELEASE hotkeys
if ( !`runTimeCommand -q -ex timedHotkeyTest` )
		-annotation "Set this to both PRESS and RELEASE on your hotkey"
		-command ("timedHotkeyFunction")


I find I have to diagnose problems with Maya fairly often; we’ve got a fairly complex setup environment to allow us to quickly switch projects with a completely different set of tools and scripts, but as it’s complex it inevitably goes wrong. I’ve written a nice little diagnosis script that basically just spits out a load of information about how Maya is setup, which allows me to check alot of different common problems on my own machine, allowing me to disrupt the user a little as possible.

The main part of the script is a diagnosis log output, this queries and then writes a log of different aspects of the Maya environment; ‘whatIs’ commands on various scripts, listings of environment variables, and listings of optionVars. Copies out a log of the script history, when scripts crash they usually output something into the history. Ability to copy various folders from the users machine so the scripts can be thoroughly checked, normally just the scripts directories in My Documents, but any folders could be copied that were important to the team setup. An extra section that will allow an extra script to be run and the results included in the log file.

To use the script you’ll have to alter the default output path to somewhere both you and your team can access, I’ve got it set to a network share. You can also easily alter some of the lists of things that are included in the log, these are all marked in the comments of the script.

Download: diagnosisMaya

optionVarBrowser Mel

Maya makes use of optionVars extensively throughout its UI, they are used to store information between Maya sessions. Every little bit of UI, settings, etc, are stored in optionVars when Maya shuts down so that everything is kept the same next time you load it up. When you start using them alot it can become cumbersome to query and debug the variables and their contents. This script helps by wrapping up the different useful operations into a nice UI. Start by typing a search string into the top input box and hit Search. It will show all optionVars that contain the string below. Selecting a variable in the top list will show its value(s) below. You can also add or delete variables with the two buttons at the top left. There are buttons to alter the contents of a variable at the bottom left; append adds a new item at the end, insert adds the item above the current selection, and remove removes the selected item. If you’ve got a non-array variable selected you can still use these commands and it will be changed into an array – be careful with this as any code that uses the variable will crash if it doesn’t get the sort of variable it was expecting. Any suggestions or feedback, leave a comment.

Download: djb_optionvarbrowser.mel


Scripted Clipboard Access in Maya

I recently had a situation where we needed to access the data in the clipboard from within a Mel script; in straight mel/python there is no way to access this data (I guess because Maya is cross platform, and clipboard support is OS specific). But by using the pyWin32 extensions we can get access to the clipboard in Windows.

I’ve written two very simple mel functions that allow you to copy and paste a string to the clipboard directly.

clipboardCopy( string ) will put the string onto the clipboard.

global proc clipboardCopy( string $text )
	//install pyWin32Extensions, needed to access the clipboard

	python( "import win32clipboard" );

	python( "win32clipboard.OpenClipboard()\nwin32clipboard.EmptyClipboard()\nwin32clipboard.SetClipboardText('" + $text + "')\nwin32clipboard.CloseClipboard()" );

clipboardPaste() will return the conents of the clipboard.

global proc string clipboardPaste()
	//install pyWin32Extensions, needed to access the clipboard

	python( "import win32clipboard" );

	python( "win32clipboard.OpenClipboard()\nd = win32clipboard.GetClipboardData( win32clipboard.CF_TEXT )\nwin32clipboard.CloseClipboard()" );
	string $clipboardText = python( "d" );

	return $clipboardText;

Python Win 32 Extensions for Maya

When using Windows OS, there are lots of features and functions of the OS that normal applications can access, however when using Python these aren’t available. To get round this a set of extensions to Python have been released that give access to all these OS specific functions –

Different versions of Maya use different versions of Python so you need to get the right version of the extensions, 8.5 uses Python 2.4, 2008 and 2009 use Python 2.5.

The downloaded executable will try to install the extensions to a python installation, and annoyingly it doesn’t detect Maya’s copy. You can use 7zip to extract the juicy bits from the .exe file, the PLATLIB folder is the one you want. The contents need to be copied into your Maya install directory, in Python\lib\site-packages\ and also the contents of pywin32_system32 (pythoncom25.dll and pywintypes25.dll) need to be copied into bin in the Maya install folder.

Script it

To make this whole process easier for our studio I wrote a mel script that handles all this automatically; a single function that checks and installs the extensions. Any script or tool can execute this function to ensure that the extensions are installed and available to it.

You just need to edit the script so that it points to the location of your downloaded extensions, we have these on a network share so they are accessible to everyone. If you want to use them then you just need to call pyWin32Extensions() in mel at the beginning of your script, the function will also return an int to indicated the status of the extensions. ( Returns 0 if not available, 1 if available, 2 if just installed )

//check and install win32 extension
//returns 0 if not available, 1 if available, 2 if just installed.

global proc int pyWin32Extensions()
	// need to check and copy the files over
	string $dest = `getenv "MAYA_LOCATION"` + "/Python/lib/site-packages/";
	$fileNames = `getFileList -folder $dest`;
	string $fileName = "";

	for ($fileName in $fileNames)
		if ($fileName == "win32")
			//return 1 to indicated that the extensions are available.
			return 1;
		-title "Python Win32 Extensions"
		-progress 50
		-status "Installing Required Extensions";

// Enter the location of the extracted pyWin32 files here
	string $source = "D:/MayaTools";
//If you want to support multiple versions of Maya then you can alter the 
//directories in the switch/case structure to point to your folders containing
//the different versions of pywin32

	string $mayaVersion = `about -version`;
	int $invalidVersion = false;

		case "2009":
		case "2008":
		case "2008 Extension 2":
			$source = $source + "/python/2008_2009_pythonWin32Extension/*.*";
		case "8.5":
		case "8.5 Service Pack 1":
			$source = $source + "/python/85_pythonWin32Extension/*.*";
			$invalidVersion = true;
	if ( $invalidVersion )
		warning "This version of Maya is not currently supported by Win32com extension, please contact your techincal artist to see if support can be added";
		progressWindow -endProgress;
		//return 0 to indicate that the extensions are not available.
		return 0;
	$dest = substituteAllString($dest, "/", "\\");
	$source = substituteAllString($source, "/", "\\");

	string $command = "xcopy \"" + $source + "\" "+"\"" + $dest + "\" /s";
	system ($command);
	$source = $dest + "pywin32_system32\\*.*";
	$dest = `getenv "MAYA_LOCATION"` + "/bin/";
	$dest = substituteAllString($dest, "/", "\\");

	string $command = "xcopy \"" + $source + "\" "+"\"" + $dest + "\" /s";
	system ($command);
	warning "you may have to restart Maya to take the installation into effect";
	progressWindow -endProgress;
	//return 2 to indicate that the extension was installed.
	return 2;

Windows Desktop Search for Mel

When customizing the Maya interface alot of the difficulty is in trying to find which of Mayas files you actually need to alter. this involves lots of scanning the scipt output to try and find command names, and ‘whatIs’ commands to get the mel files to look at. quite often you know the name of the command but you want to find out all the places that call it, and the only way to do this is to search the contents of all the mel scripts in the Maya directory. With a small tweak Windows desktop search (and probably google desktop and others) can index the contents of Mel files, and make searching them very quick and easy.

The options for windows desktop search are in Control Panel -> Indexing Options. You need to add the Maya directory to the list of folders to index. By default it won’t index the contents of Mel files, so you also need to tell it to consider these are plain text files; in the Indexing Options window, click Advanced, and go to the File Types tab. Search the list for the mel type, or you may need to add it if it’s not there, and set it to ‘Index Properties and File Contents’.

It will take a little while to index all the new files, but once done you can search the contents of Mayas mel files instantly.

EnvBrowser – Environment Variable Browser Mel

When trying to diagnose a broken Maya one of the first things I check is that all the scripts are being loaded from the right places. We have so many custom scripts that if some of the environment variable paths are wrong, or even just in the wrong order then all sort of strangeness can happen. Normally the way to check is to use ‘getenv’ on the different variables, which spits out a single line of text containing lots of different paths which is a real pain to read through.

This little UI tool is just a nice easy way to see the contents of the different variables. The main variables are added by default, you can add more or remove them with the buttons. Selecting one from the list will show its contents below, the full single list in the text box, and seperated out in the scrollField. It seperates simply, just by using the semicolons that split the path up; if you have a variable that uses semicolons for a different purpose then it will go a bit crazy, but I’ve not had that problem yet. If you press edit, you can then alter the contents in the text box line, but this change is only for this instance of Maya.

Any suggestions or feedback, leave a comment!

Download: djb_envbrowser


Maya Paths & Customization

I’ve spent a lot of time figuring out how to customize Maya; it really has an excellent system allowing you to change almost anything. The underlying system for allowing this is based on several Path variables that Maya will check to see where it should load its functionality from. With clever use of these it’s possible to create a predictable, modular system for allowing customization from different projects, or for individual users. I’ve documented how Maya handles each of the important Path variables, and have a couple of suggestions for how to set them up.

Diagnosing Path Issues

From within Maya you can use the mel commands ‘getenv’ and ‘putenv’ to get and store environment variables. Changing the variables like this is volatile, it’s only for this instance of Maya. You can use the ‘whatIs’ command to query where a specific script or command is being run from.

I’ve also written a little script to give a nice UI for checking the environment variables – envBrowser


MAYA_APP_DIR  – default is `%USERPROFILE%/My Documents/maya` MAYA_LOCATION – Defaults to the directorys that Maya is being run from (eg, `%PROGRAMFILES%/Autodesk/maya<version>`) This can be changed, but must point to a directory containing a valid Maya install, if not Maya will revert back to defaults.


When trying to find a script or command Maya will search through the script path from top to bottom until it finds it, then stop. This means that scripts higher up the path will override lower ones, including any of Mayas own default scripts. So to edit an installed script you can copy it to your local My Documents script folder and it will then ignore the original.

  • Current Project
    • The default if you haven’t set your own is: %MAYA_APP_DIR%/projects/default. Annoyingly if you set a mel directory in the project settings then it doesn’t add it to the scripts path, and Maya won’t find your scripts.
  • User environment variables System environment variables
    • It will only look at the first one of these, then skip the rest.
  • Maya.env %MAYA_APP_DIR%/<version >/Maya.env %MAYA_APP_DIR%/Maya.env
    • It will only look at the first one of these, then skip the rest. Can only define one MAYA_SCRIPT_PATH and it will use the first it comes to within the file, any others will be ignored. If you want to add multiple directories then add them all to one line, using semi-colons to seperate them.
  • %MAYA_APP_DIR%/<version>/scripts
  • %MAYA_APP_DIR%/scripts
  • %MAYA_APP_DIR%/<version>/presets
  • Shelf paths
    • All locations from %MAYA_SHELF_PATH% are added here
  • %MAYA_APP_DIR%/<version>/prefs/shelves
  • %MAYA_APP_DIR%/<version>/prefs/markingMenus
  • %MAYA_LOCATION%/scripts/startup
  • %MAYA_LOCATION%/scripts/others
  • %MAYA_LOCATION%/scripts/AETemplates
  • %MAYA_LOCATION%/scripts/paintEffects
  • %MAYA_LOCATION%/scripts/fluidEffects
  • %MAYA_LOCATION%/scripts/hair
  • %MAYA_LOCATION%/scripts/cloth
  • %MAYA_LOCATION%/scripts/fur
  • Modules

userSetup.mel Maya will only run this script once, and will use the first one it comes to in the path system. To allow us to define a team userSetup but still allow a user to run their own scripts, we use a userSetup file that is provided by the team project. After completing team setup, this file will try to run ‘localSetup.mel’ which the user can customize.


Maya searches ‘MAYA_MODULE_PATH’ for module definition files; a text file that contains the name of the module and the location it is installed. For each module path found, it appends the suffixes “plug-ins”, “presets”, “scripts”, and “icons”, and adds them to MAYA_PLUG_IN_PATH, MAYA_PRESET_PATH, MAYA_SCRIPT_PATH (and PYTHONPATH), and XBMLANGPATH, respectively.

  • User environment variables System environment variables
    • It will only look at the first one of these, then skip the rest.
  • %MAYA_APP_DIR%/<version>/modules
  • %MAYA_APP_DIR%/modules
  • Maya’s doing something weird for this next set of paths, it seems it might be defined in the registry, and a newer version of maya will continue to use an older directory if it still exists. So this could be in different places on different machines.
    • %PROGRAMFILES%/Common Files/Alias Shared/Modules/maya/<version>
    • %PROGRAMFILES%/Common Files/Alias Shared/Modules/maya OR
    • %PROGRAMFILES%/Common Files/Autodesk Shared/Modules/maya/<version>
    • %PROGRAMFILES%/Common Files/Autodesk Shared/Modules/maya
  • %MAYA_LOCATION%/modules

Plug in Path – MAYA_PLUG_IN_PATH

Any directories listed here will be added to the Plugin Manager window in Maya, however, in the same manner as scripts, Maya will use the first plugin it finds in the list, and then ignore any further plugins named the same. Even when selecting to autoload a specific plugin, when restarting Maya the ‘autoload’ checkbox will move to the first plugin it finds. The only exception to this is when you load the plugin manually by clicking the ‘Loaded’ checkbox, it will correctly load the specified plugin. Also you can load a specific plugin in a script by stating the full path of the plugin to load.

  • User environment variables System environment variables
    • It will only look at the first one of these, then skip the rest.
  • %MAYA_APP_DIR%/<version>/plug-ins
  • %MAYA_APP_DIR%/plug-ins
  • %MAYA_LOCATION%/plug-ins
  • Modules


If you query this variable it doesn’t report Mayas default shelf directory, I think it only checks here: %MAYA_APP_DIR%/<version>/prefs/shelves

  • User environment variables System environment variables Maya.env
    • It will only look at the first one of these, then skip the rest.
  • Mayas own stuff


Maya searches the ‘XBMLANGPATH’ variable for icons, the variables ‘MAYA_ICON_PATH’ and ‘MAYA_FILE_ICON_PATH` are obsolete and are no longer used.

  • User environment variables System environment variables
    • It will only look at the first one of these, then skip the rest.
  • %MAYA_APP_DIR%/<version>/prefs/icons
  • %MAYA_APP_DIR%/prefs/icons
  • %MAYA_LOCATION%/icons
  • %MAYA_LOCATION%/app-defaults
  • %MAYA_LOCATION%/icons/paintEffects
  • %MAYA_LOCATION%/icons/fluidEffects
  • %MAYA_LOCATION%/icons/hair
  • %MAYA_LOCATION%/icons/cloth
  • %MAYA_LOCATION%/icons/live
  • %MAYA_LOCATION%/icons/fur
  • Modules

Python scripts – PYTHONPATH

Similar to mel scripts, Maya will search through the path until it finds what it’s looking for, then will stop. This allows you to override scripts by placing them higher up the path. Note that ‘Modules’ is above the My Docs scripts which is different than for all the other paths. Mayas default locations aren’t shown in the PYTHONPATH variable, to see the full path that python is using, query the sys.path variable in Python.

  • User environment variables System environment variables
    • It will only look at the first one of these, then skip the rest.
  • Modules
  • %MAYA_LOCATION%/bin/
  • %MAYA_LOCATION%/Python/lib
  • %MAYA_LOCATION%/Python/lib/plat-win
  • %MAYA_LOCATION%/Python/lib/lib-tk
  • %MAYA_LOCATION%//bin
  • %MAYA_LOCATION%/Python
  • %MAYA_LOCATION%/Python/lib/site-packages
  • %MAYA_APP_DIR%/<version>/prefs/scripts
  • %MAYA_APP_DIR%/<version>/scripts
  • %MAYA_APP_DIR%/scripts This is handled slightly differently than the mel equivalent, Maya will execute all files it finds in the PYTHONPATH directories, they will be ran in the order found. Also, this file is ran before Mayas UI is created, so cannot be used to access or hack any of the default Maya UI. Any print commands will be output to the ‘Output Window’ instead of the Script Editor History.

RSS Feed. Copyright © 2004–2009. All rights reserved. Admin Login