ArcPy ListDataFrames on empty MXD crash

1089
13
05-29-2012 09:27 AM
AndrewLaux
New Contributor II
Hello,

I have a script that iterates through multiple directories and runs a report on mxd information. Running into problems with it however whenever an empty mxd is loaded.

Whenever running " DFList = arcpy.mapping.ListDataFrames(mxd)" I get a crash of the python script .

Is there any way to test for an empty mxd or perhaps some other problem that i'm missing here.

Andrew L.
Tags (2)
0 Kudos
13 Replies
MathewCoyle
Frequent Contributor
Even an empty mxd should have a dataframe. The line as you show it runs for me without crashing. Are you sure something else isn't going on? A layer index reference maybe?
0 Kudos
HugoAhlenius
Occasional Contributor
Maybe you need to check what the 'mxd' variable contains!
0 Kudos
FabianBlau
Occasional Contributor II
Please show us the error message and a little more code (using the CODE-Tags).
It would be easier to understand the problem.
Often print (or AddMessage) helps.
For example:
arcpy.AddMessage(mxd)
DFList = arcpy.mapping.ListDataFrames(mxd)
0 Kudos
JoshuaChisholm
Occasional Contributor III
I would suggest using a "try/except" statement in python. I wasn't able to duplicate your crash, but if you're just getting a error message, this will probably work:
mxd = arcpy.mapping.MapDocument("C:\\MappingStuff\\Untitled.mxd")
try:
 DFList = arcpy.mapping.ListDataFrames(mxd)
except:
 print "The MXD at "+mxd.filePath+" is empty or layers could not be listed."
 arcpy.AddMessage("The MXD at "+mxd.filePath+" is empty or layers could not be listed.")
0 Kudos
David_JAnderson
New Contributor III
I am having this same problem with ArcGIS 10.1 and Python 2.7.  Doing a arcpy.mapping.ListDataFrames will crash python if the mxd is either empty or all of the layers inside the data frame are broken.  A try/except block does not catch this error.  I get a IDLE window RESTART when this happens in while running a script.  Trying to acces the activeDataFrame property of the map document will also crash python.  The method arcpy.mapping.ListBrokenDataSources() also crashes when all of the data sources are broken.  I can open the file in ArcMap 10.1 to see that all of the data sources are broken.

**Update
The command does work inside of command line window started from ArcCatalog 10.1.
0 Kudos
RhettZufelt
MVP Frequent Contributor
Have you applied service pack 1?

I don't have a copy without SP1 to test, but, with 10.1, service pack 1 on an empty mxd OR an mxd with all the data sources broken, I can run both these commands without error.

Tried it on two different computers, works just fine in both.

R_
0 Kudos
David_JAnderson
New Contributor III
I have applied service pack 1.  I also tested again a ArcGIS 10.0 w/ sp 4.  Where I get the same results.
Did you use an IDLE/Pythonwin to test from or a command line from Arcmap/catalog.  I have determined that the problem only occurs when running from a stand alone python window.  It works fine from a arcmap/catalog command line.
This is not the first time I have encountered an issue where something fails in an external python window but works inside of arc.
0 Kudos
RhettZufelt
MVP Frequent Contributor
I do everything in the stand alone IDLE that comes with the default install.

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> doc = r"D:\test.mxd"
>>> import arcpy
>>> mxd = arcpy.mapping.MapDocument(doc)
>>> DFList = arcpy.mapping.ListDataFrames(mxd)      ### first with just empty mxd
>>> del mxd
>>> mxd = arcpy.mapping.MapDocument(doc)
>>> DFList = arcpy.mapping.ListDataFrames(mxd)
>>> arcpy.mapping.ListBrokenDataSources(mxd)       ### then added windrosepoints, deleted the dataset, ensured it was broken, this is result.
[<map layer u'windrosepoints'>]
 

R_

Only tested with empty and with ONE broken dataset.


This is not the first time I have encountered an issue where something fails in an external python window but works inside of arc.


I have found just the opposite of this.  That is why I run everything externally.....
0 Kudos
David_JAnderson
New Contributor III
I tried that test of creating a mxd then breaking the data source.  The listdataframes does work.  Perhaps a salient fact is that the mxd that I am trying to work with is rather old, version 8.1 I believe.  Which is why I am working with it as I know it is broken because the file system has changed since those days.  I'm trying to figure out how bad the problem is which is one of the reasons for the script.
0 Kudos