<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Abort a cmds.file load before load completes via Open Api in Maya Programming Forum</title>
    <link>https://forums.autodesk.com/t5/maya-programming-forum/abort-a-cmds-file-load-before-load-completes-via-open-api/m-p/8044989#M9847</link>
    <description>&lt;P&gt;I should have been more precise. The documentation you linked to for the addCheckFileCallback() is for the Maya Python API 2.0.&lt;/P&gt;</description>
    <pubDate>Mon, 04 Jun 2018 14:13:38 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2018-06-04T14:13:38Z</dc:date>
    <item>
      <title>Abort a cmds.file load before load completes via Open Api</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/abort-a-cmds-file-load-before-load-completes-via-open-api/m-p/7986765#M9844</link>
      <description>&lt;P&gt;Hi all, got a question that I'm posting after not really getting to the bottom of it over on SO:&lt;BR /&gt;&lt;A href="https://stackoverflow.com/questions/50104364/how-to-abort-the-loading-of-a-file-in-maya-started-via-cmds-file-in-python" target="_blank"&gt;https://stackoverflow.com/questions/50104364/how-to-abort-the-loading-of-a-file-in-maya-started-via-cmds-file-in-python&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;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.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;I have been through&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://download.autodesk.com/us/maya/2009help/Commands/file.html" target="_blank" rel="nofollow noreferrer"&gt;the maya&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;cmds&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;python module&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;to try and not load references, however when using the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;loadReferenceDepth&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;flag along with&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;cmds.file&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;I'm still getting a popup:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class="pln"&gt;cmds&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;file&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;(&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;r&lt;/SPAN&gt;&lt;SPAN class="str"&gt;'c:\path\to\file.ma'&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; o&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;=&lt;/SPAN&gt;&lt;SPAN class="lit"&gt;1&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; f&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;=&lt;/SPAN&gt;&lt;SPAN class="lit"&gt;1&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; lrd&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;=&lt;/SPAN&gt;&lt;SPAN class="str"&gt;'none'&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;)&lt;/SPAN&gt;  &lt;SPAN class="com"&gt;#invalid ref popup during file cmd&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;The second approach was to look into the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://help.autodesk.com/view/MAYAUL/2017/ENU/?guid=__files_Maya_Python_API_Using_the_Maya_Python_API_htm" target="_blank" rel="nofollow noreferrer"&gt;maya open api&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and register a callback on the before open event. The code below is functionally the same as how the batch is set up:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class="kwd"&gt;import&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; maya&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="typ"&gt;OpenMaya&lt;/SPAN&gt; &lt;SPAN class="kwd"&gt;as&lt;/SPAN&gt; &lt;SPAN class="typ"&gt;OpenM&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;batchFileList &lt;/SPAN&gt;&lt;SPAN class="pun"&gt;=&lt;/SPAN&gt; &lt;SPAN class="pun"&gt;[&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;r&lt;/SPAN&gt;&lt;SPAN class="str"&gt;"c:\path\to\file.ma"&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; r&lt;/SPAN&gt;&lt;SPAN class="str"&gt;"c:\path\to\file2.ma"&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;]&lt;/SPAN&gt;

&lt;SPAN class="kwd"&gt;def&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; _test_raise_exception&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;(&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;arg0&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; arg1&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; arg2&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;):&lt;/SPAN&gt;
    &lt;SPAN class="com"&gt;#pretending that a file ref failed below. Ref path validation code would go here.&lt;/SPAN&gt;
    &lt;SPAN class="kwd"&gt;print&lt;/SPAN&gt; &lt;SPAN class="str"&gt;'\n'&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;*&lt;/SPAN&gt;&lt;SPAN class="lit"&gt;5&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;+&lt;/SPAN&gt;&lt;SPAN class="str"&gt;'Test Logging'&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;+&lt;/SPAN&gt;&lt;SPAN class="str"&gt;'\n'&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;*&lt;/SPAN&gt;&lt;SPAN class="lit"&gt;5&lt;/SPAN&gt;
    &lt;SPAN class="kwd"&gt;return&lt;/SPAN&gt; &lt;SPAN class="kwd"&gt;False&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;cId &lt;/SPAN&gt;&lt;SPAN class="pun"&gt;=&lt;/SPAN&gt; &lt;SPAN class="typ"&gt;OpenM&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="typ"&gt;MSceneMessage&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;addCheckFileCallback&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;(&lt;/SPAN&gt;&lt;SPAN class="typ"&gt;OpenM&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="typ"&gt;MSceneMessage&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;kBeforeOpenCheck&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; _test_raise_exception&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;)&lt;/SPAN&gt;

