Hi
I am looking into upgrading my package to zero touch nodes, and I found an example of executing an IronPython script from string in C# using the IronPython.Hosting namespace.
https://developer.dynamobim.org/03-Development-Options/3-3-python-scripts-in-csharp.html
Now that we are using CPython I tried finding an example for the same solution, but I could not find a good example that references CPython use. I would like to run a pythonscript from string, and return some object or value from the python execution.
Does anyone have any experience with this?
2 Likes
you can see how Dynamo does it here:
curious why you want to do this?
4 Likes
Excellent, thank you so much for that great example
The main reason is curiosity, but I would also like to play with the idea of building a python interpreter into my C# addin for Revit to make users able to expand on my functionality.
I.E. I have a filtering system in place for some of my addin tasks, and I would love it if users could append small python scripts on top of my C# basic filter GUI for more advanced cases.
Could this be used outside of the Dynamo environment as well, or does it require Dynamo to be running?
1 Like
@andre.abotnes @Michael_Kirschner2
can You give me simple example how we can call Cpython from c#
I try this code but the out is non
using System;
using Python.Runtime;
namespace PythonDotNetExample
{
public class pythontest
{
public static string rrr()
{
// Initialize Python engine
PythonEngine.Initialize();
string result;
using (Py.GIL()) // Acquire the Python Global Interpreter Lock (GIL)
{
// Run the Python code
dynamic output = PythonEngine.RunString(@"# Load the Python Standard and DesignScript Libraries
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
# The inputs to this node will be stored as a list in the IN variables.
C=2 + 3
OUT=C
");
// Convert the Python output to a string
result = output?.ToString();
}
// Finalize Python engine
PythonEngine.Shutdown();
return result;
}
}
}
I think the pythonNet readme will explain using pythonNet better than I ever could
3 Likes