Mel Script - Script Jobs [API]




A script job allows us to call a specific bit of code whenever a specific event occurs within maya. The full list of all the event available within maya is available if you type the mel command

scriptJob -listEvents

For a complete list with descriptions of what each one is, look in the mel command documentation for scriptJob.





Example 1 : A script job on File New

Here we can use script job to assign our custom function to be executed whenever all the objects in the scene are deleted (ie File->New). Our command then simply creates a sphere everytime a new scene is created.



		// a function to call when a File->New occurs
		proc OnFileNew()
			// create a sphere in all new scenes
		// create the script Job for the deleteAll event
		scriptJob -event "deleteAll" "OnFileNew();";



Example 2 : A script job on Playback state Change

This example sets up a script job to create and destroy a window everytime the animation is played or stopped.



		global string $g_MyWinName="";
		// a function to call when a File->New occurs
		proc OnPlaybackChanged()
			// required to get access to the global 
			// variable inside the procedure
			global string $g_MyWinName;
			// if no window exists create it, otherwise delete it
			if($g_MyWinName=="") {

				// create a window when play is pressed
				$g_MyWinName = `window`;
						button -label "!! MOOO !!";

				showWindow $g_MyWinName;
			else {
				deleteUI $g_MyWinName;
				$g_MyWinName ="";

		// create the script Jobs for the playingBack condition
		scriptJob -conditionChange "playingBack" "OnPlaybackChanged";