Question

Is there a command in Maxscript that allows you to stop a script early? Say for example the script relies on having a single object as a selection. If there's nothing to select then it won't work.

myObj = $
if ($ == undefined) then STOP
-- rest of code

where STOP represents is the elusive or fictional stop command

Rather than having to do a test like:

myObj = $
if ($ != undefined) then
(
--rest of code
)

Which is fine, but it does mean to put the majority of the script in brackets - which I find it makes the code a but uglier to edit.

Was it helpful?

Solution

It depends, you can usually use return to force an early exit. This will work even in case of event handlers as they are basically just functions:

rollout test "Test"
(
    button btn "Exception"

    on btn pressed do
    (
        if selection.count != 1 do
            return messageBox "Selection count expected to be one."
        if NOT isKindOf selection[1] Editable_poly do
            return messageBox "Object not an Editable Poly."
        btn.text = selection[1].name
    )
)
createDialog test

Due to some implementation details, if used in a body of a loop or somewhere else where it might be executed multiple times, it would make the whole script slower, though.

If you only want to exit the script and only print information in the listener, throw might be a better function to use.

    if selection.count != 1 do
        throw "Selection count expected to be one."
    if NOT isKindOf selection[1] Editable_poly do
        throw "Object not an Editable Poly"
    print selection[1].name

However in most cases what you are currently using is the most fitting solution. If you want to improve it (especially if you find yourself writing the same piece of code over and over again), you can write a custom struct similar to, for example, Filters:

    #Struct:Filters(
        GetModOrObj:<fn>; Public,
        Is_EditSpline:<fn>; Public,
        CanSwitchTo_Segment:<fn>; Public,
        HasBaseObjectProperty:<fn>; Public,
        ...
        Is_EditPolyMod:<fn>; Public,
        Is_This_EditPolyObj:<fn>; Public,
        Is_EditPatch:<fn>; Public,
        CanSwitchTo_Face:<fn>; Public)

OTHER TIPS

use exit, here's a simple example: a = 100

while True do

(

    a -= 1

    print a

    if a == 20 do

    (

        print "a is equal to 20!" 

        exit

    )

)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top