PDA

View Full Version : Newbie MEL Script


vvmanoj
05-18-2006, 09:08 AM
Hi all,
I have just started to delve into MEL. I have written a small script to automate a process I do everyday at work. Basically take a snapshot of my work and put it into my project directory ....



// --- Version 1.12 beta ---

if (`window -ex snapshot`)
{
deleteUI snapshot;
}

setAttr defaultRenderGlobals.imageFormat 8;
string $window = `window snapshot`;
paneLayout;
popupMenu -button 2;
menuItem -label "About" -command "aboutsnap";
//menuItem -label "Exit" -command "exit";
menuItem -divider 1;
menuItem -label "Wireframe" -command "wi";
menuItem -label "Smooth Shaded" -command "smooth";
menuItem -label "Toggle Wireframe on Shaded" -command "wos";
menuItem -label "Toggle Xray" -command "xray";
menuItem -divider 1;
menuItem -label "Toggle Grid" -command "gri";
menuItem -label "Toggle Cameras" -command "cam";
menuItem -divider 1;
menuItem -label "210 x 150" -command "small";
menuItem -label "320 x 240" -command "med";
menuItem -label "640 x 480" -command "large";
menuItem -divider 1;
menuItem -label "Take Snapshot" -command "takesnap";
string $manoj = ($manoj+"a");
modelPanel -label $manoj -mbv 0;
// Variable is a work around. Keeps the appending a to the label name.
window -e -title "Snapshot 1.12 beta" -sizeable 0 -tb 0 $window;
showWindow $window;
modelEditor -e -grid 0 -wos 1 -displayAppearance "smoothShaded" -ca 0 `getPanel -withLabel $manoj`;
window -e -widthHeight 324 244 $window;

proc wos()
{
string $mycheck = `getPanel -withFocus`;
int $mycheck2 = `modelEditor -q -wos $mycheck`;
if ($mycheck2 == 1){
modelEditor -e -wos 0 $mycheck;
} else {
modelEditor -e -wos 1 $mycheck;
}
}


proc wi()
{
modelEditor -e -wos 0 -xray 0 -displayAppearance "wireframe" -ca 0 `getPanel -withFocus`;
}

proc xray()
{
string $mycheck = `getPanel -withFocus`;
int $mycheck2 = `modelEditor -q -xray $mycheck`;
if ($mycheck2 == 1){
modelEditor -e -xray 0 $mycheck;
} else {
modelEditor -e -xray 1 $mycheck;
}
}

proc smooth()
{
modelEditor -e -wos 0 -xray 0 -displayAppearance "smoothShaded" -ca 0 `getPanel -withFocus`;
}

proc gri()
{
string $mycheck = `getPanel -withFocus`;
int $mycheck2 = `modelEditor -q -grid $mycheck`;
if ($mycheck2 == 1){
modelEditor -e -grid 0 $mycheck;
} else {
modelEditor -e -grid 1 $mycheck;
}
}

proc cam()
{
string $mycheck = `getPanel -withFocus`;
int $mycheck2 = `modelEditor -q -ca $mycheck`;
if ($mycheck2 == 1){
modelEditor -e -ca 0 $mycheck;
} else {
modelEditor -e -ca 1 $mycheck;
}
}

proc small()
{
window -e -widthHeight 214 154 -title "Snapshot 1.01 beta" -sizeable 0 -tb 0 snapshot;
}

proc med()
{
window -e -widthHeight 324 244 -title "Snapshot 1.01 beta" -sizeable 0 -tb 0 snapshot;
}

proc large()
{
window -e -widthHeight 644 484 -title "Snapshot 1.01 beta" -sizeable 0 -tb 0 snapshot;
}

proc takesnap()
{
string $myfile = `file -q -sn -shn`;
playblast -frame 1 -format image -percent 100 -viewer 0 -f $myfile -fo;
}

//proc exit()
//{
//deleteUI snapshot;
//}

