1

I am writing a Python Toolbox which needs Shapefiles (or Featureclasses) as input and calculates an output for each of the input files. I would like to have the possibility to rename the files in order to avoid an overwriting or an exception. My approach is to use a toolbox parameter of type GPValueTable with 2 columns. The input files is given in the first column (by the user) and I want the 2. column to be automatically filled by the base name without extension of the input file.

GUI Python Toolbox - on the left the file given by the user and on the right the automatically derived default autput name

Unfortunatelly, I am not able to fill the second column based on the first. I tried various ways already but here is a snipped of sample code:

    def getParameterInfo(self):
    """Define parameter definitions"""

    input_paths = arcpy.Parameter(
        displayName="Input files",
        name="in_paths",
        datatype="GPValueTable",
        parameterType="Required",
        direction="Input",
        multiValue=True)
    input_paths.columns = [['GPFeatureLayer','Eingabedatensatz'], ['GPString','Ausgabename']]

    output_path = arcpy.Parameter(
        displayName="Output path",
        name="out_path",
        datatype="DEFolder",
        parameterType="Required",
        direction="Input")

    params = [input_paths,output_path]
    return params

def isLicensed(self):
    """Set whether tool is licensed to execute."""
    return True

def updateParameters(self, parameters):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parameter
    has been changed."""
    if parameters[0].value:
        new_vt = []
        gp_value_table = parameters[0].value
        vt_length = len(gp_value_table)
        if  gp_value_table[vt_length-1][0] and not not gp_value_table[vt_length-1][1]:
                    new_vt = [[v[0],v[2]] for v in gp_value_table]
                    input_path = gp_value_table[vt_length-1][0]
                    new_vt.append([vt_length-1,arcpy.Describe(input_path).baseName])
                    parameters[0].value = new_vt
    return

In this case nothing happens and the 2. column remains emtpy. How can I - row by row - derive the default output name based on the input?

  • What version of ArcMap are you using, there are differences in the behaviour for this parameter type? – Hornbydd Apr 10 '18 at 0:00
  • I'm currently using ArcMap 10.4.1 – user2132627 Apr 13 '18 at 5:00
0

The only code you need to change is the code in your updateParameters()

def updateParameters(self, parameters):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parameter
    has been changed."""

    # Create a ValueTable object and load it with current values from parameter 
    # which itself is a ValueTable
    vtab = arcpy.ValueTable(len(parameters[0].columns))
    vtab.loadFromString(parameters[0].valueAsText)

    # Create a list of the values in the parameter, this will be a list of lists
    aList = parameters[0].values

    # Step through list, extract basename and update row in ValueTable object
    for i,v in enumerate(aList):
        fc = v[0]
        bn = arcpy.Describe(fc).baseName
        newvalue = "'" + str(fc) + "'" + " " + "'" + bn + "'"
        vtab.setRow(i, newvalue)

    # Write the contents of ValueTable object back into the Parameter which will overwrite everything
    parameters[0].value = vtab.exportToString()
    return
|improve this answer|||||
  • That works as long as the user doesn't update the values manually. When changing the values the script automatically discards the changes. – user2132627 Apr 18 '18 at 12:13
  • You asked for column 2 to be automatically filled by the base name without extension of the input file. This is what the code does. You need to augment the script to include a check to see if a base name exists and if it is NOT the same (so they have manually edited it) then don't update that row. You should be able to work that out. – Hornbydd Apr 18 '18 at 13: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.