1

I want to set the default value of one parameter based on another one. The files might be moved between different locations, but their relative locations would be the same. For example, file A is at C:\test\model, and file B at C:\test\tiger. Therefore, I want to use os.path.join(os.path.dirname(fileB), "tiger") to set the default value.

I tried in the Python Toolbox as the following code, but it didn't work and gave me the error message.

import arcpy, os

class Toolbox(object):
    def __init__(self):
        self.label =  "Model"
        self.alias  = ""

        # List of tool classes associated with this toolbox
        self.tools = [step1]

class step1(object):
    def __init__(self):
        self.label       = "step 1"
        self.description = ""

    def getParameterInfo(self):
        #Define parameter definitions
        # Model folder parameter
        model_folder = arcpy.Parameter(
            displayName="Model Location",
            name="model_folder",
            datatype="Folder",
            parameterType="Required",
            direction="Input")
        model_folder.value = r'C:\model'

        # Tiger dataset folder parameter
        tiger_folder = arcpy.Parameter(
            displayName="TIGER",
            name="tiger_folder",
            datatype="Folder",
            parameterType="Required",
            direction="Input",
            enabled=True)

        parameters = [model_folder,
                      tiger_folder
                      ]

        return parameters

    def isLicensed(self): #optional
        return True

    def updateParameters(self, parameters): #optional
        parameters[1].value = os.path.join(os.path.dirname(parameters[0].value), "tiger")


    def updateMessages(self, parameters): #optional
        return

    def execute(self, parameters, messages):
        return

Errors:

Close WindowERROR Traceback (most recent call last): File "", line 219, in updateParameters File "C:\Python27\ArcGIS10.3\Lib\ntpath.py", line 205, in dirname return split(p)[0] File "C:\Python27\ArcGIS10.3\Lib\ntpath.py", line 170, in split d, p = splitdrive(p) File "C:\Python27\ArcGIS10.3\Lib\ntpath.py", line 125, in splitdrive if p[1:2] == ':': TypeError: 'geoprocessing value object' object has no attribute 'getitem'

I also add several self-defined functions to help me manipulate with the parameters interaction, but they didn't work. It seems like in the updateParameters of Python Toolbox, we can't use self-defined functions.

Any suggestions? Using Advanced 10.3.1 ArcGIS.

6
  • Did you add import os? Commented Mar 14, 2017 at 22:37
  • I have import os in the very beginning of this Python Toolbox. Commented Mar 14, 2017 at 22:41
  • I got it. Change the parameters[0].value to parameters[0].value.value. Can anyone explain this to me? Thanks. Commented Mar 15, 2017 at 0:39
  • I learned it from this link (pro.arcgis.com/en/pro-app/arcpy/geoprocessing_and_python/…), which also makes me confused, but it did work. Go to the link and search .value.value Commented Mar 15, 2017 at 1:26
  • Interesting... I must have never actually tried it in that scenario before! ArcGIS 10.x documentation says the same (since you posted the ArcGIS Pro doc) - desktop.arcgis.com/en/arcmap/latest/analyze/creating-tools/… Commented Mar 15, 2017 at 2:06

1 Answer 1

3

The os.path methods operate on strings. You are passing a Value object not a string to os.path.dirname. Use parameters[0].valueAsText or parameters[0].value.value instead of parameters[0].value.

i.e.

    parameters[1].value = os.path.join(os.path.dirname(parameters[0].valueAsText), "tiger")

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.