2

I'm running a python script from arccatalog and it works, however, I am trying to add layers to an mxd and need to declare the data frame. I keep getting this error message.

type 'exceptions.AttributeError': 'unicode' object has no attribute '_arc_object' Failed to execute (StepByStep)

Selection of my code is below.

# Import arcpy module
import arcpy
import sys    

# Licenses
arcpy.CheckOutExtension("3D")    
arcpy.CheckOutExtension("spatial")    
arcpy.OverwriteOutput = True

# Script arguments
arcpy.env.extent = "E:\\School_Test\\SchoolTest.gdb\\Extent"    
arcpy.env.cellSize = "3"

mxd = arcpy.GetParameterAsText(0)    
arcpy.env.scratchWorkspace = arcpy.GetParameterAsText(1)    
arcpy.env.workspace = arcpy.GetParameterAsText(2)    
spRef = arcpy.GetParameterAsText(3)    
Survey_Data = arcpy.GetParameterAsText(4)    
Survey_X = arcpy.GetParameterAsText(5)    
Survey_Y = arcpy.GetParameterAsText(6)    
Survey_Z = arcpy.GetParameterAsText(7)    
Sample_Data = arcpy.GetParameterAsText(8)
Sample_X = arcpy.GetParameterAsText(9)    
Sample_Y = arcpy.GetParameterAsText(10)    
Selection_Criteria = arcpy.GetParameterAsText(11)    
Site_Grid = arcpy.GetParameterAsText(12)

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

Below this I declare variables then geoprocessing.

Any suggestions?

1
  • 1
    If you are saying that when you run exactly that code from a Python script tool to see the error that you have reported, then I think you should add an arcpy.AddMessage(mxd) immediately before you try to create the DataFrame object, and then run it again to see if mxd is set to what you think.
    – PolyGeo
    Commented May 4, 2014 at 7:23

1 Answer 1

4

The problem with the code you have is that you are trying to convert an ArcMap Map Document into into a string and then you supply the string for the arcpy.mapping.ListDataFrames function. I usually handle this by:

1) Using the ArcMap Document data type for the input parameter. enter image description here

2) Referring to the mxd file path as an Map Document object.

 mxd_raw = arcpy.GetParameterAsText(0) 
 mxd = arcpy.mapping.MapDocument(mxd_raw)

Your code after you've got access to the map document object could be:

mxd_raw = arcpy.GetParameterAsText(0)
mxd = arcpy.mapping.MapDocument(mxd_raw)
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    arcpy.AddMessage(lyr.name)
3
  • 3
    I've found the ArcMap Document datatype to be a bit flaky, I tend to prefer using the File datatype and set its extension filter to .mxd/.mxt. But yeah, user cannot pass a string to ListLayers, must pass MapDocument object. Commented May 4, 2014 at 15:27
  • Thanks! I used the top comment and it resolved the issue.
    – user29866
    Commented May 5, 2014 at 4:31
  • 1
    to refine @JasonScheirer's comment the file filter syntax is mxd; mxt (semicolon delimiter, no dots) Commented Feb 25, 2015 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.