0

I have some scripting where the goal has been to create data and spatializes them from tables through an FME workbench saved as a data interop tool and saved in a FGDB, which then a script is ran to add them into the map frame, and changes the names of the feature classes. Those all work, however when I get to the symbology script I have, it runs and works just fine in the window, but does not work in a model.

import arcpy

# Set the map name where you want to symbolize the feature class
map_name = "Map"  # Change this to match your map name

# Get the current project
aprx = arcpy.mp.ArcGISProject("CURRENT")

# Get the map frame
map_frame = aprx.listMaps(map_name)[0]

# Dictionary to map layer name to symbology layer file (for one layer)
layer_to_symbology_mapping = {
    "Layer_Example": r"D:\Layer_Example.lyrx"
}

# Loop through the layers in the map frame and apply symbology
for lyr in map_frame.listLayers():
    # Check if the layer name has a mapping in the dictionary
    if lyr.name in layer_to_symbology_mapping:
        symbology_layer = layer_to_symbology_mapping[lyr.name]
        # Apply symbology from the layer file
        arcpy.management.ApplySymbologyFromLayer(lyr, symbology_layer)
        print(f"Symbology applied to {lyr.name} using {symbology_layer}")

# Save the project
aprx.save()

I am a bit stumped, when it runs in ModelBuilder, it doesn't throw an error, it will give a lock on the layer in the drawing order, but nothing happens, but works like a charm in the Python window. Any ideas on what could be happening?

2
  • What do you mean "in a model"? Please explain how you are running this script "in a model". Commented Aug 24, 2023 at 3:53
  • It is 4 year old problem. Apply symbology is not working from script either.
    – FelixIP
    Commented Aug 24, 2023 at 9:54

2 Answers 2

0

Yes, it is. ApplySymbologyFromLayer is not working from script. This is workflow with layer saved as .lyr or .lyrx file:

L = m.addDataFromPath(ps)
sym = L.symbology
lyr = m.listLayers(inBuildings)[0]
lyr.symbology = sym
m.removeLayer(L)

where m is map, ps is path to .lyr file and lyr is the one to receive this symbology. Works even with 'broken' .lyr

0

The reply from @FelixIP helped a lot as i also did something similar in an arcpy script tool,

src_layer_name = param0
src_layer = None

for layer in layers:
    if layer.name == src_layer_name:
        src_layer = layer
        break
src_symbology = src_layer.symbology

for layer in layers:
    desc = arcpy.Describe(layer)
    if layer.isFeatureLayer and desc.shapeType=="Polygon":
        arcpy.AddMessage(f"Applying symbology to {layer.name}...")
        layer.symbology = src_symbology

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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