0

The bulk of the code used in my script for reading this is from @RHB located here. His block of code runs fine with a current map open. Need it to open without an ArcMap session open. I tried as suggested shifting the parameters instead of "CURRENT" using a filepath for the mxd, at which point it fails to output the layer/file. Thought maybe I needed to add mxd.save() within the function in order to find the layer, but to no avail.

def load_feature_layer(fc_path, layer_name):
    # loads a feature layer into the active data frame in the current map
    # using a feature class path and a layer name string.
    # returns a layer object.
    mxd = arcpy.mapping.MapDocument("C:\\temp\\parser.mxd")
    df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
    layerfile = os.path.join(arcpy.env.scratchFolder, layer_name + ".lyr")
    arcpy.MakeFeatureLayer_management(fc_path, layer_name)
    arcpy.SaveToLayerFile_management(layer_name, layerfile, "CURRENT")
    add_layer = arcpy.mapping.Layer(layerfile)
    arcpy.mapping.AddLayer(df, add_layer)
    arcpy.RefreshTOC()
    l = get_layer_by_name(layer_name)
    return l
    mxd.save()

def get_layer_by_name(name_string):
'''Finds a layer in the current MXD in active data frame by name.'''
    mxd = arcpy.mapping.MapDocument("C:\\temp\\parser.mxd")
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    layer_list = arcpy.mapping.ListLayers(mxd, "*", df)
    desired_layer = None
    for l in layer_list:
        if l.name.lower() == name_string.lower():
            desired_layer = l
        if l.isGroupLayer:
            for sub_layer in l:
                if sub_layer.name.lower() == name_string.lower():
                    desired_layer = sub_layer
    return desired_layer    

if __name__ == "__main__":

    import arcpy, traceback, sys, os

try:

    outShp = arcpy.GetParameterAsText(0) # Shapefile
    outName = arcpy.GetParameterAsText(1) # String
    outLpk = arcpy.GetParameterAsText(2) # File .lpk
    #...<the beginning of your code>...

    layer1 = load_feature_layer(outShp, outName)
    layer1.description = outName

    arcpy.PackageLayer_management(layer1, outLpk, "PRESERVE", "CONVERT_ARCSDE", "", "ALL", "ALL", "ALL", "", "Footprint", "Bounding")

except:

    # PRINT ERROR MESSAGES
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value)
    arcpy.AddError("Python Messages: " + pymsg + " GP Messages: " + arcpy.GetMessages(2))

There are no errors when running it, it just fails to generate any output when pointing to the mxd.

  • Please don't just link to code in other places. To help we need to see the code snippet in the question itself that you are running to illustrate what you are asking about, and any error messages and output from print statements etc that it generates. – PolyGeo Feb 18 '16 at 5:07
  • Put it in there with the modified parameters – geodranic Feb 18 '16 at 5:19
2

To try and determine what is going wrong in your code I think you should tackle the first function first. That is easily extracted out into the code snippet below:

import arcpy,os

fc_path = <your shapefile including its path>
layer_name = <your layer name>

mxd = arcpy.mapping.MapDocument("C:\\temp\\parser.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
layerfile = os.path.join(arcpy.env.scratchFolder, layer_name + ".lyr")
arcpy.MakeFeatureLayer_management(fc_path, layer_name)
arcpy.SaveToLayerFile_management(layer_name, layerfile, "CURRENT")
add_layer = arcpy.mapping.Layer(layerfile)
arcpy.mapping.AddLayer(df, add_layer)
arcpy.RefreshTOC()
l = get_layer_by_name(layer_name)
return l
mxd.save()

If you set the two variables at its beginning to suitable test values, and run this code snippet then I suspect that you will get an error that will either mean something to you or give us a focussed question to look at.

I will be surprised if the line that includes SaveToLayerFile_management does not throw an error, and that error is probably being hidden by the try/except statements in the code you presented.

  • Good call, changed "CURRENT" to "ABSOLUTE" and commented out the try/except to see the errors, it was indeed masking them. Looks like the PackageLayer_management portion is not finding the input (layer1). Getting the following: ERROR 000735: Input Layer: Value is required Failed to execute (PackageLayer). – geodranic Feb 18 '16 at 6:00
  • I think that should be a new question, because I don't think you are finished testing the snippet in my answer yet. I would forget about the PackageLayer line until I was confident that the two functions your code calls are both working properly. – PolyGeo Feb 18 '16 at 6:08
  • It's outputting the layer file, but not adding/saving it to the mxd, and therefore can't output the layer package... I guess I'm not entirely sure how to ask that in a new question without it getting put on hold for being duplicate? – geodranic Feb 18 '16 at 6:56
  • When you say "adding/saving it to the mxd" do you mean to C:\\temp\\parser.mxd? If so, then a question based on a working code snippet quite similar to the one in my answer, that forgets about the larger program that it has been extracted from for testing purposes, should not risk being put on hold. – PolyGeo Feb 18 '16 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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