Python scripting and extension methods

I just discovered a nifty trick for accessing extension methods in (Iron)Python scripts. Namely, clr.ImportExtensions(). The example below leverages the extension methods that add Rhino/GH interoperability to Plankton.

"""Provides a scripting component.
    Inputs:
        m: an input Rhino.Geometry.Mesh
    Output:
        a: a bunch of face polylines"""

import clr
clr.AddReferenceToFile("Plankton.dll")
clr.AddReferenceToFile("Plankton.gha")
import Plankton
import PlanktonGh

# import class containing extension methods
clr.ImportExtensions(PlanktonGh.RhinoSupport)

# import mesh via Rhino.Geometry.Mesh extension method
pmesh = m.ToPlanktonMesh()

# TODO: do something exciting with our new halfedge mesh

# export polylines via Plankton.PlanktonMesh extension method
a = pmesh.ToPolylines()

via http://stackoverflow.com/questions/628482/can-you-use-linq-types-and-extension-methods-in-ironpython

4 Likes

@DanielPiker @AndersDeleuran @dave_stasiuk @mathias_gmachl

Excellent, there it is. Thanks for the heads-up. About time someone cracked that particular nut :wink:

Hi guys ,
i just copied the script in python component and i got this error :

Runtime error (IOException): Could not add reference to assembly Plankton.dll
Traceback:
line 8, in script

what should i do ?
where should i copy Plankton.dll ?

thank you for help .

Off the top of my head I had added the GH libraries folder to the Python
path in Rhino to make loading these assemblies less painful.

In Rhino 5, bring up the Python script editor (“EditPythonScript”), then go
to Tools > Options… and add %APPDATA%\Grasshopper\Libraries to the list
under the Files tab.

use this for any dll path:

import clr 

clr.AddReferenceToFileAndPath(r"D:\folder\folder\file.dll")
1 Like