proc aboutsnap()
{
confirmDialog
-title "About Snapshot"
-message "Snapshot. A MEL Script to take snapshot of your viewport. \n\n Ver 1.12 Beta Build date 17.05.2006 \n MEL script by Vangala Manoj."
-button "OK"
-defaultButton "OK";
}


I have run into the following issues....
1. My modelEditor will not start with the specified settings. I can always change the settings with the menu but I cant seem to understand why it will not start with my preferred settings.

2. The exit process (currently commented out) exits Maya and not the window.

I am running Maya under Linux.

Thanks in advance!
Manoj

Stucky
05-18-2006, 10:33 AM
hi dude. I've tested your script. I have 3 problems.

First is that I dont see any "exit" menu to close the window. The only options I see, is when I press MMB inside your window. Is this normal? I tought this was a problem of me using Linux, but once you are using Linux too....

Second is that when I run the command "deleteUI snapshot;" it only closes your window, not maya. But you can try "deleteUI -window snapshot;"

Thrid is: what do you mean by your preferred settings? for me it all seems ok...


EDIT: I've notice that in you "exit" proc, before the command to delete your window, you have "//", and the command in front of this, will not run...This may be what is causing you trouble...


-S

vvmanoj
05-18-2006, 10:54 AM
Thanks for the reply....
Oops I forgot to mention the menu is accessed my the middle mouse button.
Coming to closing the window....currently you need to hit Alt+F4 the reason is the exit script was closing Maya and that is also the reason for the // before the exit procedure. It is now fixed...thanks for the `deleteUI -window snapshot` tip!

What I mean by preferred settings is
WireframeonShaded and grid off....
but on some files it opens in wireframe mode and grid on. On other files it works sweet.
Cheers,
Manoj

EDIT:
Just to update the deleteUI -window snapshot does not seem to be working again. It closes Maya again....:eek:

Cheers,
Manoj

Stucky
05-18-2006, 11:06 AM
but how do you run the exit proc? Or you just run the command:


deleteUI snapshot;

?????



I'm not understanding what you are doing wrong. Because I started your script, and then I ran the command above, and only your window closed, not Maya...



-S

vvmanoj
05-18-2006, 11:28 AM
I use `deleteUI snapshot` to close the window. I basically uncomment the parts of the code that are currently disabled.

What version of Maya are you running? I am running Maya 7.0.1

Cheers,
Manoj

Stucky
05-18-2006, 11:30 AM
I'm using Maya 7.0.1 too. There is no way that in running that command it closes the Maya main window. I don't know why...

EDIT: just one more tip. Try to run this:


deleteUI -window $window;

this is the same as deleting the snapshot, but done through the string..

-S

vvmanoj
05-18-2006, 11:39 AM
Guess what....when I put $window in I get an error stating that $window is an undeclared variable! I just cannot understand what I am doing wrong.....
Do I need to set the variable to global?
Cheers,
Manoj

Stucky
05-18-2006, 11:53 AM
dude. I remembered one thing. Perhaps the exit proc is messing with something and exits Maya. Try to change the name of the proc to something like "closeWindow" or something..and use

deleteUI -window snapshot;

instead of:

deleteUI -window $window;



-S

vvmanoj
05-18-2006, 12:20 PM
I tried it....but the same problem. It closes Maya....the point is it work What flavor of Linux are you running? I am on Suse 9.3
Thanks for all the help...
Cheers,
Manoj

Mikademius
05-18-2006, 12:30 PM
Hi there :)

I'd be VERY CAREFUL using an already built in Maya command as a name in a script. You're guaranteed to crash your script sometime...



snapshot [flags] [objects]

This command can be used to create either a series of
surfaces evaluated at the time specified by the command
flags, or a motion trail displaying the trajectory of the object's
pivot point at the times specified.



The snapshots go directly to the project folder, but if this is something you do everyday as you say you should make the script create folders with date (061805/year month day- syntax to get it sorted correctly ) and put the files there. And why tiffs?


I also have a couple of tips for you to shorten your code:
From this:


if (`window -ex snapshot`)
{
deleteUI snapshot;
}


To this:( I also changed the name of your Win)


