1

Does anyone have / know of a script that converts all the MXD`s in a folder to individual group layer files ?

It needs to use the MXD name as the group layer file name.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
dhowal
  • 31
  • 6
  • 1
    I don't know if such a complete script already exists, but I believe you will find two of the major pieces of it at http://gis.stackexchange.com/questions/4681/ and http://gis.stackexchange.com/questions/59634/ – Chris W May 07 '15 at 22:10
  • I don't think doing this will make much sense when an MXD has more than one data frame. – PolyGeo May 07 '15 at 22:15

1 Answers1

1

I actually just needed to do this too. I posted an ArcGIS toolbox but the code is:

import os
import arcpy
import inspect
import glob
import uuid
import inspect

codeDir = os.path.dirname(inspect.getfile(inspect.currentframe()))
EmptyGroupLayerFile = codeDir+"/EmptyGroup.lyr"
inArg1 = sys.argv[1]
inArg2 = sys.argv[2]

def printit(inMessage):
    arcpy.AddMessage(inMessage)

def makeLyrFromMXD(inMXD, outLyr):
    if not (os.path.exists(inMXD)):
        printit( "ERROR: {} does not exist".format(inMXD))
        return False
    if not (os.path.exists(EmptyGroupLayerFile)):
        printit( "ERROR: {} does not exist".format(EmptyGroupLayerFile))
        return False
    if  (os.path.exists(outLyr)):
        printit( "Skipping: {} already exists".format(outLyr))
        return True

    printit( "Making Layer file: {0}".format(outLyr))

    mxd = arcpy.mapping.MapDocument(inMXD)
    ###Right now, just doing the first Dataframe, this could be modified
    df = arcpy.mapping.ListDataFrames(mxd)[0]

    theUUID = str(uuid.uuid1())

    iGroupLayerRaw = arcpy.mapping.Layer(EmptyGroupLayerFile)
    iGroupLayerRaw.name = theUUID
    arcpy.mapping.AddLayer(df,iGroupLayerRaw,"TOP")
    groupBaseName = os.path.basename(outLyr).split(".")[0]

    for lyr in arcpy.mapping.ListLayers(df):
        if not (lyr.name == theUUID):
            if (lyr.isGroupLayer):
                print "Adding Group: "+lyr.name
                arcpy.mapping.AddLayerToGroup (df, iGroupLayer, lyr, "Bottom")
            elif (lyr.longName == lyr.name):
                arcpy.mapping.AddLayerToGroup (df, iGroupLayer, lyr, "Bottom")
            else:
                iGroupLayer = lyr

        iGroupLayer.name = groupBaseName
        arcpy.SaveToLayerFile_management(iGroupLayer, outLyr)
        return os.path.exists(outLyr)

    def doMultiple(inDir,outDir):
        for iMxd in glob.glob(inDir+"/*.mxd"):
            lyrFile = outDir+"/"+os.path.basename(iMxd).lower().replace(".mxd",".lyr")
            makeLyrFromMXD(iMxd, lyrFile)

if(not os.path.exists(EmptyGroupLayerFile)):
    printit("Error: {} is missing, can not run.".format(EmptyGroupLayerFile))
else:
    if (os.path.isdir(inArg1) and (os.path.isdir(inArg2))):
        doMultiple(inArg1,inArg2)
    elif (os.path.isfile(inArg1)):
        if (os.path.exists(inArg2)):
            printit("Error: {} already exists".format(inArg2))
        else:
            makeLyrFromMXD(inArg1,inArg2)
    else:
        printit("Unable to understand input parameters")
Matt R
  • 11
  • 1