Select to view content in your preferred language

mp.ArcGISProject.save() crash

181
1
Jump to solution
06-08-2024 10:35 AM
kradijaf
New Contributor III

Got a script which does the same operation that includes opening, altering and saving arcpy.mp.ArcGISProject object. Prior to editing the worflow inside the ArcGIS Pro project all iterations of the workflow finished without a problem. After changes which shouldn´t cause issues, the project workflow crashes at the end (arcpy.mp.ArcGISProject.save()) of 2nd iteration causing an OSError. All the names are fine, inspected the code in big detail. Made a new ArcGIS Pro project, reinstalled ArcGIS Pro, same result. Neither deleting the arcpy.mp.ArcGISProject variable after using its save() method didn´t help.

 

The function called multiple times per script causing the crash:

def to_arcpy(positioned, face_num):
    # Xm, Ym, Xp, Yp, XC, YC, XB, YB
    positioned = [i * (180 / np.pi) for i in positioned]        # convert to deg
    p = ac.mp.ArcGISProject(r"outputProj\project.aprx")        # a group for upcoming map layers
    group_n = f'face{face_num}'

    if face_num in (1, 2, 3, 4, 5, 11):             # gnomonic polar north
        sr = ac.SpatialReference(102034)
    else:
        sr = ac.SpatialReference(102036)            # gnomonic polar south          

    m = p.listMaps(group_n)[0]
    geoms = []
    sr = ac.SpatialReference('WGS 1984')
    clipper = rf'outputProj\project.gdb\boundary_{face_num}'

    line = ac.Array()
    for i in range(len(positioned[6])):         
        line.add(ac.Point(positioned[7][i], positioned[6][i]))

    line.add(line[0])
    ac.CopyFeatures_management(ac.Polygon(line, sr), clipper)

    pairs1 = ((positioned[0], positioned[1]), (positioned[2], positioned[3]))        # Xm, Ym, Xp, Yp

    for i in range(len(pairs1)):
        shp = pairs1[i][0].shape    
        lines_out = []

        for j in np.arange(shp[0]):
            line = ac.Array()

            for k in np.arange(shp[1]):
                line.add(ac.Point(pairs1[i][1][j, k], pairs1[i][0][j, k]))

            lines_out.append(ac.Polyline(line, sr))
            
        geoms.append(lines_out)

    names = ('d', 's')
    for i in range(2):
        ac.Clip_analysis(geoms[i], clipper, rf'outputProj\project.gdb\{names[i]}_{face_num}') 

    names = ('boundary', 'd', 's')
    sym_names = ('boundary', 'graticule', 'graticule')

    for i in range(3):
        layer_n = f'{names[i]}_{face_num}'
        layer_loc = os.path.join(os.getcwd(), rf'outputProj\project.gdb\{layer_n}')
        layer_loc = rf'{layer_loc[0].upper()}{layer_loc[1 : ]}'

        m.addDataFromPath(layer_loc)
        sym_m = p.listMaps('symbology')[0]
        ac.ApplySymbologyFromLayer_management(m.listLayers(layer_n)[0], sym_m.listLayers(sym_names[i])[0])

    boundary = m.listLayers('boundary*')[0]
    m.moveLayer(m.listLayers()[0], boundary)
    m.clipLayers(boundary)
    p.save()
    l = p.listLayouts()[0]
    mf = l.listElements('mapframe_element', group_n)[0]
    mf.camera.setExtent(ac.Describe(boundary).extent)

    p.save()
    del p
1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

So, in my experience, I normally get the OS error when someone else (even me) is using that APRX. This includes both arcpy.mp.ArcGISProject("path")/arcpy.mp.ArcGISProject("path") and also arcpy.mp.ArcGISProject("path")/arcpy.mp.ArcGISProject("CURRENT").

Is it possible that you've opened that APRX already outside of the function you shared here?

If you have, you might want to consider passing the APRX object into the function instead.

View solution in original post

0 Kudos
1 Reply
AlfredBaldenweck
MVP Regular Contributor

So, in my experience, I normally get the OS error when someone else (even me) is using that APRX. This includes both arcpy.mp.ArcGISProject("path")/arcpy.mp.ArcGISProject("path") and also arcpy.mp.ArcGISProject("path")/arcpy.mp.ArcGISProject("CURRENT").

Is it possible that you've opened that APRX already outside of the function you shared here?

If you have, you might want to consider passing the APRX object into the function instead.

0 Kudos