0

I would like to end an entire script once an attribute has not been found. Currently, I have the following code. I display a dialog and use sys.exit() to end the script but I m wondering if there is a cmds dialog that does this automatically for you without sys.exit(),

def check_attr(attr):
    if not cmds.select(cmds.ls(attr)):
         cmds.confirmDialog(title= 'Attribute not found   ', message = attr+' attribute was not found', button =['OK'])
         sys.exit()

My question: Does a cmds...Dialog that stops a script exist?

burned_fenrir
  • 138
  • 2
  • 13
  • do you mean like something as : cmds.error() ? And there is no dialog preset for error but you can wrap yours. – DrWeeny Apr 18 '19 at 01:28
  • 1
    also you can use PySide2 or PyQt5 in maya so you could use QMessageBox : https://stackoverflow.com/questions/40227047/python-pyqt5-how-to-show-an-error-message-with-pyqt5 – DrWeeny Apr 18 '19 at 01:35
  • @DrWeeny I mean something like cmds.exitDialog("error message ") and it just exits but I m thinking that dialogs don't have that functionality – burned_fenrir Apr 18 '19 at 16:04

1 Answers1

1

Since you're using a function the easiest way would be to use return in your if condition so that it never continues the rest of the function:

def check_attr(attr):
    if not cmds.select(cmds.ls(attr)):
         cmds.confirmDialog(title= 'Attribute not found   ', message = attr+' attribute was not found', button =['OK'])
         return

    print "Continuing script"


check_attr("someAttr")

You can also use OpenMaya.MGlobal.displayError to display it in Maya's taskbar:

import maya.OpenMaya as OpenMaya


def check_attr(attr):
    if not cmds.select(cmds.ls(attr)):
         OpenMaya.MGlobal.displayError(attr + ' attribute was not found')
         return

    print "Continuing script"


check_attr("attr")

Though be careful since OpenMaya.MGlobal.displayError simply displays an error, it doesn't stop execution like cmds.error. You could use cmds.error too, but I find that the error it spits out in the taskbar is a lot less readable.

Green Cell
  • 4,677
  • 2
  • 18
  • 49