I have other scripts that I would like to modify. The scripts work when the Feature Class is the top Layer within the TOC. Can you provide an example of a recursive layer search? Thanks. Previously I used for lyr in arcpy.mapping.ListLayers(lyr):.
import arcpy inMXD = r'c:\test.mxd' outMXD = r'c:\test1.mxd' rLayer = 'test3' #Name of reference layer mLayer = 'test4' #Name of layer to be replaced and moved lyrFile = arcpy.mapping.Layer(r'c:\Test4.lyr') #Layer file used to replace layer mxd = arcpy.mapping.MapDocument(inMXD) for df in arcpy.mapping.ListDataFrames(mxd): #loop data frames for lyr in arcpy.mapping.ListLayers(mxd,"",df): #loop layers if lyr.isGroupLayer == 1: #Is layer a group layer for glyr in arcpy.mapping.ListLayers(lyr): #loop layer in group layer if glyr != lyr: #Not Group Layer Name if glyr.name.lower() == mLayer: #Layer to be worked on
def layerInGroup(lyr, layerName): if lyr.isGroupLayer == 1: for glyr in arcpy.mapping.ListLayers(lyr): #loop layer in group layer if glyr != lyr: #Not Group Layer Name resultlyr = layerInGroup(glyr, layerName) # Recursively call the function for group in a group if resultlyr != None: return resultlyr # We found the layer deeper inside the group so return it and stop function elif lyr.name.lower() == layerName: # Not a group layer so test to see it is the one we want. return lyr # This is the layer we want so return it and stop function. return None # All tests failed to find layer we want so return None
Probably the script is just getting the first layer in the map period. It is the lazy, easy way to write a script. Logic can be added to search the layer tree for a given layer name and get it at other levels, but it requires knowing the layer name in the code. Or the selected layer could be used if the code is written to do that. If layers are placed inside group layers, recursive layer searches need to be used (an additional aspect to the logic and set up). Basically it is up to the programmers skill and experience to access the needed layer and to give or restrict the user's flexibility on where it is in the layer tree.
fc = arcpy.mapping.ListLayers(mxd, "Lot_Lines", df)[0]
All,I have a script that works when the desired Feature Class is listed above Feature-Linked Annotations within ArcMap TOC. But, the script does not work when the Feature Class is displayed below Feature-Linked Annotations. What is the logic behind why this occurs?[ATTACH=CONFIG]27752[/ATTACH][ATTACH=CONFIG]27753[/ATTACH]
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.