It's unclear from your question what your actual problem is, but I can see one issue in the code...
In ArcGIS Pro, updating the map's camera extent, doesn't update the camera extent of the map frame in the layout.
When I'm writing a script tool that should update the map or map frame, I try to cater for which ever of the two is the active view at the time like this:
activeView = aprx.activeView
try:
activeView.camera.getExtent()
activeMapView = activeView
except:
try:
activeLayoutMapFrames = activeView.listElements("mapframe_element")
if len(activeLayoutMapFrames) == 1:
activeMapView = activeLayoutMapFrames[0]
except:
activeMapView = None
Let's step through what this does:
- The
activeView
is which ever view is currently active in ArcGIS Pro. This could be a map view or a layout view. We don't know yet.
activeView.camera.getExtent()
will fail if this is not a map view (and therefore it is probably a layout view, and not a map view). If it succeeds, then we have found an active map view in activeView
.
- If the above fails, then
activeView.listElements("mapframe_element")
will fail if this is not a layout view. If it succeeds, then we have found an active layout view, and we check it for the number of map frames. If there is only one map frame, use this as the active map view (it's not really a map view as such, but you can still still run the activeMapView.camera.setExtent()
on it later on).
In your case, you are not using a CURRENT
aprx, so the idea of an activeView
is not relevant. But the principle is still the same. You could change your code to something like the following (UNTESTED!) in order to change the camera extent on your map frame, instead of on your map.
# Read in project
aprx = arcpy.mp.ArcGISProject("path/to/project.aprx")
m = aprx.listMaps()[0]
fc = "path/to/poly.shp"
# add layer
# lyr = arcpy.management.MakeFeatureLayer(fc, os.path.basename(fc)[:-4])
lyr = m.addDataFromPath(fc)
lyt = aprx.listLayouts("Layout")[0]
# This assumes that the layout has exactly one map frame (or that the map frame you want to change is the first map frame)
mapFrame = lyt.listElements("mapframe_element")[0]
# Set extent to layer, then save!
# using 'camera' not 'defaultCamera' for the map frame
mapFrame.camera.setExtent(arcpy.Describe(lyr).extent)
lyt.exportToJPEG("./temp.jpg")
If this does not resolve the issue, then you need to be a lot more specific regarding what the problem actually is.