<?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: Python Script: Sketch created in an event handler does not persist in Fusion API and Scripts Forum</title>
    <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/python-script-sketch-created-while-ui-is-displayed-does-not/m-p/13321532#M227</link>
    <description>&lt;LI-CODE lang="python"&gt;import adsk.core, adsk.fusion, adsk.cam, traceback

app = adsk.core.Application.get()
ui = app.userInterface
handlers = []
design = adsk.fusion.Design.cast(app.activeProduct)
sketch = None  # Global reference to sketch

# CommandCreated handler class
class DialogCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def notify(self, args):
        dialog = args.command
        inputs = dialog.commandInputs
        
        button = inputs.addBoolValueInput('clickMe', 'Check', True)
        
        # Attach an event handler directly to the command's inputs
        valueHandler = ValueChangedHandler()
        dialog.inputChanged.add(valueHandler)  # Attach to the command's inputChanged event
        handlers.append(valueHandler)

# InputChanged handler class
class ValueChangedHandler(adsk.core.InputChangedEventHandler):
    def notify(self, args):
        global sketch
        changedInput = args.input
        if changedInput.id == 'clickMe':
            try:
                rootComp = design.rootComponent
                sketches = rootComp.sketches
                xyPlane = rootComp.xYConstructionPlane
                
                sketch = sketches.add(xyPlane)
                
                # Draw a single line segment
                startPoint = adsk.core.Point3D.create(0, 0, 0)
                endPoint = adsk.core.Point3D.create(100, 0, 0)
                sketch.sketchCurves.sketchLines.addByTwoPoints(startPoint, endPoint)
                
                app.activeViewport.refresh()
                ui.messageBox(f'New value: {changedInput.value}\nLine segment drawn')
            except Exception as e:
                ui.messageBox(f'Error: {traceback.format_exc()}')

# CommandDestroyed handler class (cleanup)
class DialogDestroyHandler(adsk.core.CommandEventHandler):
    def notify(self, args):
        global handlers
        handlers.clear()

# Main run function
def run(context):
    try:
        cmdDef = ui.commandDefinitions.itemById('NumericDialogCommand')
        if not cmdDef:
            cmdDef = ui.commandDefinitions.addButtonDefinition('NumericDialogCommand', 'Numeric Dialog', 'Dialog with numeric input')
        
        createdHandler = DialogCreatedHandler()
        destroyHandler = DialogDestroyHandler()
        
        cmdDef.commandCreated.add(createdHandler)
        handlers.append(createdHandler)
        handlers.append(destroyHandler)
        
        cmdDef.execute()
    except Exception as e:
        ui.messageBox(f'Failed:\n{traceback.format_exc()}')&lt;/LI-CODE&gt;</description>
    <pubDate>Sun, 16 Feb 2025 00:23:43 GMT</pubDate>
    <dc:creator>mash_farid</dc:creator>
    <dc:date>2025-02-16T00:23:43Z</dc:date>
    <item>
      <title>Python Script: Sketch created while UI is displayed does not persist</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/python-script-sketch-created-while-ui-is-displayed-does-not/m-p/13321531#M226</link>
      <description>&lt;P&gt;I am trying to create a script that displays a dialog box with multiple parameters and then creates an object according to those parameters. The user can check the result, modify the parameters and see the result again and again until he's satisfied.&lt;/P&gt;&lt;P&gt;The problem I'm facing is that an object that is created in the (value changed) event handler, does not persist. It disappears from view (and the sketches list) as soon as the event handling terminates.&lt;BR /&gt;I have created a small version of the script (see then attached document) to demonstrate the problem:&lt;BR /&gt;In the notify() function of &lt;SPAN&gt;&lt;STRONG&gt;ValueChangedHandler&lt;/STRONG&gt;()&amp;nbsp; &amp;nbsp;&lt;BR /&gt;after the message box is displayed and user clicks Ok, the segment disappears.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mash_farid_0-1739665091222.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1467373i888018DB1F459085/image-size/medium?v=v2&amp;amp;px=400" role="button" title="mash_farid_0-1739665091222.png" alt="mash_farid_0-1739665091222.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;what is the problem ?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Edit: Every sketch added before cmdDef.execute() in the main thread survives.&amp;nbsp; Sketches added after the dialog box is displayed -even if the sketch is added in the same main thread-&amp;nbsp; is deleted when I close the dialog box. So it does not matter if the sketch is added in an event handler or not, it will be discarded at the end.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 16 Feb 2025 21:04:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/python-script-sketch-created-while-ui-is-displayed-does-not/m-p/13321531#M226</guid>
      <dc:creator>mash_farid</dc:creator>
      <dc:date>2025-02-16T21:04:28Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: Sketch created in an event handler does not persist</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/python-script-sketch-created-while-ui-is-displayed-does-not/m-p/13321532#M227</link>
      <description>&lt;LI-CODE lang="python"&gt;import adsk.core, adsk.fusion, adsk.cam, traceback

app = adsk.core.Application.get()
ui = app.userInterface
handlers = []
design = adsk.fusion.Design.cast(app.activeProduct)
sketch = None  # Global reference to sketch

