Importing the PlanktonGh library in GHPython? Possible?

ghpython
python
unhandled
planktonmesh
(P1r4t3b0y) #1

Hi folks,

I would like to use the Plankton Mesh library inside of GHPython. I know that you can use it in C# by Using PlanktonGH, however trying to import PlanktonGH in Python unfortunately doesn’t work the same way.
Plankton.dll and PlanktonGh.dll are both installed inside my components folder.

Is this at all possible? I would be interested in some half edge mesh methods.

Thanks.

#2

You have to reference the Plankton assemblies before importing, ala:

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

Assuming here that the Grasshopper libraries folder (where the assemblies are located) has been added to the GHPython path. If not, you need to do that before referencing the assemblies.

1 Like
(P1r4t3b0y) #3

Thanks, so far, @AndersDeleuran. How would I accomplish that?

#4

I prefer to hard code it via the EditPythonScript editor:

But you could also add it through code (before referencing and importing), something like this (not tested and from memory):

import sys
import Grasshopper as gh

if gh.Folders.DefaultAssemblyFolder not in sys.path:
    sys.path.append(gh.Folders.DefaultAssemblyFolder)
1 Like
(P1r4t3b0y) #5

@AndersDeleuran, thank you for providing both methods. They work great!

Would you by any chance also know how to convert a Rhino mesh into a plankton one in Python?
The plankton documentation shows that the PlanktonGh library has a method, called ToPlanktonMesh(source) inside its RhinoSupport class.
However, I’m unable to call it in Python. It gives me a read-only error, when I’m trying to initialise an plankton mesh.

Here’s what I’m trying to do:

import Grasshopper as gh
import sys
import clr

if gh.Folders.DefaultAssemblyFolder not in sys.path:
    sys.path.append(gh.Folders.DefaultAssemblyFolder)

clr.AddReferenceToFile("Plankton.dll")

import PlanktonGh

pm = PlanktonGh.PlanktonMesh() # initialise plankton mesh
pm.ToPlanktonMesh(mesh_plane) # mesh input from grasshopper to plankton mesh

I’m kinda lost here. Unfortunately, I’m not that familiar with C# and dll files. My guess would be that the read-only error stems from methods being private in C#, but ToPlanktonMesh(source) seems to be public.

#6

I believe you go:

PlanktonGh.RhinoSupport.ToPlanktonMesh(rhinoMesh)

But it has been a few years since I last used Plankton, @will would certainly know :slight_smile:

1 Like
(P1r4t3b0y) #7

Hm, unfortunately this also gives me the read-only error.

‘namespace#’ object attribute ‘ToPlanktonMesh’ is read-only

(Will Pearson) #8

Hi @p1r4t3b0y,

Here’s a working script…

import rhinoscriptsyntax as rs
import clr
from os import path

plankton_dir = "path/to/folder/with/plankton"

clr.AddReferenceToFileAndPath(path.join(plankton_dir, "Plankton.dll"))
clr.AddReferenceToFileAndPath(path.join(plankton_dir, "Plankton.gha"))

import Plankton
import PlanktonGh

clr.ImportExtensions(PlanktonGh.RhinoSupport)

m1 = x.ToPlanktonMesh()

print len(x.Vertices)

Note a few things:

  • The Plankton namespace is in Plankton.dll and contains the mesh data structure
  • The PlanktonGh namespace is in Plankton.gha and contains Grasshopper components and helper methods
  • There are a bunch of extension methods in PlanktonGh.RhinoSupport. These add methods to RhinoCommon classes, such as Rhino.Geometry.Mesh.ToPlanktonMesh() and can be used in python after calling clr.ImportExtensions(PlanktonGh.RhinoSupport).
  • I’m using clr.AddReferenceToFileAndPath() but your script above should work the same.

Let me know if you have any more questions!

2 Likes
(P1r4t3b0y) #9

Thank you, Will. Your answer is much appreciated!

#10

Hi Will,
Is there a current C# example of converting an input mesh to PlanktonMesh? I referenced the assembly Plankton.dll and added the line using Plankton, but couldn’t call the extension method ToPlanktonMesh(). Also tried renaming the Plankton.gha to PlantonGH.dll and then added using PlanktonGH with no success either. Thanks for the help!


(I was under the impression that Plankton.gha doesn’t have to be renamed and somehow still is able to be referenced in the C# component learning from this post long ago:)

(Will Pearson) #11

Hey @timothytai, here’s an example file…

plankton_from_mesh_example.gh (10.3 KB)

I’ve included an example C# script that references both Plankton.dll and Plankton.gha and uses the Mesh.ToPlanktonMesh() extension method. I think the reason this wasn’t working for you is that you used using PlanktonGH; instead of using PlanktonGh; (note the lower case “h”).

I’ve also included an example C# script that manually constructs a PlanktonMesh from a Rhino Mesh and supports NGons (this wasn’t available when Plankton was written).

By the way, you’re right, you can reference .gha assemblies in C# scripts without renaming them to .dll first. Here’s how…

Let me know if you have any more questions!

2 Likes
#12

Hi Will, it works, thank you very much for the multiple examples!
I have one additional question as illustrated in the screenshot below. This error actually only happened when I added the reference files initially, and disappeared when I tried to delete them in the “manage assembly” popup and added them back again.

I think Plankton.gha is causing the error as the other two scripts seem to work fine when only Plankton.dll is referenced? It seems to have to do with how now Kanagroo is placed somewhere else as discussed below?