if (`window -ex snapshot`)deleteUI MA_mySnapShotWindow;


Because there is only ONE line in this if statement you don't have to include "{}"

Here is another one also dealing with ifs:
From this:


string $mycheck = `getPanel -withFocus`;
int $mycheck2 = `modelEditor -q -wos $mycheck`;
if ($mycheck2 == 1){
modelEditor -e -wos 0 $mycheck;
} else {
modelEditor -e -wos 1 $mycheck;
}




To this:



string $mycheck = `getPanel -withFocus`;
modelEditor -e -wos (!`modelEditor -q -wos $mycheck`) $mycheck;



This way you create a toggle without even using an if statement. You simply tell Maya to set the modelEditor -wos to whatever it is NOT at the time. This is a good way to create toggle and you also got FIVE lines less code!!

You can also use the same toggle on several other procs here.

The reason the exitProc isn't working is because of the name. Do it like this instead

deleteUI MA_mySnapShotWindow;


In general you should get used to creating variables with unique names from the beginning.
I use my initials MA , underScore and then name... Also stay away from the letter "g" in the beginning of procs and variables. Maya uses this letter to define globals...

vvmanoj
05-18-2006, 01:11 PM
Hi Mikademius,
Thanks for the great tips dude! The proc script shortening was great and works sweet!

string $mycheck = `getPanel -withFocus`;
modelEditor -e -wos (!`modelEditor -q -wos $mycheck`) $mycheck;

But when I exit.....Maya still shuts down! This is the reworked code......


// --- Version 1.12 beta ---

if (`window -ex mysnapshot`) deleteUI MA_mysnapshot;

setAttr defaultRenderGlobals.imageFormat 8;
string $MA_mywindow = `window MA_mysnapshot`;
paneLayout;
popupMenu -button 2;
menuItem -label "About" -command "MA_myaboutsnap";
menuItem -label "Exit" -command "MA_myclosewindow";
menuItem -divider 1;
menuItem -label "Wireframe" -command "MA_mywi";
menuItem -label "Smooth Shaded" -command "MA_mysmooth";
menuItem -label "Toggle Wireframe on Shaded" -command "MA_mywos";
menuItem -label "Toggle Xray" -command "MA_myxray";
menuItem -divider 1;
menuItem -label "Toggle Grid" -command "MA_mygri";
menuItem -label "Toggle Cameras" -command "MA_mycam";
menuItem -divider 1;
menuItem -label "210 x 150" -command "MA_mysmall";
menuItem -label "320 x 240" -command "MA_mymed";
menuItem -label "640 x 480" -command "MA_large";
menuItem -divider 1;
menuItem -label "Take Snapshot" -command "MA_mytakesnap";
string $MA_mymanoj = ($MA_mymanoj+"a");
modelPanel -label $MA_mymanoj -mbv 0;
// Variable is a work around. Keeps the appending a to the label name.
window -e -title "Snapshot 1.12 beta" -sizeable 0 -tb 0 $MA_mywindow;
showWindow $MA_mywindow;
modelEditor -e -grid 0 -wos 1 -displayAppearance "smoothShaded" -ca 0 `getPanel -withLabel $MA_mymanoj`;
window -e -widthHeight 324 244 $MA_mywindow;

proc MA_mywos()
{
string $MA_mycheck = `getPanel -withFocus`;
modelEditor -e -wos (!`modelEditor -q -wos $MA_mycheck`) $MA_mycheck;
}


proc MA_mywi()
{
modelEditor -e -wos 0 -xray 0 -displayAppearance "wireframe" -ca 0 `getPanel -withFocus`;
}

proc MA_myxray()
{
string $mycheck = `getPanel -withFocus`;
modelEditor -e -xray (!`modelEditor -q -xray $mycheck`) $mycheck;
}

proc MA_mysmooth()
{
modelEditor -e -wos 0 -xray 0 -displayAppearance "smoothShaded" -ca 0 `getPanel -withFocus`;
}

