Select to view content in your preferred language

Unexpected behaviour when setting default value for Toolbox input parameter of type DEFile

529
1
Jump to solution
01-18-2023 03:48 AM
Manu
by
Occasional Contributor

I created an ArcMap Python Toolbox with a DEFile input parameter.

Right after defining the parameter, I placed the lines

 

a = "/some/file.txt"
b = glob.glob("/some/directory/*.txt")
file_parameter.value = a if os.path.exists(a) else b[0]

 

Now, when I open the respective Tool dialogue, it shows the file defined in variable "a". I manually renamed that file and opened the Tool again, but still, the original file name was set as the default input parameter. Then I refreshed the toolbox. Now, the new file name was used as default input value, since it has the extension .txt.

The problem is that the output of the first tool is the input of the second, etc. Thus, the input files are generated or altered while using the toolbox. E.g., file "/some/file.txt" is created by the first tool and used by the second. Obviously, it should not be required to right-click and refresh the Toolbox in between executing different tools.

How does this behaviour arise and how can I tell ArcMap to automatically check what files exist (in a Geodatabase or elsewhere) when the Tool dialogue is opened (rather than when the Toolbox is refreshed or the tool is executed)?

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Interesting behavior, also reproducable in ArcGIS Pro.

To solve it, you can set the default value in getParameterInfo() and do the glob() in updateParameters():

import os, glob

class DEFileTest(object):
    label="DEFileTest"
    description="DEFileTest"    

    def getParameterInfo(self):
        parameters = [
            arcpy.Parameter(name="in_file", displayName="In File", datatype="DEFile", parameterType="Required", direction="Input"),
            ]
        parameters[0].value = "N:/.../temp/test.txt"
        return parameters
        
        
    def updateParameters(self, parameters):
        par = {p.name: p for p in parameters}
        if par["in_file"].value is None or not os.path.exists(str(par["in_file"].value)):
            b = glob.glob("N:/.../temp/*.txt")
            try:
                par["in_file"].value = b[0]
            except IndexError:
                par["in_file"].value = None
	
	
    def execute(self, parameters, messages):
        par = {p.name: p for p in parameters}
        arcpy.AddMessage("I'm working on " + str(par["in_file"].value))

Have a great day!
Johannes

View solution in original post

1 Reply
JohannesLindner
MVP Frequent Contributor

Interesting behavior, also reproducable in ArcGIS Pro.

To solve it, you can set the default value in getParameterInfo() and do the glob() in updateParameters():

import os, glob

class DEFileTest(object):
    label="DEFileTest"
    description="DEFileTest"    

    def getParameterInfo(self):
        parameters = [
            arcpy.Parameter(name="in_file", displayName="In File", datatype="DEFile", parameterType="Required", direction="Input"),
            ]
        parameters[0].value = "N:/.../temp/test.txt"
        return parameters
        
        
    def updateParameters(self, parameters):
        par = {p.name: p for p in parameters}
        if par["in_file"].value is None or not os.path.exists(str(par["in_file"].value)):
            b = glob.glob("N:/.../temp/*.txt")
            try:
                par["in_file"].value = b[0]
            except IndexError:
                par["in_file"].value = None
	
	
    def execute(self, parameters, messages):
        par = {p.name: p for p in parameters}
        arcpy.AddMessage("I'm working on " + str(par["in_file"].value))

Have a great day!
Johannes