4

Is there a way for adding new Processing scripts into Processing toolbox using PyQGIS?

In the GUI, I can click on "Add script to toolbox", but I want to add scripts using PyQGIS.

Add script by GUI

QGIS version: 3.16

Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
Wenceslauw
  • 752
  • 5
  • 17
  • Did you have a look at this answer - maybe it helps: https://gis.stackexchange.com/questions/282773/writing-a-python-processing-script-with-qgis-3-0/284410#284410 – root676 Jan 18 '21 at 12:56

3 Answers3

6

If you want to have the same action as Add Script to Toolbox..., execute the code below :

# import the script
from processing.script.AddScriptFromFileAction import AddScriptFromFileAction

get the reference to the current processing toolbox

toolbox = iface.mainWindow().findChild(QgsDockWidget, "ProcessingToolbox")

create an instance

script_add = AddScriptFromFileAction()

add the reference to the current toolbox

script_add.toolbox = toolbox

execute the script to open a Python script

script_add.execute()

You can look at what the AddScriptFromFileAction do in this script.

J. Monticolo
  • 15,695
  • 1
  • 29
  • 64
  • 1
    Thank you for your solution, but i want only actualize scripts in scripts folder, because i copy script files in openProject macro function. Problem is in copying files, when files are copied after open project. – Wenceslauw Jan 18 '21 at 13:39
  • Look at this code line : https://github.com/qgis/QGIS/blob/32e5ec024c8ff37b398304d39fa9d93a15779f94/python/plugins/processing/script/AddScriptFromFileAction.py#L64 – J. Monticolo Jan 18 '21 at 13:40
5

You can do that using the code snippet below (adapted from the QGIS Resource Sharing plugin):

import os.path
import shutil
import glob
from processing.script import ScriptUtils

my_scripts_dir = "/path/to/your/scripts/" count = 0 qgis_scripts_dir = ScriptUtils.defaultScriptsFolder()

Copy scripts from your script dir to QGIS script dir

for filename in glob.glob(os.path.join(my_scripts_dir, '*.py')): try: shutil.copy(filename, qgis_scripts_dir) count += 1 except OSError as e: print("Couldn't install script '{}'!".format(filename))

Finally, refresh the algorithms for the Processing script provider

if count: QgsApplication.processingRegistry().providerById("script").refreshAlgorithms()


Note: If you are going to add your scripts while QGIS is starting, you will need to check that the script provider is already set. Something like:

if QgsApplication.processingRegistry().providerById('script'):
    ...
Germán Carrillo
  • 36,307
  • 5
  • 123
  • 178
1

Just copy the pythonscript to the user profiles folder, e.g. under windows it would be: C:\Users\username\AppData\Roaming\QGIS\QGIS3\profiles\profilename\processing\scripts

as a python script it would be something like:

import shutil
shutil.copy2('/src/dir/myprocess.py', 'C:/Users/username/AppData/Roaming/QGIS/QGIS3/profiles/profilename/processing/scripts/myprocess.py')

you have to adjust username and profilename in the path. the default profilename is in QGIS: default

eurojam
  • 10,762
  • 1
  • 13
  • 27