I wrote a script that changes the field visibility settings through the CIM. The version I wrote as a script you paste into the Python window in Pro works fine. But the version I wrote to run from a tool box does not, and I'm positive that the settings in the CIM are correct.
The weird thing is, I have written scripts that change CIM settings from a toolbox script before, and they worked. However, they were changing the symbology, rather than field visibility. Not sure if that would cause the bug. The only other thing I can think of is that the layer I'm changing is the input layer, whereas my other scripts added a new layer and changed that CIM.
Anyone else have any ideas? Does this have anything to do with derived output parameters? I still don't understand what those are, even after reading about them multiple times.
Script and settings file screenshot included for good measure...
import pandas as pd
layer = arcpy.GetParameter(0)
view_type = arcpy.GetParameter(1)
field_settings_path = arcpy.GetParameterAsText(2)
settings = {}
settings['All Fields'] = []
settings_df = pd.read_excel(field_settings_path, view_type)
for visibility_setting in ['Visible Fields', 'Invisible Fields']:
fields_df = settings_df.loc[settings_df[visibility_setting].notnull(), [visibility_setting]]
field_list = list(fields_df[visibility_setting])
settings[visibility_setting] = field_list
settings['All Fields'] += field_list
fields = arcpy.ListFields(layer)
fields = [x.name for x in fields]
missing_fields = set(fields).difference(set(settings['All Fields']))
if len(missing_fields) > 0:
arcpy.AddError("!!!You are missing these fields from your {} settings:\n{}".format(SETTING_TYPE, '\n'.join(missing_fields)))
any_field = arcpy.ListFields(layer)[0]
arcpy.management.AlterField(layer, any_field.name) # populates CIM with all field objects
cim = layer.getDefinition('v3')
fieldDescriptions = cim.featureTable.fieldDescriptions
my_fields = {}
for field in fieldDescriptions:
my_fields[field.fieldName] = field
new_field_list = []
for name in settings['Visible Fields']:
field_object = my_fields[name]
field_object.visible = 'true'
new_field_list.append(field_object)
for name in settings['Invisible Fields']:
field_object = my_fields[name]
field_object.visible = 'false'
new_field_list.append(field_object)
### this printout confirms that I have the correct settings on the fields
for x in new_field_list:
arcpy.AddMessage("{}: {}".format(str(x.fieldName), str(x.visible)))
cim.featureTable.fieldDescriptions = new_field_list
layer.setDefinition(cim)
arcpy.AddMessage('All Done!')