mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "*")[0] LayerNeedsFieldsTurnedOff = arcpy.mapping.ListLayers(mxd, "Layer Name", df)[0] desiredFields = ["LAST","FIRST","CITY","ZIP"] # This makes the field_info object that can then be applied to a layer field_info = arcpy.Describe(LayerNeedsFieldsTurnedOff).fieldInfo for index in xrange(0, field_info.count): if field_info.getfieldname(index) not in engineFields: field_info.setvisible(index,"HIDDEN")
arcpy.MakeFeatureLayer_management(LayerNeedsFieldsTurnedOff,"temp_layer","","",field_info)
Solved! Go to Solution.
I wasn't entirely satisfied with the following code, but seems to do the trick - it comes with the caveat that you have to 'manually' code other things like labeling. Maybe I am missing something but found that since I 'stamped' what seems to be a new layer from the MFL execution to include the field_info obj, it dropped my labeling. It also wrote 'temp_layer' in the Description property. But my layer is written with at least the correct field_info settings.
import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.addOutputsToMap = False
mxd = arcpy.mapping.MapDocument(r'your path to target mxd')
df = arcpy.mapping.ListDataFrames(mxd, '*')[0]
LayerNeedsFieldsTurnedOff = arcpy.mapping.ListLayers(mxd, 'your target layer name', df)[0]
# fill in your desired fields to remain visible
desiredFields = ['fieldname0', 'fieldname1', 'fieldname2', 'etc']
field_info = arcpy.Describe(LayerNeedsFieldsTurnedOff).fieldInfo
for i in range(field_info.count):
if field_info.getfieldname(i) not in desiredFields:
field_info.setvisible(i, 'HIDDEN')
arcpy.MakeFeatureLayer_management(LayerNeedsFieldsTurnedOff, 'temp_layer', '', '', field_info)
refLyr = arcpy.mapping.Layer('temp_layer')
# rename the ref layer the same as your target layer
refLyr.name = 'your target layer name'
arcpy.ApplySymbologyFromLayer_management(refLyr, LayerNeedsFieldsTurnedOff)
arcpy.mapping.UpdateLayer(df, LayerNeedsFieldsTurnedOff, refLyr, False)
mxd.save()
# clean up
if arcpy.Exists('temp_layer'):
print '\'temp_layer\' still in memory...deleting now...'
arcpy.Delete_management('temp_layer')
print 'deleting obj refs...'
del mxd,LayerNeedsFieldsTurnedOff, refLyr
print 'done.'
Just a further note - seems you could 'mine' properties from a lyr library or another mxd (or such library) to more easily 'reapply' these properties, for instance, like labeling.
-Wayne
arcpy.MakeFeatureLayer_management(LayerNeedsFieldsTurnedOff,"temp_layer","","",field_info)
tempLayer = arcpy.mapping.Layer("temp_layer") arcpy.mapping.UpdateLayer(df,arcpy.mapping.Layer(LayerNeedsFieldsTurnedOff, tempLayer)
I wasn't entirely satisfied with the following code, but seems to do the trick - it comes with the caveat that you have to 'manually' code other things like labeling. Maybe I am missing something but found that since I 'stamped' what seems to be a new layer from the MFL execution to include the field_info obj, it dropped my labeling. It also wrote 'temp_layer' in the Description property. But my layer is written with at least the correct field_info settings.
import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.addOutputsToMap = False
mxd = arcpy.mapping.MapDocument(r'your path to target mxd')
df = arcpy.mapping.ListDataFrames(mxd, '*')[0]
LayerNeedsFieldsTurnedOff = arcpy.mapping.ListLayers(mxd, 'your target layer name', df)[0]
# fill in your desired fields to remain visible
desiredFields = ['fieldname0', 'fieldname1', 'fieldname2', 'etc']
field_info = arcpy.Describe(LayerNeedsFieldsTurnedOff).fieldInfo
for i in range(field_info.count):
if field_info.getfieldname(i) not in desiredFields:
field_info.setvisible(i, 'HIDDEN')
arcpy.MakeFeatureLayer_management(LayerNeedsFieldsTurnedOff, 'temp_layer', '', '', field_info)
refLyr = arcpy.mapping.Layer('temp_layer')
# rename the ref layer the same as your target layer
refLyr.name = 'your target layer name'
arcpy.ApplySymbologyFromLayer_management(refLyr, LayerNeedsFieldsTurnedOff)
arcpy.mapping.UpdateLayer(df, LayerNeedsFieldsTurnedOff, refLyr, False)
mxd.save()
# clean up
if arcpy.Exists('temp_layer'):
print '\'temp_layer\' still in memory...deleting now...'
arcpy.Delete_management('temp_layer')
print 'deleting obj refs...'
del mxd,LayerNeedsFieldsTurnedOff, refLyr
print 'done.'
Just a further note - seems you could 'mine' properties from a lyr library or another mxd (or such library) to more easily 'reapply' these properties, for instance, like labeling.
-Wayne