GhPython recomputes inside a cluster

Hi everyone,

I made a script in ghPython that the users can set True and pick layers from Rhino and use them in GH.
Everything works fine until I clustered the whole GH script (including the ghpython components).
Every time I set a boolean True or False it recomputes the whole cluster and also the ghpython components. As a result, the ghpython compontent keeps asking for the layers everytime a boolean is True or False.

I would like to ask if anyone knows a solution or has to suggest another workflow to do that.
I am posting the script bellow. I have Rhino 6 SR 13
I hope it makes sense, I am not a scripting expert, thanks in advance.

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino as r
import ghpythonlib as ghl

sc.doc = r.RhinoDoc.ActiveDoc

objs = []
lnames = []
cols = []
if go:
    
    layerPath = rs.GetLayers("Pick your layers for the Plots")
    
    for i in range(len(layerPath)):
        
        obj = rs.ObjectsByLayer(layerPath[i])
        objs.append(obj)
        
        lname = rs.LayerName(layerPath[i], fullpath=False)
        lnames.append(lname)
        
        col = rs.LayerColor(layerPath[i])
        cols.append(col)
        
    objects = ghl.treehelpers.list_to_tree(objs)
    layerName = lnames
    layerColour = cols
sc.doc = ghdoc

Welcome @nikolaos.argy!

The first time, your script runs, you could save the layer information in the stick dictionary, if it’s not already in there. This could prevent your GHPython component to ask again, when it recomputes, since it can take the already present information from the sticky dictionary.
The sticky dictionary lives beyond the individual components, meaning that it’s not changed by the recomputing, unless you explicitly want that.

The code could look something like this:

if not "layerPath" in sc.sticky.keys():
    layerPath = rs.GetLayers("Pick your layers for the Plots")
    sc.sticky["layerPath"] = layerPath # save to sticky

layerPath = sc.sticky["layerPath"] # get from sticky
1 Like

Thanks a lot, works perfectly!

One more question: How can I replace/clear only the layerPath from the stick dictionary ?
I tried with sticky.clear() but it deletes everything, and I have to reselect everything again.

Cheers,

Nikos

You can delete dictionary entries like this:

sc.sticky.pop("layerPath", 0) # pop(key[, default])

The 0 is passed as second, optional argument default, because when default is not given and the first argument key, - in this case “layerPath” - is not found in the dictionary, a KeyError is raised otherwise.

2 Likes

Thanks a lot! Very helpful!

1 Like