PDA

View Full Version : try catch


safakoner
10-28-2003, 12:26 PM
I use simple code for errors in 3d studio max like this;

---------
on this_button pressed do

try
(
cs = $
cs.smoothlevel = 2
)
catch messagebox "select an object"
---------

How can I write similar code in Maya ?

GDC
10-28-2003, 05:09 PM
I don't know max's scripting language, but it looks like you are testing whether there is a selection or not and then prompting the user. There are many ways to do this with MEL.
Here's one way:

// check for a selection
if(size(`ls -sl`)) == 0){
confirmDialog -message "Select an object" -button "ok";
}



--g


Originally posted by equinox
I use simple code for errors in 3d studio max like this;

---------
on this_button pressed do

try
(
cs = $
cs.smoothlevel = 2
)
catch messagebox "select an object"
---------

How can I write similar code in Maya ?

mhovland
10-28-2003, 07:25 PM
Look up catch in the MEL docs.

From the docs:

catch ( <Mel expression> )



catch will return 1 if the expression in parenthesis produces an error at runtime and 0 otherwise.

This expression is designed to allow users to check for runtime failure and recover, if desired. The use of catch stops a runtime error from being propagated back up through the execution path of the script. Normally, a runtime error results in the termination of the execution of a script or procedure.

safakoner
10-28-2003, 09:06 PM
thanks.

I am trying a code like this. But probably another object selected in the scene and my script cannot go on.

string $sel[] = `ls -sl("SmoothObject")`;
if(`size($sel)` == 0)
error "SmoothObject not found.\n";

I want to do this;

If ( If I can found an object called "SmoothObject" in the scene )

SmoothObject.divisions = 2
commands
commands
etc.

else

error "SmoothObject not found.\n";

What do you think?

mhovland
10-28-2003, 10:26 PM
Try this out

if(catch(`select "SmoothObject"`))
print "\"SmoothObject\" not found in scene";
else
{
//your code to execute if "SmoothObject" is found in scene
}

Hope that helps.

safakoner
10-29-2003, 09:19 AM
Thank you. This is a perfect code for me. :D

thanks all. :wavey:

alexx
10-29-2003, 11:22 AM
using catch commands on stuff like that is not a really very good way of scripting at all.

you should check if that object exists using the according mel command:

objExists

cheers

alexx

safakoner
10-29-2003, 11:26 AM
hmmm, objExists is absolutely right thing for the purpose

Thanks

CGTalk Moderation
01-16-2006, 01:00 PM
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.