Python module inside C#

Hi,

I am trying to run python module inside visual studio c#, but I get an error after line scope.Add = new Func((x, y) => x + y);.

Error:
Missing compiler required member ‘Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create’

When I write a script in the main function (And referenced all required python .dll) :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IronPython;
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using Microsoft.CSharp;

namespace Bob.Meshes {
   public class pythonFromCShapr {

        ScriptEngine engine = Python.CreateEngine();

        public pythonFromCShapr() {

        }

        public void something() {
            dynamic scope = engine.CreateScope();
            scope.Add = new Func<int, int, int>((x, y) => x + y);
            Console.WriteLine( scope.Add(2, 3));
        }

    }
}

The Rhino.Runtime.PythonScript has functions to run Python scripts. Or are you trying to do something else?

Hi,

Thank you for an answer. I did not know that there is Rhino.Runtime.PythonScript.

I would like to run networkX library for IronPython , but in grasshopper component.

I have used networkX in grasshopper using Python Component, but wondering if it possible to use several functions from this library in my custom component.

Nevertheless, I would like to ask much more simple question.

Could please show me a simple example using Rhino.Runtime.PythonScript when I want to execute python commands in C# component and get values from it?

Thanks,
Petras

I managed to run Python in C# grasshopper component using visual studio.

I would like to ask how to load IronPython library.

Do these references will work fine, the same way as using python editor?

import random
import Rhino as r
from Rhino.Geometry import *
import Grasshopper as gh
import copy
import networkx as nx

It looks like all modules can be loaded instead of Grasshopper.

Do you know how I could load Grasshopper module?

import random
import Rhino as r
**from Rhino.Geometry import ***
import Grasshopper as gh
import copy
import networkx as nx

And do you know how can I pass c# value such as data tree to python ?

Hi Petras

You need to use:

import clr
clr.AddReference("Grasshopper")

before importing Grasshopper.

I am curious however: what makes it more useful, for you to write Python inside a C# component? I am doubting there are actually many benefits, and debugging gets even more complicated.

Hi,

Thank you for an answer.

I think so too.

I just need to test a library that I know. Then probably I have to search for something similar in c# instead of using python library.

And do you know how can I pass c# value such as data tree to python ?

Sorry, that’s pretty involved. You can check what GhPython does, if you want, in the GhPython source. I would suggest, however, not to maintain such a codebase, but it’s your choice.