title: How to abort the loading of a file in Maya started via cmds.file in python
body: <p>I have code that runs a batch operation on files in Maya. The actual implementation isn't important, just know that it gets fed a list of filepaths, maya opens the files, then an operation is performed on each file in turn.</p>
  
  <p><strong>In cases where a reference isn't valid for whatever reason (like an invalid path) I want to abort loading of that whole scene and skip to the next in the batch list.</strong></p>
  
  <p>Looking at other questions on this site and elsewhere I've only been able to see users asking how to query references. I already have a reasonable function for doing so, it's what to do after that function returns an invalid reference path that stumps me.</p>
  
  <p>Resolving invalid reference paths has historically been done manually via the popup, however in large batches constantly babysitting the maya instance isn't feasible. Nor is suppressing the popup window itself, as I believe that will still open the file, and run the batch operation on the scene while it's in an invalid state.</p>
  
  <p>I have been through <a href="https://download.autodesk.com/us/maya/2009help/Commands/file.html" rel="nofollow noreferrer">the maya <code>cmds</code> python module</a> to try and not load references, however when using the <code>loadReferenceDepth</code> flag along with <code>cmds.file</code> I'm still getting a popup:</p>
  
  <pre><code>cmds.file(r'c:\path\to\file.ma', o=1, f=1, lrd='none')  #invalid ref popup during file cmd
  </code></pre>
  
  <p>The second approach was to look into the <a href="https://help.autodesk.com/view/MAYAUL/2017/ENU/?guid=__files_Maya_Python_API_Using_the_Maya_Python_API_htm" rel="nofollow noreferrer">maya open api</a> and register a callback on the before open event. The code below is functionally the same as how the batch is set up:</p>
  
  <pre><code>import maya.OpenMaya as OpenM
  
  batchFileList = [r"c:\path\to\file.ma", r"c:\path\to\file2.ma"]
  
  def _test_raise_exception(arg0, arg1, arg2):
      #pretending that a file ref failed below. Ref path validation code would go here.
      print '\n'*5+'Test Logging'+'\n'*5
      return False
  
  cId = OpenM.MSceneMessage.addCheckFileCallback(OpenM.MSceneMessage.kBeforeOpenCheck, _test_raise_exception)
  
  for file_ in batchFileList:
      try:
          rv = cmds.file(file_, o=1)
          #do stuff to the maya scene here
      except:
          #something didn't validate on load, except and skip, logging the occurrence.
          print 'load cancelled'
  
  OpenM.MSceneMessage.removeCallback(cId)
  </code></pre>
  
  <p>However, even though the <a href="https://help.autodesk.com/view/MAYAUL/2018/ENU/?guid=__py_ref_class_open_maya_1_1_m_scene_message_html" rel="nofollow noreferrer"><code>addCheckFileCallback</code> indicates that if the callback function returns <code>False</code> the operation is aborted</a>, the file is loaded anyway.</p>
  
  <p>Likewise, replacing the <code>return False</code> for a <code>raise RuntimeError</code> doesn't let me catch the exception. Instead the <code>cmds.file</code> completes and only prints out a small message in the log saying "python callback failed". <a href="https://help.autodesk.com/view/MAYAUL/2017/ENU/?guid=__files_Maya_Python_API_Differences_between_the_C_Maya_API_and_Maya_Python_API_htm" rel="nofollow noreferrer">The python open api docs say that the bindings prefer exceptions over MStatus return codes though, so I would've expected that to work.</a></p>
  
  <blockquote>
    <p>We have removed the MStatus class. Python exceptions must be used
    instead of MStatus.</p>
  </blockquote>
  
  <p>Am I missing something here? There must be a way to do this. Building up a very crude .ma parser is another option but that would mean dropping support for .mb files, which I don't want to do.</p>
  
  <p>Thanks for your time!</p>