Grasshopper

algorithmic modeling for Rhino

Hi everybody, 

I was trying to compile a component that was created using ironpython. I got it compiled into a dll and it works (I tested the method using a simple "series" operation). But I can't quite figure out how to reference that dll in the SolveInstance. The only examples I can find is ones where the solution is written into vb. Can anyone help?

Thanks in advance,

Zach

Views: 1864

Attachments:

Replies to This Discussion

Hi Zachary,

if you compiled the Python module with clr.CompileModules(), you still need the whole IronPython implementation to run it. IronPython compiled modules are not "self-sufficient", to put it simply. The DLR does still all the method bindings, etc.

For sure you can use this .dll from the GhPython component.
I'll see if I can find the way to link this .dll as in the picture, but cannot promise it.

- Giulio
_______________
giulio@mcneel.com

Hi Giulio,

What if I used the pyc.py Compiler that ships with ironpython? When I do this it produces both a dll and an executable file that is self sufficient. Could this mean that possibly the dll produced is also self sufficient? 

Thanks for the reply Giulio,

Zach

Also, I know that it includes dependent modules in the dll. Perhaps this is a good sign?

Example,

If I say in my main file:

import HiWorld1.py

import HiWorld2.py

Then both HiWorld1 and HiWorld2 would be included in the dll

About pyc.py, from the current IronPython documentation:
"The pyc sample can be used to compile IronPython code into an assembly. The sample builds on top of clr-CompileModules. The assembly can then be loaded and executed using Python-ImportModule. However, note that the MSIL in the assembly is not CLS-compliant and cannot be directly accessed from other .NET languages.".

---

My suggestion would be then to compile the assembly and use the GhPython component to execute the code like this:

  1. Edit and test carefully the Python code you would like to execute. Make it into a module. In this example, the code is in a file called seriesModule.py, later renamed seriesModule_code.py for testing. In this case, the module contains a single function, ThreeSeries(), but it could be anything really.
  2. Compile the module with clr.CompileModules or pyc.py. This is the same, as pyc.py uses clr.CompileModules ultimately. The code I used is in compile.py. You can assemble more than a Python module in the same result. Choose a name for the new .dll file. Rename or remove the text module so that you can be sure it is not having any effect.
  3. In a GhPython component, use clr.AddReference("theNameOfTheDll"). You can now just import the module and it will work as it did previously.
    To make the use of the .dll simpler, you can dynamically add the location of the Grasshopper file to the module search path. Now the .dll can just stay in the same folder as the Grasshopper file and it will be found.

I hope this suggestion is helpful,

- Giulio
_______________
giulio@mcneel.com

Attachments:

Hi Giulio,

So I'm gathering from the first part of your reply that it isn't possible to use the dll in the creation of a compiled gha file using vb studio?

Thanks for all the info,

Zach

I would not say it is impossible. They have a very general hosting infrastructure (*), but at present what I commented there is by far a more reachable solution.

If this is not usable for any reason - please let me know,

- Giulio
_______________
giulio@mcneel.com

* More geeky details here. The next most promising spot is the __clrtype__ metaclass. However, things get rapidly more difficult as you are required to write the metaclass in IL. I'd do that for you if it were the only step, but then additionally more things would be necessary to make this fully compatible with RhinoScriptSyntax, Grasshopper and third-party libraries (almost rewriting the whole GhPython component, this time in IL). This compatibility, that is already part of the Rhino-embedded IronPython framework and GhPython, is what would seem to take a sizable time commitment to get right.

Thanks Giulio,

I'll let you know how it goes.

Zach

I got it going Giulio, Thanks for the direction, it works perfectly! I would like to be able to distribute a few components, which is why I wanted to be able to compile to a gha. This alternative method isn't ideal because you can't edit the hints for each input and output and you would still be able to open the python editor when double clicking the components, however it does successfully mask the source code, and compiles all dependencies into a single dll. So I guess this means that I can distribute a folder containing all of the custom components and the dll file. Like I said not ideal but the biggest thing is being able to run these components without installing a ton of modules. Thanks again for all the help, This forum has been amazing.

Hi Zachaey,

I think you might be able to help me. I am new to programming and grasshopper in general. I have coded something in grasshopper and now would like to package it into a single component. I also have to write the component in python since my original grasshopper code contains a python script component that uses some external libraries. Would it be possible for you to list the steps necessary to create and access a grasshopper component written in python? Again I am new to all this so please be as thorough and clear as possible.

Thanks

-Marcel

RSS

About

Translate

Search

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service