Mel Script - Using system()




Maya is rarely used on its own, there are normally a whole host of command line utilities associated with the development of 3D assets. At some point or other, we may find that we need one of those external programs to be called from inside Maya. To do this we use the system call.





Example 1 : using DIR/ls

This simple example uses system to read the contents of the current working directory. Please don't use this for reading directories though, use the portable mel command getFileList instead.



		// for windows
		string $files[] = system( "DIR" );

		// for linux
		string $files[] = system( "ls" );

		// loop through the returned files and print
		for( $file in $files )
			// print file
			print( $file + "\n" );



Example 2 : spawning GUI based App's

A big problem with system is that mel will wait for it to finish execution of the command before control will be returned to the script. This is fine for simple programs such as ls or pwd where we actually want the returned results. However an application with a GUI normally doesn't terminate for a long time after the initial system call was made.

In effect, we need to spawn GUI based App's in the background so that our script can continue running. Under linux we need to redirect the output of the application (normally to dev/null). Under Windows you need to use either shell or start before the executable name.



		// spawning fcheck as a background process under WIN32
		system("start C:/aw/Maya/bin/fcheck.exe " + $filename);

		// spawning fcheck as a background process under linux
		system("fcheck " + $filename + " >/dev/null 2>&1 &");