GH sc.sticky change event / call python code components from another python component

Hello,

I would like to load and save a configuration from and to a file in Grasshopper using python, which works, apart from the right save function trigger.

Once loaded, the configuration is saved in a dict in the sc.sticky. The config file should automatically (without for example pressing a button) reflect changes made to the configuration, but I don’t know how to properly trigger the save function.

Right now, I am using a Timer-component to periodically trigger a save config python component.

I checked if there is an event that I could use for this (https://developer.rhino3d.com/wip/api/grasshopper/html/N_Grasshopper_Kernel.htm), but I didn’t find a suitable one. Additionally, it doesn’t seem like one can even use these events from Python, only from VB and C#. Is it possible in GH to call python from a VB or C# script?

Another solution would be calling some python component from another python component. I did not find anything on this here either.

Of course, I could trigger the update directly by connecting all of the components, but I’d rather not do this.

Thanks

from scriptcontext import sticky
id = ghenv.Component.InstanceGuid
if sticky.has_key('i'):
   a = sticky['i']
from scriptcontext import sticky
comp = ghenv.Component.OnPingDocument().FindObject(id, False)
sticky['i'] = v
comp.ExpireSolution(True)

UpdateById.gh (9.7 KB)

2 Likes

Hello Mahdiyar, thanks for your answer! :smile:
How fragile is this solution? When copying these elements from one file to another, the ids change, if you copy them as a cluster they ids stay the same. Is this generally true?

I think a version which is using named groups (that’s the way Rhino computed identified elements too as I learned today) would be more robust.
Unfortunately I haven’t found a way to find out which Grasshopper groups a component is part of yet.
I checked the forum for solutions (Is there a way to accesss groups-information in an object? this one is talking about Rhino groups, not about GH groups), but coudn’t find anything on it.

Thanks again!

#ID
from scriptcontext import sticky
comp = ghenv.Component.OnPingDocument().FindObject(id, False)
sticky['i'] = v
comp.ExpireSolution(True)
#Name
from scriptcontext import sticky
sticky['i'] = v
for obj in ghenv.Component.OnPingDocument().Objects:
    if obj.NickName in names:
        obj.ExpireSolution(True)
#Group
from scriptcontext import sticky
import Grasshopper as gh

def FindPythonObjectInCurrentGroup():
    pythonObjects = []
    ghObjects = ghenv.Component.OnPingDocument().Objects
    for obj in ghObjects:
        if type(obj) is gh.Kernel.Special.GH_Group:
            if obj.ObjectIDs.Contains(ghenv.Component.InstanceGuid):
                group = obj
                for obj in ghObjects:
                    if ghenv.Component.ComponentGuid == obj.ComponentGuid:
                        if ghenv.Component.InstanceGuid != obj.InstanceGuid:
                            if group.ObjectIDs.Contains(obj.InstanceGuid):
                                pythonObjects.append(obj)
    return pythonObjects

sticky['i'] = v
pythonObjects = FindPythonObjectInCurrentGroup()
for obj in pythonObjects: 
    obj.ExpireSolution(True)

Update.gh (9.1 KB)

2 Likes

Thank you very much! :+1: