I am attempting to use this code in a tool that I am working on:

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
XMIN = df.extent.XMin
YMIN = df.extent.YMin
XMAX = df.extent.XMax
YMAX = df.extent.YMax

I then go on to do other calculations with the extent. The code works perfectly in the Python window, but because Toolbox tools are run in separate processes I am unable to use "CURRENT".

I think I could probably add a derived input variable and pass the coordinates through the validation script, but that doesn't work for what I am doing. I need to be able to do this later on in the script and add an additional temporary feature to the current map:

points = arcpy.CreateFeatureclass_management("in_memory", "NewFeature", "POINT")
arcpy.AddField_management(points, "NewField", "TEXT", "", "", 20)

I know all of my code works right, because I can paste the entire .py into the Python window and then call Main() and run through it, but I need to share it with others in an easier way.

I would prefer the solution be compatible with 10.0 and 10.1 if at all possible, because some of the people I work with do not have 10.1 yet. I will accept a 10.1 only solution if I have to though.

What options do I have to do this?

I still can not get this to work, they have not gotten me Python Add-In Wizard yet so I am unable to test that approach. I have again tried the "Run in process" box checked and unchecked. Neither are working.

Checked gives me this: RuntimeError: CreateObject cannot open map document

Handled that exception: Object: CreateObject cannot open map document

From my testing looks like it is having the same error running in process or not.

I will still be attempting to do this within the Add-In Wizard, when they finally give me it.

The winning answer is I wasn't running it in the foreground.

  • 1
    I'm not clear on what you are trying to achieve but if it is to work out what map you currently have open try mxd.filePath. – PolyGeo May 18 '13 at 20:46
  • It would work if I wanted to do something on a specific mxd, and not keep the output later in_memory, but I want to be able to open a new arc map and run the tool. Instead of having a specific mxd. – eseglem May 18 '13 at 22:50
  • I think you will need to include more code (and pseudo code) in your question but my understanding is that your in_memory workspace is gone when you close ArcMap or complete a Python script which imports ArcPy. – PolyGeo May 18 '13 at 22:59
  • 2
    On your script tool, is run in-process checked? See Running a script in process. – blah238 May 19 '13 at 0:40
  • 3
    You should be running this tool in process, and in the foreground. ("current" doesnt work in the background as there isn't a map there to operate on) – KHibma May 22 '13 at 16:32

As commented by @KHibma:

You should be running this tool in process, and in the foreground. ("current" doesnt work in the background as there isn't a map there to operate on)

However, I think you will be better using ArcGIS Add-Ins for Python which only became available at ArcGIS 10.1.

  • That may be what I have to do. Do you happen to know if an add in I create with that wizard can work on 10.0 or is it purely 10.1? Sadly I work in a fairly mixed environment and need to maintain compatibility with both. – eseglem May 19 '13 at 0:50
  • 3
    Purely 10.1 as far as I know. – PolyGeo May 19 '13 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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