Adding Layer from Pywin to open mxd using Python

3015
8
Jump to solution
10-15-2015 01:21 PM
TobyJacobs1
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!

0 Kudos
1 Solution

Accepted Solutions
FreddieGibson
Occasional Contributor III

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
FreddieGibson
Occasional Contributor III

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.
TobyJacobs1
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...

0 Kudos
DanPatterson_Retired
MVP Emeritus

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

0 Kudos
TobyJacobs1
New Contributor II

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

0 Kudos
DanPatterson_Retired
MVP Emeritus

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?

0 Kudos
TobyJacobs1
New Contributor II

I'm assuming it was the data management tool

0 Kudos
DanPatterson_Retired
MVP Emeritus

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

0 Kudos
FreddieGibson
Occasional Contributor III

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

0 Kudos