Skip to content

Commit

Permalink
Merge pull request #3 from gramaziokohler/2-clear-sticky
Browse files Browse the repository at this point in the history
2 clear sticky
  • Loading branch information
funkchaser authored Apr 17, 2024
2 parents 3cf9af5 + 4933bf6 commit f9a89c8
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/aixd_grasshopper/components/aixd_DatasetCreate/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from aixd_grasshopper.gh_ui_helper import session_id, component_id
from scriptcontext import sticky as st

cid = component_id(ghenv.Component, "create_dataset_object")
cid = component_id(session_id(), ghenv.Component, "DatasetCreate")


design_parameters = [x.data for x in design_parameters]
Expand Down
2 changes: 1 addition & 1 deletion src/aixd_grasshopper/components/aixd_DatasetLoad/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from aixd_grasshopper.gh_ui_helper import session_id, component_id
from scriptcontext import sticky as st

cid = component_id(ghenv.Component, "create_dataset_object")
cid = component_id(session_id(), ghenv.Component, "DatasetLoad")

if load:
st[cid] = load_dataset(session_id())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from aixd_grasshopper.gh_ui_helper import session_id, component_id
from scriptcontext import sticky as st

cid = component_id(ghenv.Component, "dataset_summary")
cid = component_id(session_id(), ghenv.Component, "DatasetSummary")

if get:
st[cid] = dataset_summary(session_id())
Expand Down
2 changes: 1 addition & 1 deletion src/aixd_grasshopper/components/aixd_Generator/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __repr__(self):
from aixd_grasshopper.gh_ui_helper import session_id, component_id
from scriptcontext import sticky as st

cid = component_id(ghenv.Component, "run_generation")
cid = component_id(session_id(), ghenv.Component, "Generator")


if run and request_ok:
Expand Down
2 changes: 1 addition & 1 deletion src/aixd_grasshopper/components/aixd_ModelDims/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from aixd_grasshopper.gh_ui_helper import session_id, component_id
from scriptcontext import sticky as st

cid = component_id(ghenv.Component, "model_dims")
cid = component_id(session_id(), ghenv.Component, "ModelDims")

if get:
st[cid] = model_input_output_dimensions(session_id())
Expand Down
2 changes: 1 addition & 1 deletion src/aixd_grasshopper/components/aixd_ModelLoad/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from aixd_grasshopper.gh_ui_helper import session_id, component_id
from scriptcontext import sticky as st

cid = component_id(ghenv.Component, "model_load")
cid = component_id(session_id(), ghenv.Component, "ModelLoad")

if not checkpoint_path:
checkpoint_path = ""
Expand Down
2 changes: 1 addition & 1 deletion src/aixd_grasshopper/components/aixd_ModelSetup/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from aixd_grasshopper.gh_ui_helper import component_id
from aixd_grasshopper.gh_ui_helper import session_id

cid = component_id(ghenv.Component, "model_setup")
cid = component_id(session_id(), ghenv.Component, "ModelSetup")

