Mel Script - Script Nodes




In maya there is a script node which can be used to store and execute mel scripts within the Maya scene. These scripts can either be called manually, or on specific key events such as file open/close, render start, render frame start etc.

Each script node can contain two scripts held in the "before" and "after" attributes of the node. This allows you to create a script to be run before a render to set up any default settings etc, then at the end you can process the data. In the case of file load, the before script is called when the file is opened, the after script is called when the file closes.

Note: script nodes are not the same thing as expressions. Expressions should be used when controlling objects within your scene. Script nodes are more useful to help impliment features for a tool rather than manipulate key objects within the scene.





Example 1 : Executing a Script on demand

You can create a script node specifically for the



		// if the option doesn't exist, create it
		if( !`optionVar -exists "myName"` )
			// add a string option called "myName" and set its
			// value to "Rob"
			optionVar -stringValue "myName" "Rob";

		// use the -query flag to retrieve the option value
		print( `optionVar -query "myName"` + "\n" );

		// remove the option
		optionVar -remove "myName";



Example 2 : Executing a Script on File Open

Again using the script node we are able to execute a command whenever the file is opened or closed. One use of this is to re-define global variables for any expressions or script nodes that need them. By simply moving the data declarations from your scripts into a script node you are able to re-define the data without need to manually re-run a script.