Adding Layer from Pywin to open mxd using Python

2363
8
Jump to solution
10-15-2015 01:21 PM
Highlighted
New Contributor II

Trying to automate some editing tasks and then exporting the files to pdf's. I'm getting stuck with loading the layers into ArcMap.

I can do it in the Python window using the standard:

arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

newlayer = arcpy.mapping.Layer(r"H:\2.shp")

arcpy.mapping.AddLayer(df, newlayer,"TOP")

but when I try to do it by referencing the mxd path instead of "CURRENT" in Pywin or a script tool (see below), it runs through but doesn't actually load the shapefile onto the map:

arcpy.mapping.MapDocument(r"H:\map2.mxd")

df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

newlayer = arcpy.mapping.Layer(r"H:\2.shp")

arcpy.mapping.AddLayer(df, newlayer,"TOP")

I've tried the second code in the Arc Python window, and it doesn't work there either. Any ideas? I'm sure it's a simple issue...

Thanks!

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Regular Contributor II

Can you try running the following and let me know if this works for you?

import arcpy

mxd = arcpy.mapping.MapDocument(r"H:\map2.mxd")

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

lyr = arcpy.management.MakeFeatureLayer(r"H:\2.shp").getOutput(0)

arcpy.mapping.AddLayer(df, lyr, "TOP")

I would say you'd want to note two things here.

  1. You should avoid using arcpy.Layer against the shapefile directly. This method is intended to be used against layer files (i.e. files that end in .lyr) as noted in the documentation.

    layer.png
  2. You will only see the layers added to an open map document if you run the code within ArcMap and utilize the CURRENT keyword. Otherwise you will need to run the code, save the map via the code, and then open the map document on the system to see the changes.

View solution in original post

8 Replies
Highlighted
Regular Contributor II

Can you try running the following and let me know if this works for you?

import arcpy

mxd = arcpy.mapping.MapDocument(r"H:\map2.mxd")

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

lyr = arcpy.management.MakeFeatureLayer(r"H:\2.shp").getOutput(0)

arcpy.mapping.AddLayer(df, lyr, "TOP")

I would say you'd want to note two things here.

  1. You should avoid using arcpy.Layer against the shapefile directly. This method is intended to be used against layer files (i.e. files that end in .lyr) as noted in the documentation.

    layer.png
  2. You will only see the layers added to an open map document if you run the code within ArcMap and utilize the CURRENT keyword. Otherwise you will need to run the code, save the map via the code, and then open the map document on the system to see the changes.

View solution in original post

Highlighted
New Contributor II

Thanks for the feedback, but that doesn't seem to solve the issue. It looks

like others had this issue in 10.0, but I'm using 10.3. Seems like a major

bug...

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

on a lark, could you place the file in a folder rather than in the root directory

Reply
0 Kudos
Highlighted
New Contributor II

Yes, I tried moving it all over the place with no change in result

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

Can't find arcpy.mapping.MakeFeature layer ... there is a Layer property  in the arcpy.mapping classes​ ​and no reference in the arcpy.mapping functions​ can you provide a link or are you referring to the datamanagement tool of the same name?

Reply
0 Kudos
Highlighted
New Contributor II

I'm assuming it was the data management tool

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

I was just curious whether Freddie's code worked... and order for it to persist, the layer would have to be made permanent since it won't be saved otherwise 

http://desktop.arcgis.com/en/desktop/latest/tools/data-management-toolbox/make-feature-layer.htm

Reply
0 Kudos
Highlighted
Regular Contributor II

Sorry about that Dan. That was a typo on my part. It should say arcpy.management.MakeFeatureLayer

Reply
0 Kudos