[Closed] Error handling and "pass-through"
Hello
Well, here is my problem:
I’ve made a script to apply properties to objects. So it has checkboxes to set, for example, if an object is visible to camera or if it casts shadows. The problem is that if no object is selected, the script fails with this kind of error message :”>> MAXScript Rollout Handler Exception: – Unknown property: “renderable” in undefined <<“
I can understand this … but my question is how would you add some code to make a messagebox popping up, asking something like “Please select at least on object”, then you can hit ok, and be able to select an object without script failure … for now, after I hit ok in the messageBox, I still get the error … any idea, please ?
Thanks in advance !
NicolasC
Something like this:
on checkbox chaged do
(
if selection != undefined do
(
YOUR CODE HERE
)
else
(
Messagebox "Please select at least one object!"
)
)
This kind of exception handling is exactly what the try … catch construct is for. Check out “Try Expression” in the docs.
EDIT: oops, duped
Thank you all for your answers.
In fact, I already tried using try … catch construction, but without success so far. Maybe did I implement it the wrong way. I’ve tried Piflik’s suggestion, and it worked
On another side, I got the help of Matt Clementson, who gave me this other possibility:
fn checkSelection =
(
local b_return_value = true
if selection.count == 0 then
(
messageBox "Please select at least one object !"
b_return_value = false
)
return b_return_value
)
Then I only need to add
if checkSelection() then ...
Just to let you know …
Thanks again !
NicolasC
yes thats true and with a variable to return much better
but MS allways returns the last caculated “vale” in a expression so you can leave return away and just put the variable there (faster)
but in a checkFunction i woudn’t put a messagebox
put it in the if/then construct when it evaluates false (imo)
or try (to do something) catch( messagebox “and catch it”)
For the record I’m pretty sure a Return at the end of a function is ignored per Bobo’s maxscript comments.
So Return will be slow in the middle of a function but if you put it at the end it won’t actually have a performance hit.
…and my info comes straight from Larry Minton, so it must be true.
But it is much shorter to say
fn checkSelection =
(
if selection.count == 0 then (
messageBox "Please select at least one object !"
false
) else true
)
or if no messagebox is needed, just
fn checkSelection =
(
selection.count != 0
)
which of course could be just written inline wherever the function call would have been.
I usually prefer:
if selection[1] != undefined then ...
then I can at least assume there’s one ore more objects in the selectionset
and it easy for shortening the selection further downstream
if (local theObj = selection[1]) != undefined then
print theObj.name
I don’t know if it’s good practice, but I like to work as compact as possible.
-Johan
Hello,
I must admitt I had rarely posted here in the past, and I’m very pleased to see how you guys are very reactive on this forum. Thanks a lot for all your advices and tricks !
Best regards.