Select to view content in your preferred language

Model Builder/Chart Module Index Error

375
3
06-28-2024 05:45 PM
JJH
by
Emerging Contributor

I am encountering an issue when using a charts module script in conjunction with a model builder tool. The script tool functions perfectly when used outside model builder and can find the input layer in the contents and add the created chart to it, however, once this same script tool is put into model builder it throws an index error and cannot find the layer being created within the model. The odd thing is, if the script is run after the fact on the same output layer from the model, it works perfectly, and if it is run in a separate model with just the output feature layer (as a variable) then the chart tool also runs perfectly. Only when both the layer creation and chart creation are being done in the same model does it throw the index error.  

I have tried:

  • checking add to display in model builder for the layer and setting it as a parameter.
  • various iterators, logic stops, export features, and save to layerfile, none of which worked.
  • unchecked the project properties setting for model builder that automatically outputs into a group. 

Chart Tool Code Example: (Can provide params if needed)

import arcpy

Input_Layer = arcpy.GetParameter(0)
ChartInfo_Title = arcpy.GetParameterAsText(2)
ChartInfo_xField = arcpy.GetParameterAsText(3)
ChartInfo_binCount = arcpy.GetParameter(4)
ChartInfo_xTitle = arcpy.GetParameterAsText(5)
ChartInfo_yTitle = arcpy.GetParameterAsText(6)
ChartInfo_TransformType = arcpy.GetParameterAsText(7)

aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps("Map")[0]
lyr = map.listLayers(Input_Layer)[0]
arcpy.env.addOutputsToMap = True
arcpy.AddMessage(lyr)

c = arcpy.charts.Histogram(ChartInfo_xField, title=ChartInfo_Title, binCount=ChartInfo_binCount, xTitle=ChartInfo_xTitle, yTitle=ChartInfo_yTitle, dataTransformationType=ChartInfo_TransformType, dataSource=lyr)
c.addToLayer(lyr)

 

Failing Model example (simplified): 

FailingModel.PNG

Any advice or tips are appreciated. Thank you in advance!

0 Kudos
3 Replies
TonyAlmeida
Frequent Contributor

Model builder in Pro is kind of clunky.  Although once you have your model created its runs smooth. Try creating a new model.

Also, take a look at derived output parameter.

https://pro.arcgis.com/en/pro-app/3.0/arcpy/geoprocessing_and_python/setting-script-tool-parameters....

JJH
by
Emerging Contributor

Thank you for the reply. The output is derived from the input layer in the parameters. I am hoping to not have to use a second model builder tool, I need to be able to apply the charts output within the same model that the layer is created. 

0 Kudos
JJH
by
Emerging Contributor

Update: So far, the only partial solution I have identified is hardcoding the predetermined output string name from the models as the input layer name into the code. This makes it so these models can only run with predetermined feature layers, but it does run successfully within the same model. 

 

import arcpy

Input_Layer = arcpy.GetParameter(0)
ChartInfo_Title = arcpy.GetParameterAsText(2)
ChartInfo_xField = arcpy.GetParameterAsText(3)
ChartInfo_binCount = arcpy.GetParameter(4)
ChartInfo_xTitle = arcpy.GetParameterAsText(5)
ChartInfo_yTitle = arcpy.GetParameterAsText(6)
ChartInfo_TransformType = arcpy.GetParameterAsText(7)
SelectLayer= "Output_Layer1"

aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps("Map")[0]
lyr = map.listLayers(SelectLayer)[0]
arcpy.env.addOutputsToMap = True
arcpy.AddMessage(lyr)

c = arcpy.charts.Histogram(ChartInfo_xField, title=ChartInfo_Title, binCount=ChartInfo_binCount, xTitle=ChartInfo_xTitle, yTitle=ChartInfo_yTitle, dataTransformationType=ChartInfo_TransformType, dataSource=lyr)
c.addToLayer(lyr)

 

 

 

0 Kudos