Grasshopper Plug-In with Python library

Hi,

I’m planning to write a Grasshopper Plug-In that will use a library written in Python.
What would be the best way to go about this?

I assume that the Plug-In itself has to be in C# or some such, right? (I.e., not in, say IronPython.)
In that case, what is the best way to access the Python library.

Thanks,
Thomas

RhinoCommon has functions which let you run python scripts. See the Rhino.Runtime.PythonScript class for this in the RhinoCommon SDK

@piac, did we ever release the GhPython with support for creating python based components?

Thanks Steve,

So in other words, the approach would be to write a C# plug-in, and call Python via the Python Runtime you mentioned, right?

Thanks,
Thomas

Yes, that would be one approach. I mentioned @piac because he wrote another optional approach where everything can be done in python. I’m not sure what the status of that project is though.

That temptative approach is in the next Rhino branch. Should that be changed?

No, that’s fine. I just couldn’t remember where we were with respect to that functionality.

Steve, Giulio,
that sounds like great news for Python scripters. I would personally like to see that in R5 and I suspect a lot of people would too. If R6 is a long way away, a lot of ppl will ask for having that functionality in R5.
Cheers,
Florin

2 Likes

Hi Steve,

Thanks for your help so far. It took me a while, but I finally got around to working on this project.
I’m experimenting with running a python script from C# in Grasshopper, which seems to work (ExecuteFile returns True).

However, I’m struggling to understand how to input/output with that script. Here’s my C# code so far:

string path = “C:\python_hello.py”;
Rhino.Runtime.PythonScript rbfopt = Rhino.Runtime.PythonScript.Create();
rbfopt.SetVariable(“hello”, “blank”);
rbfopt.ExecuteFile(path).ToString();
object output = rbfopt.GetVariable(“hello”);
Print(output.ToString());

python_hello.py looks like this:

hello = “Hello Thomas!”

Now, I’d expect “Hello Thomas!” as an output, but I get “blank”.

So, if I run a file, (how) can I access its variables? Alternatively, can I somehow “print” some output?
I guess I can always read and write *.txt files, but I’m hoping that there’s an easier way.

Thanks!
Thomas

I found a way to solve my problem:

1 Like

Just to be shure I understood this: There will be a way to create “real” components using python (as opposed to using the python-script component)?
Is that already in the R6 WIP? If yes, what is the way to get that WIP?

Cheers
Henning

Yep, there’ll be a way to compile Python code. This has been in the works for a while, from what I understand. You might want to check out this discussion.
/SPM

Yes. You just need Rhino WIP to test this.