you also can also define a Feature class and fill it after Trace ended ending of trace and use MakeFeatureLayer to craete Layer
arcpy.env.workspace = r"E:/Utility/Sample/Montgomery.gdb"
G_N = "E:/Utility/Sample/Montgomery.gdb/Water/Water_Net"
Flag = "E:/Utility/Sample/Flag.shp"
traceOut = "traceOut_Net"
# Process: Trace Geometric Network
arcpy.TraceGeometricNetwork_management(G_N, traceOut, Flag, "TRACE_UPSTREAM", "", "", "", "", "", "NO_TRACE_ENDS", "", "", "", "AS_IS", "", "", "", "AS_IS")
spatial_reference = arcpy.SpatialReference('Projected Coordinate Systems/World/WGS 1984 Web Mercator (auxiliary sphere)')
FC = arcpy.CreateFeatureclass_management("in_memory","Q234","POLYLINE","","DISABLED","DISABLED",spatial_reference)
add proper filed to FeatureClass
FC = arcpy.CreateFeatureclass_management("in_memory","MyLines","POINT","","DISABLED","DISABLED",spatial_reference)
for FeatureClass in FC:
arcpy.AddField_management(FeatureClass, "TableName", "TEXT", "", "", "80", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(FeatureClass, "Obj_ID", "TEXT", "", "", "80", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(FeatureClass, "GeoJson", "TEXT", "", "", "880", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(FeatureClass, "LayerId", "TEXT", "", "", "80", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(FeatureClass, "LayerName", "TEXT", "", "", "80", "", "NULLABLE", "NON_REQUIRED", "")
do an a loop on result of trace next fillthen fill FeatureClass with values
for sublayer in traceOut :
layername=sublayer.name
ld=arcpy.Describe(sublayer)
layeraliasname=ld.aliasname
numbeeLayerId=numbeeLayerId+1;
if arcpy.Describe(sublayer).shapeType=="Point":
with arcpy.da.SearchCursor(sublayer,("OBJECTID","SHAPE@","SHAPE@JSON"),"",spatial_reference=spatial_reference)as cursora:
with arcpy.da.InsertCursor(FC,("OID","Shape","TableName","Obj_ID","GeoJson","LayerId","LayerName")) as iCur:
for row in cursora:
iCur.insertRow((row[0],row[1],layername,row[0],row[2],numbeeLayerId,layeraliasname))
now you can use MakeFeatureLayer function to generate your layer