# CommandCreated handler class
class DialogCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def notify(self, args):
        dialog = args.command
        inputs = dialog.commandInputs
        
        button = inputs.addBoolValueInput('clickMe', 'Check', True)
        
        # Attach an event handler directly to the command's inputs
        valueHandler = ValueChangedHandler()
        dialog.inputChanged.add(valueHandler)  # Attach to the command's inputChanged event
        handlers.append(valueHandler)

# InputChanged handler class
class ValueChangedHandler(adsk.core.InputChangedEventHandler):
    def notify(self, args):
        global sketch
        changedInput = args.input
        if changedInput.id == 'clickMe':
            try:
                rootComp = design.rootComponent
                sketches = rootComp.sketches
                xyPlane = rootComp.xYConstructionPlane
                
                sketch = sketches.add(xyPlane)
                
                # Draw a single line segment
                startPoint = adsk.core.Point3D.create(0, 0, 0)
                endPoint = adsk.core.Point3D.create(100, 0, 0)
                sketch.sketchCurves.sketchLines.addByTwoPoints(startPoint, endPoint)
                
                app.activeViewport.refresh()
                ui.messageBox(f'New value: {changedInput.value}\nLine segment drawn')
            except Exception as e:
                ui.messageBox(f'Error: {traceback.format_exc()}')

# CommandDestroyed handler class (cleanup)
class DialogDestroyHandler(adsk.core.CommandEventHandler):
    def notify(self, args):
        global handlers
        handlers.clear()

# Main run function
def run(context):
    try:
        cmdDef = ui.commandDefinitions.itemById('NumericDialogCommand')
        if not cmdDef:
            cmdDef = ui.commandDefinitions.addButtonDefinition('NumericDialogCommand', 'Numeric Dialog', 'Dialog with numeric input')
        
        createdHandler = DialogCreatedHandler()
        destroyHandler = DialogDestroyHandler()
        
        cmdDef.commandCreated.add(createdHandler)
        handlers.append(createdHandler)
        handlers.append(destroyHandler)
        
        cmdDef.execute()
    except Exception as e:
        ui.messageBox(f'Failed:\n{traceback.format_exc()}')&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 16 Feb 2025 00:23:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/python-script-sketch-created-while-ui-is-displayed-does-not/m-p/13321532#M227</guid>
      <dc:creator>mash_farid</dc:creator>
      <dc:date>2025-02-16T00:23:43Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: Sketch created while UI is displayed does not persist</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/python-script-sketch-created-while-ui-is-displayed-does-not/m-p/13322458#M228</link>
      <description>&lt;P&gt;I confirm that while the UI thread is running, every sketch added is deleted on exit from the dialog box.&lt;/P&gt;&lt;P&gt;I don't know why.&lt;BR /&gt;The only workaround so far is to :&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;While the UI is active in the dialog box, save the created sketches in a global entity&lt;/LI&gt;&lt;LI&gt;create a threading&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;Event that will be set when the dialog box is closed with [Ok]&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;create another threading&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;Event that will be set when the dialog box is closed with Cancel]&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;in the main thread after cmDef.execute()&amp;nbsp; wait for either of the events to be set&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;if it's the [Ok] event then add the sketches saved in the global entity&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;if it's [cancel] event then terminate,&amp;nbsp;&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Feb 2025 00:35:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/python-script-sketch-created-while-ui-is-displayed-does-not/m-p/13322458#M228</guid>
      <dc:creator>mash_farid</dc:creator>
      <dc:date>2025-02-17T00:35:47Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: Sketch created while UI is displayed does not persist</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/python-script-sketch-created-while-ui-is-displayed-does-not/m-p/13323858#M229</link>
      <description>&lt;P&gt;You should create the sketch in the ExecutePreview event of the command, not in the ValueChanged event.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Feb 2025 17:42:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/python-script-sketch-created-while-ui-is-displayed-does-not/m-p/13323858#M229</guid>
      <dc:creator>BrianEkins</dc:creator>
      <dc:date>2025-02-17T17:42:41Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: Sketch created while UI is displayed does not persist</title>
      <link>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/python-script-sketch-created-while-ui-is-displayed-does-not/m-p/13326152#M230</link>
      <description>&lt;P&gt;Thank you Brian,&lt;/P&gt;&lt;P&gt;Of course!&amp;nbsp;&lt;BR /&gt;I should've asked for a simple Hello World example that does what I want to do in the first place.&lt;/P&gt;&lt;P&gt;But I learned a lot during the painful process of looking for a solution.&lt;/P&gt;&lt;P&gt;cheers&lt;/P&gt;</description>
      <pubDate>Tue, 18 Feb 2025 19:00:09 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/fusion-api-and-scripts-forum/python-script-sketch-created-while-ui-is-displayed-does-not/m-p/13326152#M230</guid>
      <dc:creator>mash_farid</dc:creator>
      <dc:date>2025-02-18T19:00:09Z</dc:date>
    </item>
  </channel>
</rss>

