Select to view content in your preferred language

Difference: Project Notebook vs Script Tool

298
8
Jump to solution
3 weeks ago
DanCrawford
Occasional Contributor

Hello.  I find that when I run a python command from my project notebook, the script runs and does what I expect it to.  When I run the same code from a toolbox script that I just created, it runs without error but doesn't do anything.

This is the notebook command:
arcpy.cartography.ConvertLabelsToGraphics(
  'Map', 20000, 'SINGLE_LAYER', 'XBA/New LTC-SUP/PofC', 'Graphics', 'DISPLAY',  
'GRAPHICS_LAYER_PER_FEATURE_LAYER', 'ONLY_PLACED', 'GroupGraphics')

This is the tool script:
import arcpy
def pofctographic():
tenure_type=arcpy.GetParameterAsText(0)
if tenure_type == "LTC-SUP":
   arcpy.AddMessage("LTC-SUP selected")
   arcpy.cartography.ConvertLabelsToGraphics(
     'Map', 20000, 'SINGLE_LAYER', 'XBA/New LTC-SUP/PofC', 'Graphics', 'DISPLAY',
     'GRAPHICS_LAYER_PER_FEATURE_LAYER', 'ONLY_PLACED', 'GroupGraphics')

if tenure_type == "CP":
  arcpy.AddMessage("CP selected")
  arcpy.cartography.ConvertLabelsToGraphics(
    'Map', 20000, 'SINGLE_LAYER', 'XBA/New CP/PofC', 'Graphics', 'DISPLAY',
    'GRAPHICS_LAYER_PER_FEATURE_LAYER', 'ONLY_PLACED', 'GroupGraphics')
pofctographic()

0 Kudos
1 Solution

Accepted Solutions
DanCrawford
Occasional Contributor

Dylan from ESRI Canada has provided this solution:
As a workaround, you can assign the result of the arcpy.cartography.ConvertLabelsToGraphics function to a variable, then add the first element of this variable (which will be the group layer) to the map object as follows:

import arcpy
arpx = arcpy.mp.ArcGISProject("CURRENT")
map_obj = arpx.listMaps("Map")[0]
glayer = arcpy.cartography.ConvertLabelsToGraphics(map_obj, 2000, 'SINGLE_LAYER', 'XBA/PofC')
map_obj.addLayer(glayer[0])

 Thank-you Dylan!

View solution in original post

8 Replies
CodyPatterson
MVP Regular Contributor

Hey @DanCrawford 

I think in the Notebook, it's setting all the project parameters automatically, but in the Toolbox, you may need to explicitly specify where everything's going, something like this:

import arcpy

def pofctographic():
    aprx = arcpy.mp.ArcGISProject("CURRENT")  # If in Pro, "CURRENT" if a .aprx file, use the path to that file
    map_obj = aprx.listMaps("Map")[0]  # Make sure 'Map' matches the name of your map

    tenure_type = arcpy.GetParameterAsText(0)
    if tenure_type == "LTC-SUP":
        arcpy.AddMessage("LTC-SUP selected")
        arcpy.cartography.ConvertLabelsToGraphics(
            map_obj, 20000, 'SINGLE_LAYER', 'XBA/New LTC-SUP/PofC', 'Graphics', 'DISPLAY',
            'GRAPHICS_LAYER_PER_FEATURE_LAYER', 'ONLY_PLACED', 'GroupGraphics')
    elif tenure_type == "CP":
        arcpy.AddMessage("CP selected")
        arcpy.cartography.ConvertLabelsToGraphics(
            map_obj, 20000, 'SINGLE_LAYER', 'XBA/New CP/PofC', 'Graphics', 'DISPLAY',
            'GRAPHICS_LAYER_PER_FEATURE_LAYER', 'ONLY_PLACED', 'GroupGraphics')

pofctographic()

Let me know if that works!

Cody

DanCrawford
Occasional Contributor

Thanks!  I'm off work until Tuesday but will let you know.

DanCrawford
Occasional Contributor

Sadly, adding those parameters has no effect.

0 Kudos
DanCrawford
Occasional Contributor

Same thing happens if I run it as a model.  Manual model works fine, run from the toolbox does nothing.

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

I don't have much constructive to add except for I'm currently wrapping up a support case where notebooks/python window work for the code but an ATBX or a PYT also run and do nothing with the exact same code while calling a Geoprocessing Service. 

All this to say, it isn't unheard of.

DavidSolari
MVP Regular Contributor

I swapped out the toolbox parameter for a feature layer input and got it down to this 2-line reproduction:

import arcpy
arcpy.cartography.ConvertLabelsToGraphics(
    arcpy.mp.ArcGISProject("CURRENT").activeMap,
    20000,
    'SINGLE_LAYER',
    arcpy.GetParameterAsText(0),
    'Graphics',
    'MAXOF',
    'GRAPHICS_LAYER_PER_FEATURE_LAYER',
    'ONLY_PLACED',
    'GroupGraphics'
)

I get the same issue as you in Pro 3.4.0, no changes are made to the map. I think the next move is to contact your support team to see if they can replicate it, hopefully this leads to a bug fix or a workaround.

DanCrawford
Occasional Contributor

Thanks.  I have opened a ticket with ESRI.  I'll update this post if/when a solution is provided.

0 Kudos
DanCrawford
Occasional Contributor

Dylan from ESRI Canada has provided this solution:
As a workaround, you can assign the result of the arcpy.cartography.ConvertLabelsToGraphics function to a variable, then add the first element of this variable (which will be the group layer) to the map object as follows:

import arcpy
arpx = arcpy.mp.ArcGISProject("CURRENT")
map_obj = arpx.listMaps("Map")[0]
glayer = arcpy.cartography.ConvertLabelsToGraphics(map_obj, 2000, 'SINGLE_LAYER', 'XBA/PofC')
map_obj.addLayer(glayer[0])

 Thank-you Dylan!