Problem: activeView always None

1918
9
Jump to solution
02-19-2022 05:08 PM
AndyAnderson
Occasional Contributor II

Running ArcGIS Pro 2.9.1 on Windows 10 Enterprise v. 1607 (OS Build 14393.4886), also Windows 10 v. 20H2 (OS Build 19042.1526).

Problem: activeView always returns None. Steps to reproduce:

1) Create new Map project.

2) Open Notebook or Python Window

3) Include this code taken from https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/mapview-class.htm :

import arcpy, os

aprx = arcpy.mp.ArcGISProject('current')
mv = aprx.activeView


# Change the extent of the map view
ext = mv.camera.getExtent()

4) Result:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
In  [16]:
Line 7:     ext = mv.camera.getExtent()

AttributeError: 'NoneType' object has no attribute 'camera'
--------------------------------------------------------------------------- 

aprx.activeMap works, as does aprx.activeMap.defaultView, but I'm pretty limited if I can't manipulate the camera!

The docs say "The activeView property is intended for scripts that are to be executed in the ArcGIS Pro application, such as scripts run in the Python pane or those associated with a script tool", which is what I'm trying. What am I missing?

Thanks,

-- Andy

0 Kudos
1 Solution

Accepted Solutions
AndyAnderson
Occasional Contributor II

I found confirmation of this issue here:

https://community.esri.com/t5/python-questions/zoom-to-layer-on-the-map-view-arcpy-mp/m-p/1147618/hi...

The problem is that a Notebook is also a "view", and is technically the active view (even though it still shows null). If the code is in a script file, the correct active view is returned.

The solution is in version 3.0 of ArcGIS Pro, forthcoming at this date. It will correctly provide the active view, whether a map or a layout, and provide some additional methods for maps and layouts to open views for themselves.

-- Andy

View solution in original post

0 Kudos
9 Replies
DanPatterson
MVP Esteemed Contributor

Camera—ArcGIS Pro | Documentation

MapView—ArcGIS Pro | Documentation

The second link sounds like your situation


... sort of retired...
0 Kudos
AndyAnderson
Occasional Contributor II

Yes, the second link is where I'm getting the sample code that doesn't work. I also just tried it with ArcGIS Pro 2.8.3, and it failed there, too.

Is there some other way to access the active MapView or a list of MapViews in a project? The ArcGISProject object provides methods to access a list of Maps and a list of Layouts, but only the latter can access Cameras, because they know about their MapFrames (analogous to MapViews) and the latter know about their Cameras.

-- Andy

0 Kudos
DanPatterson
MVP Esteemed Contributor

I don't think anything has changed from the limitations posted in the second link and those posted by esri staff in this link

Solved: Zoom to layer on the map view arcpy.mp - Esri Community

 


... sort of retired...
0 Kudos
AndyAnderson
Occasional Contributor II

Thanks for that link, it didn't show up when I searched for possible solutions.

And from my reading, I'm not subject to the limitations described because I am running my code within "the application", that is ArcGIS Pro.

-- Andy

0 Kudos
DanPatterson
MVP Esteemed Contributor

 No clue then because if I have a mapview selected/activated and run it using a notebook or the python window run from within Pro, there are no issues.  Outside of Pro, it doesn't work, but I have a standalone install and not subject to any other constraints (portal and the lot)


... sort of retired...
0 Kudos
AndyAnderson
Occasional Contributor II

How are you selecting/activating a MapView?

0 Kudos
DanPatterson
MVP Esteemed Contributor

try an alternate way... you have to know which view or its index you want

import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
m0 = aprx.listMaps()[0]
c = m0.defaultView.camera
ext = c.getExtent()
print(ext)
299996.388450461 4999999.22702228 300015.611549539 5000015.77297772 NaN NaN NaN NaN
dir(c)
['X', 'Y', 'Z', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
 '__eq__', '__format__', '__from_scripting_arc_object__', '__ge__',
 '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__',
 '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__',
 '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
 '__subclasshook__', '__weakref__', '_arc_object', 'getExtent', 'heading',
 'mode', 'pitch', 'roll', 'scale', 'setExtent']

... sort of retired...
0 Kudos
AndyAnderson
Occasional Contributor II

Yeah, that's the default view, which is a  property of the Map, and apparently loaded when you open a map. I can do this:

extent = arcpy.Extent(-74,42,-72,44)
c.setExtent(extent)
print(c.getExtent())

-74 42 -72 44 NaN NaN NaN NaN

But that's ignored -- the MapView is not updated. Or, if I close the map and reopen it, the defaultView is reset to Esri's default (and why is that the US? Inquiring minds want to know!).

Thanks for the suggestions!

-- Andy

0 Kudos
AndyAnderson
Occasional Contributor II

I found confirmation of this issue here:

https://community.esri.com/t5/python-questions/zoom-to-layer-on-the-map-view-arcpy-mp/m-p/1147618/hi...

The problem is that a Notebook is also a "view", and is technically the active view (even though it still shows null). If the code is in a script file, the correct active view is returned.

The solution is in version 3.0 of ArcGIS Pro, forthcoming at this date. It will correctly provide the active view, whether a map or a layout, and provide some additional methods for maps and layouts to open views for themselves.

-- Andy

0 Kudos