Select to view content in your preferred language

Script Tool Help! df = arcpy.mapping.ListDataFrames(mxd)[0] ISSUE

3867
3
03-28-2013 05:30 PM
courtneygarlock
Emerging Contributor
I'm trying to create a script tool but when I run the tool I keep getting and error message: I'm not sure what the problem is with
the  df = arcpy.mapping.ListDataFrames(mxd)[0]

Traceback (most recent call last):
  File "C:\Users\Courtney\Desktop\ScriptTool.py", line 14, in <module>
    df = arcpy.mapping.ListDataFrames(mxd)[0]
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\mapping.py", line 1478, in ListDataFrames
    result = mixins.MapDocumentMixin(map_document).listDataFrames(wildcard)
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 727, in listDataFrames
    return list(reversed(list(self.dataFrames)))
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 694, in dataFrames
    return map(convertArcObjectToPythonObject, self.pageLayout.dataFrames)
  File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\mixins.py", line 678, in pageLayout
    return convertArcObjectToPythonObject(self._mxd._arc_object.pageLayout)
AttributeError: 'unicode' object has no attribute '_arc_object'

Failed to execute (ScriptTool).


MY SCRIPT IS AS FOLLOWS:

#Import Modules
import arcpy
from arcpy import env
import datetime
import os

arcpy.env.overwriteOutput = True

#Define Variables
mxd = arcpy.mapping.MapDocument = arcpy.GetParameterAsText(0)
env.workspace = arcpy.GetParameterAsText(1)
df = arcpy.mapping.ListDataFrames(mxd)[0]



#Define variables
featureClass = arcpy.GetParameterAsText(2)
layer = "AISlayer"  #layer Name
OutlyrFile = r"C:\Courtney\Jan1AIS.lyr"   
addLayer = arcpy.mapping.Layer(OutlyrFile)

#Make a layer from the feature class
arcpy.MakeFeatureLayer_management(featureClass , layer) #This creates a temporary layer in memory
#Save the layer in memory to layer
arcpy.SaveToLayerFile_management(layer, OutlyrFile) #This saves the layer to the disk

#Check to see if layer supports time and if time properties are set
lyr = addLayer
if lyr.supports("TIME"):
    lyrTime = lyr.time
    if lyr.time.isTimeEnabled:
        LYRstartTime = lyrTime.startTime
        LYRendTime = lyrTime.endTime
        timeDelta = LYRendTime - LYRstartTime
        print "Start Time: " + str(LYRstartTime)
        print "End Time: " + str(LYRendTime)
        print "Time Extent: " + str(timeDelta)
    else:
        print "No time properties have been set on the layer"
        #Enables time on a layer with another already time enabled layer
        #Do this within the else statement because you only want to enable time if it isn't already
        #df = arcpy.mapping.ListDataFrames(mxd)[0]
        #lyr = arcpy.mapping.ListLayers(mxd, "Jan1AIS", df)[0]
        timeFile = arcpy.mapping.Layer(r"C:\Courtney\TimeLayer.lyr") #originally authored in ArcMap                                                              
        arcpy.mapping.UpdateLayerTime(df, lyr, timeFile)             #refrence in toolbox, SAVE ZIP, navigate to file
        print "Time has been enabled."
else:
    print "Time is not supported on this layer"

#Add layer to the data frame
arcpy.mapping.AddLayer(df, addLayer)
#Save the mxd
#mxd.save()


lyr = arcpy.mapping.ListLayers(mxd, layer, df)[0]
for df in arcpy.mapping.ListDataFrames(mxd):
    #zoom to to the extent of the selected features for a specific layer
    df.extent = lyr.getSelectedExtent()
    #Set mape scale...each data frame's rotation property to 0 and scale property to 1:20,000,000
    df.rotation = arcpy.GetParameterAsText(3)    #0
    df.scale = arcpy.GetParameterAsText(4)      #20000000
    mxd.activeView = 'PAGE_LAYOUT'

inFolder = arcpy.GetParameterAsText(5)


df.time.currentTime = arcpy.GetParameterAsText(6)          #datetime.datetime(2009, 01, 1, 00, 00)
endTime = arcpy.GetParameterAsText(7)                 #datetime.datetime(2009, 01, 1, 23, 59)
interval = arcpy.GetParameterAsText(8)             #arcpy.time.EsriTimeDelta(1, 'hours')
                                                    #or #interval = datetime.timedelta(hours=1)
Tags (2)
0 Kudos
3 Replies
curtvprice
MVP Alum
I'm trying to create a script tool but when I run the tool I keep getting and error message: I'm not sure what the problem is with the  df = arcpy.mapping.ListDataFrames(mxd)[0]


Your mxd variable wasn't successfully set to a map document object, it's a unicode string (ie the path to the mxd as a text string).

Try changing this

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

to this

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

ps Please follow [thread=48475]these instructions[/thread] to post/edit Python code in the forum.
0 Kudos
courtneygarlock
Emerging Contributor
Thank you,

Are you saying any variable set to a string in the original script needs to have this format?

I read how to post but I was kind of confused. Sorry for just cutting and pasting.
0 Kudos
curtvprice
MVP Alum
Are you saying any variable set to a string in the original script needs to have this format?


No, this is specific to the function arcpy.mapping.MapDocument(). There is a code sample in the help.

Desktop Help 10.1: Map Document

I read how to post but I was kind of confused.


Pretty simple really, just paste your code into your forum post and surround it with these tags


[noparse]
# my code goes here
[/noparse]
0 Kudos