[GIS] Setting transparency for layer in ArcPy

arcpyfeature-layertransparency

I have a blank map. And I have a shapefile. I would like to add the shapefile to the map document, change its colour (which I figured out using @AlexTereshenkov's answer to Adding a layer with specific color to map in ArcPy) and set its transparency.

My plan is to make a layer from the shapefile (MakeFeatureLayer_management), add the resulting layer to the map document (mapping.AddLayer) and then change the transparency using arcpy.transparency.

As it was explained to me in the other question, I need to reference the map layer not the feature layer. I assume the same applies for setting the layer's transparency. I tried both:

import arcpy, os

### Set directory
arcpy.env.workspace = ...
arcpy.env.overwriteOutput = 1

### Define inputs
yellow = "symbology/yellow.lyr"
city = boston
year = 2000

# Set map document
mxd_city_year = arcpy.mapping.MapDocument(r"...\blank_map.mxd")
DF = arcpy.mapping.ListDataFrames(mxd_city_year)[0]

# Add layers
arcpy.MakeFeatureLayer_management("states/continental_US.shp", "us")
basis = arcpy.mapping.Layer("us")
arcpy.mapping.AddLayer(DF, basis, "AUTO_ARRANGE")
map_lyr = arcpy.mapping.ListLayers(mxd_city_year, wildcard="us", data_frame=DF)[0]
arcpy.ApplySymbologyFromLayer_management(map_lyr, yellow)

# Set transparency (the part that does not work!)
map_lyr.transparency = 0
basis.transparency = 0

# Save map
mxd_city_year.saveACopy("thresh_" + city + "_" + year + ".mxd")

However, neither referencing the feature layer (basis.transparency = 0) nor the map layer (map_lyr.transparency = 0) seems to work. Does anyone know why?

Best Answer

You have to use the map layer object; you don't need to use the feature layer object (basis variable).

You are doing everything correct; you have to change the value of the transparency attribute of the map_lyr object.

From the Help page arcpy.mapping.Layer:

A layer's transparency value. This enables you to see through a layer. Use values between 0 and 100. A value of 0 is not transparent. A transparency value of more than 90% usually results in the layer not being drawn at all.

So, if you would like to make your layer slightly transparent, you would set transparency to be somewhere between 0 and 30 percent, i.e. map_lyr.transparency = 30.

Related Question