settings = {
"inputML": inputML,
Expand Down
2 changes: 1 addition & 1 deletion src/aixd_grasshopper/components/aixd_ModelSummary/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from aixd_grasshopper.gh_ui_helper import component_id
from aixd_grasshopper.gh_ui_helper import session_id

cid = component_id(ghenv.Component, "model_summary")
cid = component_id(session_id(), ghenv.Component, "ModelSummary")


if not max_depth:
Expand Down
2 changes: 1 addition & 1 deletion src/aixd_grasshopper/components/aixd_ModelTrain/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from aixd_grasshopper.gh_ui_helper import component_id
from aixd_grasshopper.gh_ui_helper import session_id

cid = component_id(ghenv.Component, "model_train")
cid = component_id(session_id(), ghenv.Component, "ModelTrain")


if not epochs or epochs < 1:
Expand Down
2 changes: 1 addition & 1 deletion src/aixd_grasshopper/components/aixd_PlotContours/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from aixd_grasshopper.gh_ui_helper import convert_str_to_bitmap
from aixd_grasshopper.gh_ui_helper import session_id

cid = component_id(ghenv.Component, "create_dataset_object")
cid = component_id(session_id(), ghenv.Component, "PlotContours")


if plot:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from aixd_grasshopper.gh_ui_helper import convert_str_to_bitmap
from aixd_grasshopper.gh_ui_helper import session_id

cid = component_id(ghenv.Component, "create_dataset_object")
cid = component_id(session_id(), ghenv.Component, "PlotCorrelations")


if plot:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from aixd_grasshopper.gh_ui_helper import convert_str_to_bitmap
from aixd_grasshopper.gh_ui_helper import session_id

cid = component_id(ghenv.Component, "create_dataset_object")
cid = component_id(session_id(), ghenv.Component, "PlotDistributions")


if plot:
Expand Down
10 changes: 4 additions & 6 deletions src/aixd_grasshopper/components/aixd_ProjectSetup/code.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# flake8: noqa
import os

from scriptcontext import sticky as st

from aixd_grasshopper.gh_ui import project_setup
from aixd_grasshopper.gh_ui_helper import component_id

from aixd_grasshopper.gh_ui_helper import session_id
from aixd_grasshopper.gh_ui_helper import session_id, component_id, clear_sticky

if project_folder and dataset_name:
complete_path = os.path.join(project_folder, dataset_name)
if not os.path.exists(complete_path):
print("The path {} does not exist and will be now created.".format(complete_path))
os.mkdir(complete_path)

cid = component_id(ghenv.Component, "project_setup")
cid = component_id(session_id(), ghenv.Component, "ProjectSetup")

if set:
clear_sticky(ghenv, st)

st[cid] = project_setup(session_id(), project_folder, dataset_name)

if cid in st.keys():
Expand Down
60 changes: 58 additions & 2 deletions src/aixd_grasshopper/gh_ui_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import Grasshopper
import urllib2
from System import Convert
from System import Guid
from System.Drawing import Bitmap
from System.Drawing import Size
from System.IO import MemoryStream
Expand Down Expand Up @@ -81,8 +82,63 @@ def session_id():
return doc_key


def component_id(component, name):
return "{}_{}".format(component.InstanceGuid, name)
def component_id(session_id, component, name):
return "{}_{}_{}".format(session_id, component.InstanceGuid, name)


def clear_sticky(ghenv, st):
"""
Removes all items from the sticky dictionary use by Grasshopper components in the given document.
Resets all components that used the sticky to hold data.
Parameters
----------
ghenv: Grasshopper environment object `GhPython.Component.PythonEnvironment`
st: sticky dictionary
"""

ghdoc = ghenv.Component.OnPingDocument()
ghdoc_id = ghdoc.DocumentID.ToString()

keys = st.keys()

# The keys we are looking for are strings of the form "{session_id}_{guid_str}_{ghcomponent_nickname}".
# There might be other keys in the sticky dictionary, so we need to filter them out.
for key in keys:

try:
session_id = key.split("_")[0]
guid_str = key.split("_")[1]
except:
session_id = None
guid_str = None

if not session_id or not guid_str:
continue

# The retrieved session_id and guid_str may either come from a different Grasshopper document, or from some other process and be incorrect/meaningless.
# In these cases, the following code will do nothing anyway.
if session_id == ghdoc_id:
reset_component(ghdoc, guid_str)
st.pop(key)


def reset_component(ghdoc, guid_str):
"""
adapted from: https://github.com/compas-dev/compas/blob/ea4b5b5191a350d24cbb479c6770daa68dbe53fd/src/compas_ghpython/timer.py#L8
"""

guid = Guid(guid_str)
ghcomp = ghdoc.FindComponent(guid)

def callback(ghdoc):
if ghdoc.SolutionState != Grasshopper.Kernel.GH_ProcessStep.Process:
ghcomp.ExpireSolution(False)

if not ghcomp:
return
delay = 1 # [ms]
ghdoc.ScheduleSolution(delay, Grasshopper.Kernel.GH_Document.GH_ScheduleDelegate(callback))


def find_component_by_nickname(ghdoc, component_nickname):
Expand Down

0 comments on commit f9a89c8

Please sign in to comment.