How to make function return true or false

Hi all, I am still pretty weak in ‘integrating’ variables into functions here and there, and I do need some advices to ensure that I am at the very least in the right track.

I am trying to write a function where it checks whether a particular pass exisits and if it is not. If it exists, it will continue to the next function, else it will prompt a warning and do a sys.exit()
As such, I was wondering if it is indeed a good idea to use sys.exit(), cause it feels as though the execution is bailing out in the middle of the script. Are there other methods that I can try it out? And also, will it be possible to do the exit decision within the main function instead of withing the chk_pass function (just fear that having the exit to be in the main function may affects the other functions within)?

Following is the code I have used, can someone advice me?


def chk_pass():    
    if cmds.objExists == "renderResPass":
        print "Object Exists. Continue on to next process"
    else:
        cmds.warning("Create a Beauty Pass and relink the shaders before moving on.")
        sys.exit()
            
def main():

    cur_pass()
    
    # All other functions will be added here
    ..
    ..
    
    print "Rendering is complete"

you could just have the chk_pass return False in the else statement, and have main do something if it returns False.

Really don’t know what your are trying to accomplish since i never needed to use sys.exit() in a maya script mostly only use that for command line stuff with python.

I wouldn’t use sys.exit() in a called function normally. As passerby says, have the called function return true or false to indicate success. Alternatively, you can look at raising your own exceptions (errors) and then dealing with those in the calling function. This is probably more advanced that what you’re trying to do right now.


def cur_pass():    
    if cmds.objExists == "renderResPass":
        print "Object Exists. Continue on to next process"
        return True
    else:
        cmds.warning("Create a Beauty Pass and relink the shaders before moving on.")
        return False
            
def main():
    if cur_pass():
        # All other functions will be added here
        ..
        ..
    
    print "Rendering is complete"

[QUOTE=passerby;24949]you could just have the chk_pass return False in the else statement, and have main do something if it returns False.

Really don’t know what your are trying to accomplish since i never needed to use sys.exit() in a maya script mostly only use that for command line stuff with python.[/QUOTE]

Hi passerby, pardon me that I am not phrasing it well enough but that is indeed what I am trying to accomplish. The reason I use sys.exit() is because I thought that this command is pretty straightforward though it seems to have some setbacks?

[QUOTE=btribble;24953]I wouldn’t use sys.exit() in a called function normally. As passerby says, have the called function return true or false to indicate success. Alternatively, you can look at raising your own exceptions (errors) and then dealing with those in the calling function. This is probably more advanced that what you’re trying to do right now.


def cur_pass():    
    if cmds.objExists == "renderResPass":
        print "Object Exists. Continue on to next process"
        return True
    else:
        cmds.warning("Create a Beauty Pass and relink the shaders before moving on.")
        return False
            
def main():
    if cur_pass():
        # All other functions will be added here
        ..
        ..
    
    print "Rendering is complete"

[/QUOTE]

Hi btribble, thanks for the code. Just wondering under what circumstances can sys.exit() be applied in?

sys.exit is mainly used in a script run directly from the command line or by a batch script. It’s used to signal the larger environment that the script completed correctly or not. It’s not usually used in defs that are called by other python functions.

Hi Theodox, noted with thanks!