proc MA_mygri()
{
string $mycheck = `getPanel -withFocus`;
modelEditor -e -grid (!`modelEditor -q -grid $mycheck`) $mycheck;
}

proc MA_mycam()
{
string $mycheck = `getPanel -withFocus`;
modelEditor -e -ca (!`modelEditor -q -ca $mycheck`) $mycheck;
}

proc MA_mysmall()
{
window -e -widthHeight 214 154 -title "Snapshot 1.01 beta" -sizeable 0 -tb 0 snapshot;
}

proc MA_mymed()
{
window -e -widthHeight 324 244 -title "Snapshot 1.01 beta" -sizeable 0 -tb 0 snapshot;
}

proc MA_mylarge()
{
window -e -widthHeight 644 484 -title "Snapshot 1.01 beta" -sizeable 0 -tb 0 snapshot;
}

proc MA_mytakesnap()
{
string $MA_myfile = `file -q -sn -shn`;
playblast -frame 1 -format image -percent 100 -f $MA_myfile -fo;
}

proc MA_myclosewindow()
{
deleteUI -window MA_mysnapshot;
}

proc MA_myaboutsnap()
{
confirmDialog
-title "About Snapshot"
-message "Snapshot. A MEL Script to take snapshot of your viewport. \n\n Ver 1.12 Beta Build date 17.05.2006 \n MEL script by Vangala Manoj."
-button "OK"
-defaultButton "OK";
}



It also outputs jpgs and not tiffs.


setAttr defaultRenderGlobals.imageFormat 8;


Thanks for your time!
Cheers,
Manoj

Stucky
05-18-2006, 01:46 PM
Hey Mikademius. Thanks for jumping in. Do you have a clue why maya closes when he tries to exit the snapshot window? because I've tested the new code that vvmanoj provided, and my Maya does not close, only the snapshot windows closes....



vvmanoj: I am using Suse 10.0, but I think this is not an OS problem...



-S

darkjedi1929
05-18-2006, 01:48 PM
Dude, I just checked out your script, works like a charm. Even the exit command. Only crit I have is, don't you think you have to many procs? For most of your scenario, a simple switch : case statement would suffice...

Mikademius
05-18-2006, 02:11 PM
Hi Mikademius,
Thanks for the great tips dude! The proc script shortening was great and works sweet!


Shortening is important to take workload off Maya's back.

About the crash when exiting, try:

deleteUI MA_mysnapshot;


and leave the -win flag out. Shouldn't be neseccary, but try it. It works here.


setAttr defaultRenderGlobals.imageFormat 8;


I'm gonna let you in on a little secret :) This doesn't work if you're in mentalRay mode. What you have to do is actually make sure you go into software mode, set the image format there, go back to MR, set it to 8 there too and then it will output jpg...

Again, to shorten some more code and release some more memory you can give your proc some arguments. Example:



proc MA_myScreenSize (int $MA_screenWidth, int $MA_screenHeight){
window -e -widthHeight $MA_screenWidth $MA_screenHeight -t "Snapshot 1.01 beta" -sizeable 0 -tb 0 MA_mysnapshot;
}



This way to use this later in a command you use these commands:



MA_myScreenSize 640 320




which will set 640*320. To use this as a command for 320*240 you use


MA_myScreenSize 320 240


I also noticed that you haven't fixed all your names. Go through it once more and check it.

Another thing; when naming you should name it like this:
myNewFunkyMobilePhone (example) Se how I capitalize the first letter in the new words?
What is easier to read?

MA_myscrolllayout or MA_myScrollLayout

also get used to writing like this because all MEL commands are written like this.

I cover most of this here:
http://www.final3.com/html/tutorials/mel_ShapeEditor_01.htm

Stucky: Also answered your question in this post :)

vvmanoj
05-19-2006, 04:51 AM
Hi,
Thanks for that Mikademius. I tried the window thingy but still no luck dude. But the funny thing is it works sweet on a Windows machine.I will implement the naming convention in future scripts.
Jedi...will look into what you say...
Cheers,
Manoj

CGTalk Moderation
05-19-2006, 04:51 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.