Select to view content in your preferred language

How can I set the "Description" parameter of a layer with Python?

1145
7
04-18-2011 04:50 AM
MarcFasel
Deactivated User
Hello, I need to make a package of layers in a Python script. The "PackageLayer" tool required that the layers have a description in order to run, but the layers I have to pack are created and saved on disk just before the packaging. Thus, I can't set a description manually. There is a way to set it directly in Python? (or to avoid it as I don't need any description)

Thanks for your help

Marc
Tags (2)
0 Kudos
7 Replies
AndrewChapkowski
Esri Regular Contributor
You need to create a layer object and add the then pass a description text as a string to the layer property.
You can try something like this:
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    lyr.description = "some text"
    lyr.save()

del mxd

See the layer properties here: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s300000008000000.htm for more information.
0 Kudos
MarcFasel
Deactivated User
Hello, thank you for your answer! Does anyone know if it's possible to define the parameter when saving a layer object to a file (like in the code below)?

[...]

layer = arcpy.MakeFeatureLayer_management(clippedFC, clippedFCLayer)
layer.description = "N/A"
arcpy.SaveToLayerFile_management(layer, clippedFCLayer, "RELATIVE")

[...]


In fact, in my case, I'd like to set the description to "N/A" for all .lyr files in a given directory (either when creating the layer files or after they have been created) for packing them.

Marc
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You could manually create an empty MXD called 'Test.mxd'.  Then you can use python to add all the layer files from the directory, update the description, and delete MXD when you're finished.  Ex:

import arcpy, os
from arcpy import env
arcpy.env.overwriteOutput = True

env.workspace = "<lyr file workspace>"

mxd = arcpy.mapping.MapDocument(r"C:\temp\Test.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]

listFiles = arcpy.ListFiles("*.lyr")
for file in listFiles:
    addLayer = arcpy.mapping.Layer(file)
    arcpy.mapping.AddLayer(df, addLayer)

arcpy.RefreshTOC()
arcpy.RefreshActiveView()

for lyr in arcpy.mapping.ListLayers(mxd):
    lyr.description = "N/A"
    lyr.saveACopy(str(lyr))

del mxd

os.remove(<'PathToMXD>')
0 Kudos
FrankRoberts
Frequent Contributor
I'm having some odd behavior with writing to the description of the layer in a mxd.  I've used the code below to write to the description:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Data\GISData\GDG\GrabDataPy\test.mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
    print
    print lyr.name
    print lyr.description
    lyr.description = "some text"
    
mxd.save()
del mxd


I can see based on the code above print statement and the date/time on the mxd that when I run the above code the mxd is updated.  Since if I run this code a 2nd time, the print shows the new values, and the time changes on the file stamp of the mxd to when the code was last run.    However, when I open the mxd in ArcMap 10.0 sp3, the change to the description is not present.  It just has what was in it originally.

Ideas??
0 Kudos
FrankRoberts
Frequent Contributor
On further testing, I have noticed that if I copy the mxd to a different machine, that I can see the changes. I just can't see the changes in ArcMap on the machine the python was ran from. Very strange.....
0 Kudos
AndrewChapkowski
Esri Regular Contributor
Did you try refreshing the TOC or Refreshing the Activeview before viewing the description?
0 Kudos
FrankRoberts
Frequent Contributor
Andrew, thank for chiming back in.  I'm running the python code from the Command Prompt window (aka dos prompt).  I have rebooted my machine and now it seems to see the changes after running the script.  Not sure what was buggered up...  I did swap in the code per your recommendation and ran it with it and without it and it didn't seem to make a difference.  At least now it is working as it should.

thanks!
0 Kudos