6

I have 2 scripts within QGIS processing toolbox and I can't seem to work out how to access the parameters of 1 script from the other. Since QGIS sets up the processing parameters (by 'looking for' the ## notation) before the rest of the script is run, the parameters are already set up by the time the second script is called. Has anyone found a work around for this?

script a:

#script a
import imp, os.path

def import_(filename):
    (path, name) = os.path.split(filename)
    (name, ext) = os.path.splitext(name)

    (file, filename, data) = imp.find_module(name, [path])
    return imp.load_module(name, file, filename, data)

foo = import_("path to b")
foo.getParameters()

and script b...

#script b

def getParameters():

    ##parameter_1=string
    ##parameter_2=number
    return 

The reason I'm trying to do this is because QGIS makes a local copy of the script to run when a user clicks 'add script from file'. So, if I later want to change the input parameters, I would need to inform the user that the script is updated and that they need to 'add script from file' again to get the most recent version. If I can create a script for local copies that just calls the main server script, I can make updates without having to get all the script users to manually change to the latest version.

I know plugins are a more appropriate way to control this kind of workflow, but in the corporate I'm working on I don't have (and can't get) Qt installed to make the GUI.

5
  • I think that i don't understand your quiestion completely,but check this documentation and tutorial for more information about processing scripts : howtoinqgis.wordpress.com/2017/04/12/… , qgistutorials.com/es/docs/processing_python_scripts.html , github.com/qgis/QGIS-Processing/tree/master/scripts
    – Fran Raga
    Commented Dec 11, 2017 at 11:20
  • two questions,Any reason for not use Qsettings?And you first execute band after execute a,right?
    – Fran Raga
    Commented Dec 11, 2017 at 12:03
  • Not quite, I want to execute script a which imports the functions from script b and executes them from within a. I'm not too familiar with Qsettings() so not sure how that would help. If you have an idea please share Commented Dec 11, 2017 at 12:18
  • If you want this,your code is correct
    – Fran Raga
    Commented Dec 11, 2017 at 12:20
  • Qt Designer is free and when you install QGIS from Osge4w ,Qt Designer is installed too for make ui
    – Fran Raga
    Commented Dec 11, 2017 at 15:07

2 Answers 2

3
+50

Ok, this doesn’t really answer your question, but since I had the same problem, I thought you might be interested in the workaround I found:

In the QGIS Processing-Options that can be found in the processing menu (top of QGIS windows in the menu bar), under “Scripts” you can add a user defined path (by simply putting a “;” and start a new path) to the place where your scripts are stored, e.g. at a central storage. This way, QGIS will load the latest version of your scripts at startup. It would be necessary to modify this path in the installations of all your colleges, but ones done, your all set. And theoretically the second scripts wouldn’t be necessary, cause you will always end up with the latest “update” of your scripts.

Hope this helps.

3

After my comments I add an example to try to help you.

I have Two script "A" and "B" and for in firts time execute the Script B ,and the user enter inputs for script (parameter_1 and parameter_2) and after execute Script A. I this script have a log that you can show print in a custom tab.

For get parameters values taht the user write in a Script B I'm using QSettings class.

Script A

import imp, os.path
from qgis.core import QgsMessageLog

Log = lambda m: QgsMessageLog.logMessage(m, 'Test Scripts')

def import_(filename):
    (path, name) = os.path.split(filename)
    (name, ext) = os.path.splitext(name)
    print name
    print path

    (file, filename, data) = imp.find_module(name, [path])
    return imp.load_module(name, file, filename, data)

foo = import_("C:\\Users\\fjraga\\.qgis2\\processing\\scripts\\b.py")
param1,param2=foo.getParameters()
Log("Parameter 1: "+ str(param1))
Log("Parameter 2: "+ str(param2))

#Other call
foo.CallFromA("Calling from A script")
#Name
n=foo.GetNameValue()
Log("Name : "+ str(n))

Script B

##parameter_1=string
##parameter_2=number
from PyQt4.QtCore import QSettings
from qgis.core import QgsMessageLog

Log = lambda m: QgsMessageLog.logMessage(m, 'Test Scripts')
name="My Name"
try:
    QSettings().setValue( '/myscript/processing/b/parameter_1', parameter_1 )
    QSettings().setValue( '/myscript/processing/b/parameter_2', parameter_2 )
except:
    None

def getParameters():
    parameter_1_out = QSettings().value( '/myscript/processing/b/parameter_1')
    parameter_2_out = QSettings().value( '/myscript/processing/b/parameter_2')
    return parameter_1_out,parameter_2_out

def CallFromA(value):
    Log(value)
    return 

def GetNameValue():
    return name

First,Execute B and write the inputs: enter image description here

And after execute A and you can show the logs messages,

enter image description here

I hope this helped you

3
  • Thanks for this, this has sparked some ideas for a couple of projects I'm currently looking at. Unfortunately, itdoes not solve the problem outlined here though as the user must still make a local copy of b and run it to load Qsettings(). Nevertheless, a REALLY useful idea. Commented Dec 11, 2017 at 16:58
  • you can call a b.py located in your server and overwrite user settings too
    – Fran Raga
    Commented Dec 11, 2017 at 17:33
  • And is more simple,if you have you b.py in your server the clients only need extract this values,is the example GetNameValue for return each variable.
    – Fran Raga
    Commented Dec 11, 2017 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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