diff --git a/src/aixd_grasshopper/components/aixd_DatasetCreate/code.py b/src/aixd_grasshopper/components/aixd_DatasetCreate/code.py index 6b0473d..a63dd02 100644 --- a/src/aixd_grasshopper/components/aixd_DatasetCreate/code.py +++ b/src/aixd_grasshopper/components/aixd_DatasetCreate/code.py @@ -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] diff --git a/src/aixd_grasshopper/components/aixd_DatasetLoad/code.py b/src/aixd_grasshopper/components/aixd_DatasetLoad/code.py index 84cc85c..5ed3053 100644 --- a/src/aixd_grasshopper/components/aixd_DatasetLoad/code.py +++ b/src/aixd_grasshopper/components/aixd_DatasetLoad/code.py @@ -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()) diff --git a/src/aixd_grasshopper/components/aixd_DatasetSummary/code.py b/src/aixd_grasshopper/components/aixd_DatasetSummary/code.py index ca6fa91..7a4664e 100644 --- a/src/aixd_grasshopper/components/aixd_DatasetSummary/code.py +++ b/src/aixd_grasshopper/components/aixd_DatasetSummary/code.py @@ -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()) diff --git a/src/aixd_grasshopper/components/aixd_Generator/code.py b/src/aixd_grasshopper/components/aixd_Generator/code.py index 62d2001..50aac37 100644 --- a/src/aixd_grasshopper/components/aixd_Generator/code.py +++ b/src/aixd_grasshopper/components/aixd_Generator/code.py @@ -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: diff --git a/src/aixd_grasshopper/components/aixd_ModelDims/code.py b/src/aixd_grasshopper/components/aixd_ModelDims/code.py index 3ade311..d75c13c 100644 --- a/src/aixd_grasshopper/components/aixd_ModelDims/code.py +++ b/src/aixd_grasshopper/components/aixd_ModelDims/code.py @@ -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()) diff --git a/src/aixd_grasshopper/components/aixd_ModelLoad/code.py b/src/aixd_grasshopper/components/aixd_ModelLoad/code.py index 77aa85f..1a5d883 100644 --- a/src/aixd_grasshopper/components/aixd_ModelLoad/code.py +++ b/src/aixd_grasshopper/components/aixd_ModelLoad/code.py @@ -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 = "" diff --git a/src/aixd_grasshopper/components/aixd_ModelSetup/code.py b/src/aixd_grasshopper/components/aixd_ModelSetup/code.py index c6e9b80..06eb9be 100644 --- a/src/aixd_grasshopper/components/aixd_ModelSetup/code.py +++ b/src/aixd_grasshopper/components/aixd_ModelSetup/code.py @@ -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, diff --git a/src/aixd_grasshopper/components/aixd_ModelSummary/code.py b/src/aixd_grasshopper/components/aixd_ModelSummary/code.py index baf78c4..93d1388 100644 --- a/src/aixd_grasshopper/components/aixd_ModelSummary/code.py +++ b/src/aixd_grasshopper/components/aixd_ModelSummary/code.py @@ -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: diff --git a/src/aixd_grasshopper/components/aixd_ModelTrain/code.py b/src/aixd_grasshopper/components/aixd_ModelTrain/code.py index 084c07d..c2e8422 100644 --- a/src/aixd_grasshopper/components/aixd_ModelTrain/code.py +++ b/src/aixd_grasshopper/components/aixd_ModelTrain/code.py @@ -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: diff --git a/src/aixd_grasshopper/components/aixd_PlotContours/code.py b/src/aixd_grasshopper/components/aixd_PlotContours/code.py index 3ab51ca..98901f0 100644 --- a/src/aixd_grasshopper/components/aixd_PlotContours/code.py +++ b/src/aixd_grasshopper/components/aixd_PlotContours/code.py @@ -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: diff --git a/src/aixd_grasshopper/components/aixd_PlotCorrelations/code.py b/src/aixd_grasshopper/components/aixd_PlotCorrelations/code.py index ee7a85e..a32a92c 100644 --- a/src/aixd_grasshopper/components/aixd_PlotCorrelations/code.py +++ b/src/aixd_grasshopper/components/aixd_PlotCorrelations/code.py @@ -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: diff --git a/src/aixd_grasshopper/components/aixd_PlotDistributions/code.py b/src/aixd_grasshopper/components/aixd_PlotDistributions/code.py index 7efff09..fadd476 100644 --- a/src/aixd_grasshopper/components/aixd_PlotDistributions/code.py +++ b/src/aixd_grasshopper/components/aixd_PlotDistributions/code.py @@ -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: diff --git a/src/aixd_grasshopper/components/aixd_ProjectSetup/code.py b/src/aixd_grasshopper/components/aixd_ProjectSetup/code.py index 7c60c87..8466155 100644 --- a/src/aixd_grasshopper/components/aixd_ProjectSetup/code.py +++ b/src/aixd_grasshopper/components/aixd_ProjectSetup/code.py @@ -1,12 +1,8 @@ # 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) @@ -14,9 +10,11 @@ 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(): diff --git a/src/aixd_grasshopper/gh_ui_helper.py b/src/aixd_grasshopper/gh_ui_helper.py index 32dee15..21cff32 100644 --- a/src/aixd_grasshopper/gh_ui_helper.py +++ b/src/aixd_grasshopper/gh_ui_helper.py @@ -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 @@ -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):