&lt;SPAN class="kwd"&gt;for&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; file_ &lt;/SPAN&gt;&lt;SPAN class="kwd"&gt;in&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; batchFileList&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="kwd"&gt;try&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;:&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;        &lt;BR /&gt;        rv &lt;/SPAN&gt;&lt;SPAN class="pun"&gt;=&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; cmds&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;file&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;(&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;file_&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;,&lt;/SPAN&gt;&lt;SPAN class="pln"&gt; o&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;=&lt;/SPAN&gt;&lt;SPAN class="lit"&gt;1&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;)&lt;/SPAN&gt;
        &lt;SPAN class="com"&gt;#do stuff to the maya scene here&lt;/SPAN&gt;
    &lt;SPAN class="kwd"&gt;except&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;:&lt;/SPAN&gt;
        &lt;SPAN class="com"&gt;#something didn't validate on load, except and skip, logging the occurrence.&lt;/SPAN&gt;
        &lt;SPAN class="kwd"&gt;print&lt;/SPAN&gt; &lt;SPAN class="str"&gt;'load cancelled'&lt;/SPAN&gt;

&lt;SPAN class="typ"&gt;OpenM&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="typ"&gt;MSceneMessage&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;.&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;removeCallback&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;(&lt;/SPAN&gt;&lt;SPAN class="pln"&gt;cId&lt;/SPAN&gt;&lt;SPAN class="pun"&gt;)&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, even though the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://help.autodesk.com/view/MAYAUL/2018/ENU/?guid=__py_ref_class_open_maya_1_1_m_scene_message_html" target="_blank" rel="nofollow noreferrer"&gt;addCheckFileCallback&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;indicates that if the callback function returns&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;False&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;the operation is aborted&lt;/A&gt;, the file is loaded anyway.&lt;/P&gt;&lt;P&gt;Likewise, replacing the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;return False&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;for a&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;raise RuntimeError&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;doesn't let me catch the exception. Instead the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;cmds.file&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;completes and only prints out a small message in the log saying "python callback failed".&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;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" target="_blank" rel="nofollow noreferrer"&gt;The python open api docs say that the bindings prefer exceptions over MStatus return codes though, so I would've expected that to work.&lt;/A&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;We have removed the MStatus class. Python exceptions must be used instead of MStatus.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Am I missing something here? There must be a way to do this.&amp;nbsp;Specifically I'm looking to abort a cmds.file load, not a workaround to this particular use case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your time!&lt;/P&gt;</description>
      <pubDate>Tue, 08 May 2018 10:17:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/abort-a-cmds-file-load-before-load-completes-via-open-api/m-p/7986765#M9844</guid>
      <dc:creator>mikey.n</dc:creator>
      <dc:date>2018-05-08T10:17:34Z</dc:date>
    </item>
    <item>
      <title>Re: Abort a cmds.file load before load completes via Open Api</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/abort-a-cmds-file-load-before-load-completes-via-open-api/m-p/7998792#M9845</link>
      <description>&lt;P&gt;The documentation you're referring to is for the Maya Python API 2, but the classes you're using are from the Maya Python API 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In API 1 most methods take exactly the same parameters as in C++. This means that a CheckFileCallback will be passed three parameters, the first of which is a reference to a C++ bool variable. To abort the file load you must set that variable to false, which requires using either ctypes or MScriptUtil.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In API 2 the CheckFileCallback returns True or False to indicate whether the file load should proceed. So in your example change 'import maya.OpenMaya' to 'import maya.api.OpenMaya' and remove one parameter from _test_raise_exception's parameter list and you should be good to go.&lt;/P&gt;</description>
      <pubDate>Sun, 13 May 2018 23:43:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/abort-a-cmds-file-load-before-load-completes-via-open-api/m-p/7998792#M9845</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-05-13T23:43:04Z</dc:date>
    </item>
    <item>
      <title>Re: Abort a cmds.file load before load completes via Open Api</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/abort-a-cmds-file-load-before-load-completes-via-open-api/m-p/8044683#M9846</link>
      <description>&lt;P&gt;Thanks for the insight! Funnily that bit about exceptions was pulled from the 1.0 api notes, but I couldn't find anything concrete on the 1.0 api docs themselves.&lt;BR /&gt;&lt;BR /&gt;I'll have to retest the above as we transition to 2018, baring in mind the api differences and imports. I'll report back once I've had a chance to do that.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jun 2018 12:13:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/abort-a-cmds-file-load-before-load-completes-via-open-api/m-p/8044683#M9846</guid>
      <dc:creator>mikey.n</dc:creator>
      <dc:date>2018-06-04T12:13:51Z</dc:date>
    </item>
    <item>
      <title>Re: Abort a cmds.file load before load completes via Open Api</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/abort-a-cmds-file-load-before-load-completes-via-open-api/m-p/8044989#M9847</link>
      <description>&lt;P&gt;I should have been more precise. The documentation you linked to for the addCheckFileCallback() is for the Maya Python API 2.0.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Jun 2018 14:13:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/abort-a-cmds-file-load-before-load-completes-via-open-api/m-p/8044989#M9847</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-06-04T14:13:38Z</dc:date>
    </item>
    <item>
      <title>Re: Abort a cmds.file load before load completes via Open Api</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/abort-a-cmds-file-load-before-load-completes-via-open-api/m-p/8095533#M9848</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I've come back around to looking at this and like you say I was referring to API 2.&lt;BR /&gt;Switching over had the effect of properly aborting the file load, thanks for the hint!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However after testing, it seems you can't raise an exception in the callback. Well obviously you can, but the function then doesn't return the False it needs to abort the load. The exception was also caught internally it seemed. There was also a secondary problem where simply returning False wouldn't halt execution of the program, and didn't give enough feedback as to why the operation was aborted.&lt;BR /&gt;&lt;BR /&gt;The solution&amp;nbsp;I found was to pass in some clientdata to the callback in which we set an exception, then check for that when we're sure the callback would've been hit. In this case immediately after the load command. Essentially putting something analogous to the MStatus back into the API &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;It still needs proper testing in real-world scenarios, but it looks to do the job.&lt;BR /&gt;&lt;BR /&gt;Below is a simple contrived example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;import maya.api.OpenMaya as OpenM
batchFileList = [r"c:\path\to\file.ma", r"c:\path\to\file2.ma"]

def _test_raise_exception(fileObject, clientData):
    #pretending that a file ref failed below. Ref path validation code would go here.
    clientData['exception'] = RuntimeError('bad ref {}'.format(fileObject.expandedFullName()))&lt;BR /&gt;    return False

for file_ in batchFileList:
    try:&lt;BR /&gt;        outData = {} &lt;BR /&gt;        cId = OpenM.MSceneMessage.addCheckFileCallback(OpenM.MSceneMessage.kBeforeCreateReferenceCheck, _test_raise_exception, clientData=outData)
        rv = cmds.file(file_, o=1)&lt;BR /&gt;        OpenM.MSceneMessage.removeCallback(cId)
        if 'exception' in outData: #check if an exception was set, if so, raise it&lt;BR /&gt;            raise outData['exception']
    except:
        #handle the exception here
        print 'load cancelled'

&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jun 2018 16:50:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/abort-a-cmds-file-load-before-load-completes-via-open-api/m-p/8095533#M9848</guid>
      <dc:creator>mikey.n</dc:creator>
      <dc:date>2018-06-27T16:50:13Z</dc:date>
    </item>
  </channel>
</rss>

