I am attempting to perform a SelectByLocation on a feature class and export that selection. I am running this code directly in the Python Window in ArcGIS Pro. Eventually this operation will be ported over to a larger script that will become a tool in a toolbox. If I use EnvManager as shown below to set "addOutputsToMap=True", the below works fine. When set to False, it does not.
<SPAN class="keyword token">import</SPAN> arcpy
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:\FULLPATH\TEST.gdb"</SPAN>
sheets <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Sheets"</SPAN>
parcels <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Parcels"</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>EnvManager<SPAN class="punctuation token">(</SPAN>addOutputsToMap<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
sheet_lyr <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>management<SPAN class="punctuation token">.</SPAN>SelectLayerByLocation<SPAN class="punctuation token">(</SPAN>sheets<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"INTERSECT"</SPAN><SPAN class="punctuation token">,</SPAN> parcels<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>management<SPAN class="punctuation token">.</SPAN>CopyFeatures<SPAN class="punctuation token">(</SPAN>sheet_lyr<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Sheet_Intersect"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>I have tried all sorts of combinations using MakeFeatureLayer(), using r"memory\xyz" as a workspace, using getOutput() from the Result, etc., and cannot figure out any combination that avoids adding the unnecessary intermediate layer "sheet_lyr" to the map. There seems to be several different methods that all work when adding the "sheet_lyr" to the map, but none when "addOutputsToMap=False".
Is there no way to perform a SelectByLocation and export the result entirely within memory workspace, without adding a layers to a map? Or is it just not possible to do directly in the Python Window?