7

I have a a fairly simple script tool dialogue which has been setup to get 6 parameters from the user and return them as the tool output as a list. the aims is that the out put would then be used elsewhere within a python add-in tool. I know I could revert to storing these values as a text file but that's not my preferred option. I can see the result in the results window but just can't get to it.

Below is the script to dialogue calls on closure and the seventh parameter is set to an output to store the first six parameters. I have been using the pythonaddins module to call the tool through PToolDialog(Toolbox,Toolname)

What I really want is to parse the output parameter from the script tool to the Python addin.

Script called by Python tool dialogue:

import arcpy


para1 = arcpy.GetParameter(0)
para2 = arcpy.GetParameter(1)
para3 = arcpy.GetParameter(2)
para4 = arcpy.GetParameter(3)
para5 = arcpy.GetParameter(4)
para6 = arcpy.GetParameter(5)
paraList = [para1,para2,para3,para4,para5,para6]
arcpy.SetParameter(6,paraList)
  • 2
    Save yourself some typing with a list comprehension: paraList = [arcpy.GetParameterAsText(i) for i in range(6)] – Jerry Nov 3 '13 at 16:18
5

Well I couldn't work out how to access the outputs of the script tool from the python addin, but i have achieved the next best thing which works for me. Which was to pass the list from the script in the script tool dialog to a list in the python add in tool. By importing the python add in tool into the script for the script tool I was able to access the list variable.

PythonAddinToolbar.py

    import pythonaddins

    parameterList = []

    pythonaddins.GPToolDialog('Toolbox','PythonDialogTool')

PythonDialogToolScript.py

    import PythonAddinToolbar

    #get parameters from dialog
    para1 = arcpy.GetParameterAsText(0)
    para2 = arcpy.GetParameterAsText(1)
    para3 = arcpy.GetParameterAsText(2)
    para4 = arcpy.GetParameterAsText(3)
    para5 = arcpy.GetParameterAsText(4)
    para6 = arcpy.GetParameterAsText(5)

    paraList = [para1,para2,para3,para4,para5]

    PythonAddinToolbar.parameterList = paraList
2

Another option is to exchange parameters by making use of environment variables. However, this works only for strings.

Set an environment variable in the add-in code:

os.environ['TOOLNAME_VAR'] = 'abc'

Read the environment variable in the script tool code:

param = os.environ['TOOLNAME_VAR']
1

The GetParameter returns an object. If you are needing text, I think you would want to use GetParameterAsText which will return a string. Thus, your paraList will be a list of strings. This list can then be parsed and passed to tools/buttons/etc within your addin using python. If I understand your question correctly, this should work for you.

EDIT: I belive SetParameter is useful for passing objects from a script to a script tool. However, I'm not sure this can be passed as a parameter to an addin script. I believe the addin would need to see the parameter values (object or string) a global variable.

  • Cheers I have actually written an alternative script which uses GetParametersAsText() and passes these to a string vartiable which is then written to a text file for recall. – Cassidy Oct 29 '13 at 0:07
  • However what i really want is a way to pass the parameter from the script tool to the python addin which called the script tool. Any ideas?? – Cassidy Oct 29 '13 at 0:09
  • I would use a global variable. Look here – Barbarossa Oct 29 '13 at 13:32
0

In your script tool, add an output variable for every input that you want to pass on to your add-in script. Make these output variables 'Type' as Derived and match each one with the corresponding input variable (with the 'Obtained from' property). These output variables don't show up in the tool when it is run, but are included in the Result, which can be picked up by the Add-in. You'll have to look into it a bit more as to how to get the ouputs out of the result, but some experimentation should be able to give you your answers.

Hope it helps

EDIT: To get the output from a Result, see this Esri help page,

result = yourscripttool(para1,para2,para3,para4,para5,para6)
output1 = result.getOutput(0) # or result[0]
output2 = result.getOutput(1) # or result[1]

I also found this post, and it looks like the result isn't obtainable from GPToolDialog.

  • Thanks but it's trying to access the outputs out of the result that is my problem – Cassidy Oct 30 '13 at 8:04
  • Hmm... I would think it's fairly straightforward. I've edited my answer, but what add-in are you using and why? – cndnflyr Oct 30 '13 at 13:16
  • Thanks for your added input, hopefully they fix this in the future but for now at least I have a better work around then writing to a text file. – Cassidy Oct 30 '13 at 15:59

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.