0

I'm building a Python toolbox and when I run one of my tools, I noticed that the table of contents doesn't update itself in the drop down list of my Python tool. As you can see in the image below, some layers are present in the list and are not in the table of contents although I've updated my data frame (by the way all of these layers are created by a previously executed Python tool).

enter image description here

These are the functions I used to update my data frame, without any result:

arcpy.RefreshActiveView() arcpy.RefreshTOC()

Also, the increment after the GPLx layers adds up every time I re-execute the tool. This means that somewhere in the system, an identical layer is stored. That's why Python increments the layer number suffix (I guess).

I thought that ArcGIS probably stores the layer history somewhere so I looked for it but didn't find it.


EDIT

Run the following tool and put any layer you have in the input parameter. Once it's done, remove the layer from your data frame and go back to re-execute the tool. You will notice in the drop down list that the removed layer is still there:

# -*- coding: utf-8 -*- import arcpy

class Toolbox(object):

def __init__(self):
    """Define the toolbox (the name of the toolbox is the name of the
    .pyt file)."""
    self.label = "LayerTest"
    self.alias = ""`

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

class LayerTestTool(object):

def __init__(self):
    """Define the tool (tool name is the name of the class)."""
    self.label = "LayerTestTool"
    self.description = ""
    self.canRunInBackground = False

def getParameterInfo(self):
    """Define parameter definitions"""
    params = []

    param_in = arcpy.Parameter(
            displayName="In Layer",
            name="Layer",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")
    params.append(param_in)


    param_out = arcpy.Parameter(
            displayName="out",
            name="out",
            datatype="GPFeatureLayer",
            parameterType="Derived",
            direction="Output")
    params.append(param_out)

    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."""
    return

def updateMessages(self, parameters):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return

def execute(self, parameters, messages):
    """The source code of the tool."""
    Layer = parameters[0].valueAsText
    out = arcpy.MakeFeatureLayer_management(Layer, "myoutput")
    arcpy.SetParameterAsText(1, out)
    return
  • Can you post all of your code? Are you using mxd = arcpy.mapping.MapDocument("CURRENT") – GBG Jun 21 '17 at 14:42
  • Yes, I am using arcpy.mapping.MapDocument("CURRENT"). My code is kind of long to post it all here, but I can tell you that these layers are output parameters of my Python tool. – mattcp2 Jun 21 '17 at 15:29
  • We still need to see some test code extracted from your full code that is a Python Toolbox illustrating where you are stuck. I think you may need to use tool validation. – PolyGeo Jun 21 '17 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.