Select to view content in your preferred language

Renaming layers in an open mxd

2553
1
09-10-2010 03:16 AM
JohanSandell_Bull-Njaa
Emerging Contributor
Hi everyone

I'm trying to rename the layers in an open ArcMap session. When I run the script below it renames the layers but then they are removed. If I close the mxd without saving it and reopen it the layers are there and are renamed. If you rename one layer with the following code it works and the layer is not removed:
arcpy.mapping.ListLayers(mxd)[0].name = ???New Layer Name???

Am I doing something wrong here or is this somekind of bug.

I appreciate all the help I can get


BR
Johan Sandell


CODE:
import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

for lyr in arcpy.mapping.ListLayers(mxd,"", df):
if lyr.isFeatureLayer:
        tempName = lyr.name.replace('_',' ')
        lyr.name = tempName
arcpy.RefreshTOC()
del mxd
0 Kudos
1 Reply
JohanSandell_Bull-Njaa
Emerging Contributor
Hi again

I might have to clarify this.

The script that I talked about in the previous post is run in a toolbox, in ArcGIS 10. The script renames the layers in the TOC by removing the _ from the layer name, but it doesn't matter what it renames lyr.name to I can't get it to work. As I said before in the last post the layers are removed after renaming them. If you run the code in the new python window in ArcMap it works.

Have anyone experienced the same problem or have any solutions to it.


Johan
0 Kudos