Python for Grasshopper not importing

Grasshopper vs. 0.9.0076
Rhino V5 SR14 64-bit
Python for grasshopper

Create python script ->

import ghpythonlib.treehelpers as th

error message:

Runtime error (ImportException): cannot import treehelpers from ghpythonlib

Followed this example:

Any known solutions? Python works in grasshopper in general, but can not import grasshopper components.

They weren’t available in Rhino 5. They were introduced in Rhino 6. But…In the sample file at the end of that page, there is a gh file. That file contains 2 methods:
1 - one component using GH Classes to make datatrees (a little trickier, but works in Rhino 5)
2 - one component using Treehelpers, (which are great, but only in Rhino 6).

1 Like

I see, thanks. They should change the documentation, because at the top it says you are viewing the documentation for “Rhino 5”, which makes following the example for Rhino 5 frustrating if they didn’t exist at that time.

Also the sample file in both cases uses the treehelper, and neither works in 5.

it doesn’t?! It’s working in Rhino 5 for me, (but I also have Rhino 6 installed, maybe that has something to do with it?) This is from Rhino 5.

using the example file from this page: (the treehelper fails, as expected, in rhino 5)

this is the code from that example, using GH classes: it’s not using the treehelper function at all.

import Rhino

# for accesssing GH classes
from Grasshopper.Kernel.Data import GH_Path
from Grasshopper import DataTree
import System.Array as array

layerTree = DataTree[object]() # make a DataTree
for i in range(len(layernames)):
    objs = Rhino.RhinoDoc.ActiveDoc.Objects.FindByLayer(layernames[i])
    if objs:
        geoms = [obj.Geometry for obj in objs]
        path = GH_Path(array[int]([0,0,i]))
        layerTree.AddRange(geoms, path)
a = layerTree

I am so confused, that is not the code when I download it. Thanks for checking it!

Am I looking at the right script to download?

I see, it was the box above the one I was looking at. Sorry!

ok, couple things.
1 - that is the correct link and file
2 - your screen shot is showing the component using Treehelpers…this will not work in Rhino 5
3 - the component ABOVE that uses GH classes. It will work in Rhino 5.

The script in that component requires some geometry to be on the layers that are named in the layernames input. If there is no geometry in the Rhino document, on those layers, there will be no output from the component.

Yes very sorry, I noticed that I was looking at the wrong box. When you said two examples, I thought it was the grasshopper groups that you were talking about.


Since you have been so helpful so far, anychance you know why the variable ‘layernames’ defined in the python input is not defined in the script?

any chance you have a typo somewhere? Make sure that the input spelling of “layernames” is the same as where it is referenced in your code, (line 10).

Just for reference, here’s everything you’re probably ever gonna need to know regarding reading/writing DataTrees in GHPython:

1 Like