<?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: Hard crash: return focus to starting document from a command with a dialog in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/hard-crash-return-focus-to-starting-document-from-a-command-with/m-p/8413355#M15185</link>
    <description>&lt;P&gt;Thanks for posting and providing the steps to reproduced it. I created&amp;nbsp;UP-40635 ticket in our internal bug tracking system. I hope fix should be available in January release of Fusion.&lt;/P&gt;</description>
    <pubDate>Tue, 20 Nov 2018 13:25:13 GMT</pubDate>
    <dc:creator>goyals</dc:creator>
    <dc:date>2018-11-20T13:25:13Z</dc:date>
    <item>
      <title>Hard crash: return focus to starting document from a command with a dialog</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/hard-crash-return-focus-to-starting-document-from-a-command-with/m-p/8405242#M15184</link>
      <description>&lt;P&gt;Good evening,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have found I can generate a hard crash of Fusion 360 (see, e.g.,&amp;nbsp;CER_183996679) by returning focus to the initial document from within a command.&amp;nbsp; &amp;nbsp;By initial document, I mean the document that was active when you executed the command.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is true whether you lose focus by creating a new document or opening an existing one, and whether you return focus by closing the new document or by using the activate() method of the initial document.&amp;nbsp; The code snippet below causes the hard crash every time:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;def returnFocusTest():
    app = adsk.core.Application.get()
    startingDoc = app.activeDocument
    otherDoc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
    adsk.doEvents()
    startingDoc.activate()
    adsk.doEvents()&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, this is &lt;STRONG&gt;only&lt;/STRONG&gt; true when the command generates a command dialog.&amp;nbsp; Running&amp;nbsp;returnFocusTest() as a script works fine.&amp;nbsp; As does putting it the command execute handler of a dialog-free command.&amp;nbsp; However, as soon as you add any command input to the create event handler (even if they go unused) then the function above causes Fusion 360 to crash on the activate() line.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a vague recollection of this issue being&amp;nbsp;discussed several years ago, but I can't find any reference to it in the forums.&amp;nbsp; Has anyone else come across this?&amp;nbsp; Does anyone know a way around it?&amp;nbsp; And secondarily, is Autodesk aware of it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you everyone for your help.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 21:59:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/hard-crash-return-focus-to-starting-document-from-a-command-with/m-p/8405242#M15184</guid>
      <dc:creator>p.seem</dc:creator>
      <dc:date>2018-11-15T21:59:42Z</dc:date>
    </item>
    <item>
      <title>Re: Hard crash: return focus to starting document from a command with a dialog</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/hard-crash-return-focus-to-starting-document-from-a-command-with/m-p/8413355#M15185</link>
      <description>&lt;P&gt;Thanks for posting and providing the steps to reproduced it. I created&amp;nbsp;UP-40635 ticket in our internal bug tracking system. I hope fix should be available in January release of Fusion.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Nov 2018 13:25:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/hard-crash-return-focus-to-starting-document-from-a-command-with/m-p/8413355#M15185</guid>
      <dc:creator>goyals</dc:creator>
      <dc:date>2018-11-20T13:25:13Z</dc:date>
    </item>
    <item>
      <title>Re: Hard crash: return focus to starting document from a command with a dialog</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/hard-crash-return-focus-to-starting-document-from-a-command-with/m-p/8426555#M15186</link>
      <description>&lt;P&gt;We looked at it and sharing our findings. Adding a new document fires up a command internally and it is not recommended when an existing command is completing we should not fire a new command because it might lead to some weird behaviours while cleaning up the objects of the existing command. I hope it answer your query but in case you have follow up question then feel free to reach out to us. Thank you.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Nov 2018 08:16:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/hard-crash-return-focus-to-starting-document-from-a-command-with/m-p/8426555#M15186</guid>
      <dc:creator>goyals</dc:creator>
      <dc:date>2018-11-27T08:16:33Z</dc:date>
    </item>
    <item>
      <title>Re: Hard crash: return focus to starting document from a command with a dialog</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/hard-crash-return-focus-to-starting-document-from-a-command-with/m-p/8426697#M15187</link>
      <description>&lt;P&gt;Given autodesk's non-answer answer above, it doesn't sound like they intend to do anything to fix this crash.&amp;nbsp; So if anyone else comes across this issue, it's worth knowing there is a way around it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This had the smell of a threading problem, and sure enough if you push the work (everything after 'otherDoc = ...') onto it's own thread (as in their&amp;nbsp;ThreadEventHandler example) that allows your initial command to close cleanly so the rest of the work continues to execute&amp;nbsp;without any errors/crashes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't imagine that was very clear, so here's what I mean, for posterity.&amp;nbsp; This version of returnFocusTest works:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;stopFlag = None
myCustomEvent = 'returnFocusCustomEventId'
startingDoc = None
    
def returnFocusTest():
    global startingDoc
    startingDoc = app.activeDocument
    app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
    adsk.doEvents()
    
    # Create a new thread for the other processing.        
    global stopFlag        
    stopFlag = threading.Event()
    myThread = MyThread(stopFlag)
    myThread.start()      

class ThreadEventHandler(adsk.core.CustomEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        startingDoc.activate()
        adsk.doEvents() 
                
class MyThread(threading.Thread):
    def __init__(self, event):
        threading.Thread.__init__(self)
        self.stopped = event

    def run(self):
       self.stopped.wait(10)
       app.fireCustomEvent(myCustomEvent) &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Nov 2018 09:22:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/hard-crash-return-focus-to-starting-document-from-a-command-with/m-p/8426697#M15187</guid>
      <dc:creator>p.seem</dc:creator>
      <dc:date>2018-11-27T09:22:59Z</dc:date>
    </item>
  </channel>
</rss>

