"operation not permitted" when writing a file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi all,
I am trying to create a simple add-in in python, simply to save a body to an STL file. I am a complete noob at the Fusion 360 API, and the learning curve is steep, it always crashed without any meaning error message!
I was about to get things going, when I got this error message when saving the file: "PermissionError: Operation not permitted".
I am suspecting some Mac OS process sandboxing issue, but it has to be noted that I can save an STL file manually from Fusion 360 in the same directory, so it's not a complete interdiction.
here is the message:
here is the work in progress:
import json import adsk.cam import adsk.core import adsk.fusion import traceback USER_PARAM = 'exporter_stl' keepHandlers = [] deletableObjects = [] def find_brep(chain, component, ui): if len(chain) > 1: if component.objectType == 'adsk::fusion::Component': child = component.occurrences.itemByName(chain[0]) else: child = component.childOccurrences.itemByName(chain[0]) return find_brep(chain[1:], child, ui) elif len(chain) == 1: return component.bRepBodies.itemByName(chain[0]) return None class ExportCommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler): def notify(self, args): cmd = args.command app = adsk.core.Application.get() ui = app.userInterface try: design = app.activeProduct dir_param = design.userParameters.itemByName(USER_PARAM) data = None if dir_param: data = json.loads(dir_param.comment) else: dialog = ui.createFileDialog() dialog.title = 'Select export file' dialog.filter = '*.*' result = dialog.showSave() if result == adsk.core.DialogResults.DialogOK: file_name = dialog.filename ui.messageBox('select body') selected = ui.selectEntity('select body', 'Bodies,MeshBodies') entity = selected.entity chain = [entity.name] obj = entity while True: obj = obj.assemblyContext if obj: chain.append(obj.name) else: break data = {'file': file_name, 'chain': list(reversed(chain))} json_params = json.dumps(data) design.userParameters.add(USER_PARAM, adsk.core.ValueInput.createByString('0'), '', json_params) if data: body = find_brep(data['chain'], design.rootComponent, ui) if body: ui.messageBox('body found: ' + body.name) exportMgr = design.exportManager ui.messageBox('file: ' + data['file']) with open(data['file'] + '.txt', 'w') as out_file: out_file.write('whatever') stl_export_options = exportMgr.createSTLExportOptions(body) stl_export_options.sendToPrintUtility = False stl_export_options.meshRefinement = 0 stl_export_options.filename = data['file'] exportMgr.execute(stl_export_options) else: ui.messageBox('.'.join(data['chain']) + 'not found') except: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) def run(context): app = adsk.core.Application.get() ui = app.userInterface try: print('STARTING') cmdDefs = ui.commandDefinitions myButton = ui.commandDefinitions.itemById('ExportDesign') if myButton: myButton.deleteMe() myButton = cmdDefs.addButtonDefinition('ExportDesign', 'Export STL', 'Export the design in various formats.') onCommandCreated = ExportCommandCreatedEventHandler() myButton.commandCreated.add(onCommandCreated) keepHandlers.append(onCommandCreated) ui.allToolbarPanels.itemById('SolidMakePanel').controls.addCommand(myButton, '', False) except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) def stop(context): ui = None try: app = adsk.core.Application.get() ui = app.userInterface button = ui.commandDefinitions.itemById('ExportDesign') if button: button.deleteMe() global keepHandlers keepHandlers = [] # for obj in deletableObjects: # obj.deleteMe() except: if ui: ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
thanks for your help.