7

How do I export my processing modeler as .py so that I can edit its code?

This does not seem as intuitive as previous versions.

5

Early versions of QGIS 3 did not support exporting models to scripts (See: Why "Export as Python script" button is unaccessible in QGIS 3.0/3.1 Modeler by default?)

From QGIS 3.6, the export to script button is back and creates a proper QGIS3 Processing script:

enter image description here

3

You can make it accessible.

  1. Find "DlgModeler.ui" and "ModelerDialog.py" files in QGIS folder. (That folder depends on which way you installed it).
    By default (in Windows), "DlgModeler.ui" is located in "QGIS_PATH/python/plugins/processing/ui/" and "ModelerDialog.py" is located in "QGIS_PATH/python/plugins/processing/modeler/"
  2. Open "DlgModeler.ui" in editor (maybe notepad), find "addaction" tags. Add "<addaction name="mActionExportPython"/> and save.
  3. Open "ModelerDialog.py" in editor and uncomment the lines below and save it.

#self.mActionExportPython.setIcon( # QgsApplication.getThemeIcon('/mActionSaveAsPython.svg')) . #self.mActionExportPython.triggered.connect(self.exportAsPython)

  1. Open QGIS 3.0

enter image description here

EDIT: As @ndawson says (in comments), this method just re-adds the removed "Export as Python Script" button. Exported script has QGIS 2.x syntax and it won't be usable in QGIS 3.x. For further information about QGIS 3.x syntax, visit Processing script template for QGIS3.

EDIT2: From v3.6, QGIS supports to export a model as Python script. Look at @underdark's answer.

  • since adding the button I've been getting an error that doesn't seem to be causing issues yet, but thought I'd see if you could see where I might have gone wrong. I'll be posting a follow up question – Mary Johnson Apr 2 '18 at 1:06
  • 2
    I didn't realized that the syntax of exported file by "Export as Python script" in QGIS 3.0 (in its current form) is uncompatible with QGIS 3.0, because the way to write Processing script has changed. Further information: Processing script template for QGIS3 – Kadir Şahbaz Apr 2 '18 at 2:37
  • 1
    This answer is incorrect - it readds a removed button, but the generated scripts won't actually be usable! – ndawson Apr 2 '18 at 19:42
  • I came on just to state that :) there is a related post about why this has been a disabled function. See kadirs comment above. – Mary Johnson Apr 2 '18 at 21:06
  • 1
    You're right. I reported this situation to moderators. – Kadir Şahbaz Sep 22 '18 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.