I am working on a standalone ArcPy script running FULLY out of the ArcGIS Pro application and I am running into an issue regarding the implementation of a scalebar. Currently, my script can add a scalebar however the scalebar says 0 KM and is a very small sliver shown below. I think the issue lies in the scalebar element not recognizing the map it's supposed to access. Previously this was not an issue when running the arcpy script in the ArcGIS Pro application since I used a line which include ("CURRENT") which helped the tools identify the map. If someone could help that would be great!
Solved! Go to Solution.
Try this,
scalebars = layout.listElements("MAPSURROUND_ELEMENT")
    sb = None
    for s in scalebars:
        if s.name == "Scale Bar":
            sb = s
            break
    if sb:
        print(f"Scalebar Found: {sb.name}")
        # Scalebar linked to the map frame
        if sb.mapFrame.name != mf.name:
            sb.mapFrame = mf  # map frame
            print(f"Scalebar linked to map frame: {mf.name}")
you script wasn't posted
def mapLayout(map_title):
                            def MakeRec_LL(llx, lly, w, h):
                                xyRecList = [
                                    [llx, lly],
                                    [llx, lly + h],
                                    [llx + w, lly + h],
                                    [llx + w, lly],
                                    [llx, lly]
                                ]
                                array = arcpy.Array([arcpy.Point(*coords) for coords in xyRecList])
                                rec = arcpy.Polygon(array)
                                return rec
                            p = arcpy.mp.ArcGISProject(new_aprx_path)
                            lyt = p.createLayout(17, 11, 'INCH')
                            m = p.listMaps("Map")[0]
                            # Adjust the map frame to be larger
                            map_frame_width, map_frame_height = 14, 8  # Adjusted to make the map larger
                            map_frame_x = (17 - map_frame_width) / 2
                            map_frame_y = (11 - map_frame_height) / 2   # Adjusted for the larger map size
                            mf = lyt.createMapFrame(
                                MakeRec_LL(map_frame_x, map_frame_y, map_frame_width, map_frame_height),
                                m,
                                "New Map Frame"
                            )
                            #SCALEBAR
                            scale_bar_width = 2.5
                            scale_bar_height = 0.3
                            scale_bar_x = map_frame_x + 0.2
                            scale_bar_y = map_frame_y - 0.6
                            sbEnv = MakeRec_LL(scale_bar_x, scale_bar_y, scale_bar_width, scale_bar_height)
                            sbName = 'Scale Line 1 Metric'
                            sbStyle = p.listStyleItems('ArcGIS 2D', 'Scale_bar', sbName)[0]
                            sb = lyt.createMapSurroundElement(sbEnv, 'Scale_bar', mf, sbStyle, 'My Scale Bar')
                            sb.elementWidth = 3.5I am working on a standalone ArcPy script running FULLY out of the ArcGIS Pro application and I am running into an issue regarding the implementation of a scalebar. Currently, my script can add a scalebar however the scalebar says 0 KM and is a very small sliver shown below. I think the issue lies in the scalebar element not recognizing the map it's supposed to access. Previously this was not an issue when running the arcpy script in the ArcGIS Pro application since I used a line which include ("CURRENT") which helped the tools identify the map. If someone could help that would be great!
Try this,
scalebars = layout.listElements("MAPSURROUND_ELEMENT")
    sb = None
    for s in scalebars:
        if s.name == "Scale Bar":
            sb = s
            break
    if sb:
        print(f"Scalebar Found: {sb.name}")
        # Scalebar linked to the map frame
        if sb.mapFrame.name != mf.name:
            sb.mapFrame = mf  # map frame
            print(f"Scalebar linked to map frame: {mf.name}")
Awesome, worked great! Thank you.
