+ Chapter - 10 - MEL - Hello Mr. OS +
Instructor - Pradeep Mamgain
+ Overview +
system command executes commands from the shell. The output of the executed command is returned as string. This command is used to execute OS commands.
Synopsis
system <string>
Example - 10.1, opens notepad.exe when executed. Notice that MEL uses forward slashes. Example - 10.2, opens Photoshop exe. Example - 10.3, displays current directory listing.
Example - 10.1, Launching Notepad.
system("start C:/windows/notepad.exe" );
Example - 10.2, Launching Photoshop.
system ("start C:/Program Files/Adobe/Adobe Photoshop CS2/Photoshop.exe");
Example - 10.3, Dir Command.
system("dir");
Now we will look at the internalVar command and then we will create a file backup system using system and internalVar command.
+ internalVar Command +
Synopsis
internalVar [-userAppDir] [-userBitmapsDir] [-userMarkingMenuDir] [-userPrefDir] [-userPresetsDir] [-userScriptDir]
[-userShelfDir] [-userTmpDir] [-userWorkspaceDir]
This command returns the values of internl variables used by MEL. It returns string data type.
Example - 10.4, internalVar command in action.
print (`internalVar -uad` + "\n"); // Returns user application directory.
print (`internalVar -usd` + "\n"); // Returns sser scripts directory.
print (`internalVar -upd` + "\n"); //Return the user preference directory.
print (`internalVar -ups` + "\n"); //Return the user presets directory.
print (`internalVar -ush` + "\n"); //Return the user shelves directory.
print (`internalVar -umm` + "\n"); //Return the user marking menu directory.
print (`internalVar -ubd` + "\n"); //Return the user bitmaps prefs directory.
print (`internalVar -utd` + "\n"); //Return a temp directory. Will check for TMPDIR environment variable, otherwise will return the current directory.
print (`internalVar -uwd` + "\n"); // Return the user workspace or projects directory.
When Example - 10.4 executes, displays following result in my script editor.
C:/Documents and Settings/Pradeep Mamgain/My Documents/maya/
C:/Documents and Settings/Pradeep Mamgain/My Documents/maya/2008/scripts/
C:/Documents and Settings/Pradeep Mamgain/My Documents/maya/2008/prefs/
C:/Documents and Settings/Pradeep Mamgain/My Documents/maya/2008/presets/
C:/Documents and Settings/Pradeep Mamgain/My Documents/maya/2008/prefs/shelves/
C:/Documents and Settings/Pradeep Mamgain/My Documents/maya/2008/prefs/markingMenus/
C:/Documents and Settings/Pradeep Mamgain/My Documents/maya/2008/prefs/icons/
C:/DOCUME~1/PRADEE~1/LOCALS~1/Temp/
C:/Documents and Settings/Pradeep Mamgain/My Documents/maya/projects/
+ Hands On : Creating a scene file backup system +
In this hands on execercise, we will use system command to launch wzzip, a winzip addon, a file compressor utility that runs from a command line. Using this utility we will compress files that are inside our default project. The exact syntax for wzzip that we will use is as shown in Example 10.5.
Example - 10.5 - wzzip command line syntax.
wzzip -rpex c:/myProjectBackup.zip "C:/Documents and Settings/user/My Documents/maya/projects/*.*"
As you can see from Example - 10.5, window likes the path inside the quotes, also shown in Figure - 10.1, with yellow highlights. We will use special string code \"; to add a quotation mark in the string.
Figure - 10.1 - Windows likes path in quotes, if folder name contains a space.
Example - 10.6, Script in Action.
//Variable declaration, it contains string for the system command
string $systemStr;
// Output > -rpex c:/myProjectBackup.zip "
$systemStr = " -rpex c:/myProjectBackup.zip \"";
// internalVar -uwd command is used to fetch the default workspace or project path
//output > C:/Documents and Settings/user/My Documents/maya/projects/
$systemStr = $systemStr + `internalVar -uwd`;
//Wild card *.* is added to include all the files inside of project folder, followed by a quotaion mark.
//output > *.* "
$systemStr = $systemStr + "*.*\"";
//system command is used to open the wzzip and execute the string generated above.
system ("start C:/Program Files/WinZip/wzzip " + $systemStr);
One you execute the Example - 10.6, MEL will compress all files inside project folder and create a zip file in C drive of your hard drive. If you want a incremental compressed file save, you can do this by declaring a integer data type and then increment its value by 1, as shown in Example - 10.7.
Example 10.7 - Incremental Save.
//Variable declaration, it contains string for the system command
string $systemStr;
//Variable declaration, it contains incremental value for file name.
int $counter;
//Increase counter by one.
$counter++;
// Output > -rpex c:/myProjectBackup[counter].zip "
$systemStr = " -rpex c:/myProjectBackup" + $counter + ".zip \"";
// internalVar -uwd command is used to fetch the default workspace or project path
//output > C:/Documents and Settings/user/My Documents/maya/projects/
$systemStr = $systemStr + `internalVar -uwd`;
//Wild card *.* is added to include all the files inside of project folder, followed by a quotation mark.
//output > *.* "
$systemStr = $systemStr + "*.*\"";
//system command is used to open the wzzip and execute the string generated above.
system ("start C:/Program Files/WinZip/wzzip " + $systemStr);
//Print the custom message.
print ("\n" + "File myProjectBackup" + $counter + ".zip created ");
Example 10.7, works fine as long as you have active maya session, once you close Maya and re-open it and execute the script again; value for $counter will be again 1. So wzzip will update the myProjectBackup1.zip, instead of creating a new backup using the filename, myProjectBackup[last seq no +1].zip. To overcome this problem, we will create a loop that will loop from 1 to infinity. Then using filetest command, we will check whether file exists on hard drive or not. If it exists we will break the loop and create a new file. Study the Example 10.8 and execute it. If you have any queries feel free to ask in the forums.
Example 10.8 - Automatic Seq Increment.
//Variable, it contains string for the system command
string $systemStr;
//Variable, it is used to check the last saved file seq number. If found, its value is incremented by one, then it acts as sequence number for next file to be saved.
int $fileCounterCheck=0;
//Variable, It contains value, $fileCounterCheck+1
int $counter;
//Variable, used to check the name of the last saved file.
string $fileName;
//Variable, used to check whether file exits on hard drive in specified folder.
int $fileExists;
while ($fileCounterCheck >=0 )
{
//Name of the first file would be myProjectBackup1.zip
$fileCounterCheck++;
//File Name String
$fileName="c:/myProjectBackup"+$fileCounterCheck+".zip";
//Check whether file exists
$fileExists=`filetest -r $fileName`;
//If file does not exists, break the loop and create it, else loop.
if ($fileExists==0)
{
$counter=$fileCounterCheck;
break;
}
}
// Output > -rpex c:/myProjectBackup[counter].zip "
$systemStr = " -rpex c:/myProjectBackup" + $counter + ".zip \"";
// internalVar -uwd command is used to fetch the default workspace or project path
//output > C:/Documents and Settings/user/My Documents/maya/projects/
$systemStr = $systemStr + `internalVar -uwd`;
//Wild card *.* is added to include all the files inside of project folder, followed by a quotation mark.
//output > *.* "
$systemStr = $systemStr + "*.*\"";
//system command is used to open the wzzip and execute the string generated above.
system ("start C:/Program Files/WinZip/wzzip " + $systemStr);
//Print the custom message.
print ("\n" + "File myProjectBackup" + $counter + ".zip created ");
+ What Next ? +
Now you can make a MEL button for you in the shelf and execute this script whenever you need to take the compressed backup. In Next chapter we will explore the buttonManip command.
+ Download Project +
Dear guest, Only registered members can download projects/footages/scripts and Video files.Kindly get registered to download the files. It will also help us in informing you that when new tutorial/article/resource or cg news is available. Once you get registered downlaod link will appear here. -
Downloaded
times.
+ Save / Promote This Article/Tutorial/Information: +
If you enjoyed this article your vote is always highly appreciated.+ Other Info +
+ Discuss in Forums