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.

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

  • 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. – firefly-orange Dec 11 '17 at 16:58
  • you can call a b.py located in your server and overwrite user settings too – Fran Raga Dec 11 '17 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 Dec 11 '17 at 18:16

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.