Select to view content in your preferred language

Calling Python Script from C# Add-in Button - ArcGISProject('Current') Not Working

1025
1
07-05-2023 01:44 PM
EvanMyers1
Frequent Contributor

I have ran into a problem while trying out the ESRI sample "CallScriptFromNet".  I have my own scripts that I would like to try but they seem to get stuck when I create a "arcpy.mp.ArcGISProject('current')" variable.  I get this message:

EvanMyers1_0-1688589465076.png

The script being referenced in C#:

EvanMyers1_1-1688589537791.png

The code works fine in Pro's Python window and its the correct parameter for that variable to work.  I am using this line of code so I can get a list of layers, check if they are properly named, etc...

Any ideas what I should do?

0 Kudos
1 Reply
tempStephenRhea_NV5
Frequent Contributor

Since you're calling the script via System.Diagnostics.Process, it's being run outside the Pro environment, meaning that "current" doesn't mean anything. You have two options, depending on whether you want to continue following the sample or run the script within the Pro environment. I've done both and personally prefer the latter option, even though there's a little more setup.

To continue with the sample, you'll need to add an argument to your Python script and pass the value of `ArcGIS.Desktop.Core.Project.Current.Path` to it. That value will then replace "current".

To run the script within the Pro environment, you'll need to move the code to a Python toolbox (pyt) file and include it in your add-in. From there you can call it just like a geoprocessing tool, and "current" will continue to work.