From 34881cc5d2eb26bb1ad2de27a5858e01b8011a8a Mon Sep 17 00:00:00 2001 From: Aleksandra Apolinarska Date: Wed, 10 Apr 2024 14:25:13 +0200 Subject: [PATCH 1/5] #2 clear sticky + reset components working --- .../components/aixd_ProjectSetup/code.py | 8 ++-- src/aixd_grasshopper/gh_ui_helper.py | 42 +++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/aixd_grasshopper/components/aixd_ProjectSetup/code.py b/src/aixd_grasshopper/components/aixd_ProjectSetup/code.py index 7c60c87..e5c7fad 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) @@ -17,6 +13,8 @@ cid = component_id(ghenv.Component, "project_setup") 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..da87b05 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 @@ -85,6 +86,47 @@ def component_id(component, name): return "{}_{}".format(component.InstanceGuid, name) +def clear_sticky(ghenv, st): + """ + Removes all keys from the sticky dictionary. + Resets all components that used the sticky to hold data. + TODO: clear only keys from this session + + + Parameters: + ----------- + ghenv: Grasshopper environment object `GhPython.Component.PythonEnvironment` + st: sticky dictionary + """ + + ghdoc = ghenv.Component.OnPingDocument() + + keys = st.keys() + + st.clear() # TODO: clear only keys from this session + + for key in keys: + guid_str = key.split('_')[0] + reset_component(ghdoc, guid_str) + + +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) + + delay = 1 # [ms] + ghdoc.ScheduleSolution(delay, Grasshopper.Kernel.GH_Document.GH_ScheduleDelegate(callback)) + + def find_component_by_nickname(ghdoc, component_nickname): found = [] all_objects = ghdoc.Objects From 83949cebb5e531ace3f1f7db6e6c0f0eab3834f1 Mon Sep 17 00:00:00 2001 From: Aleksandra Apolinarska Date: Wed, 10 Apr 2024 14:53:42 +0200 Subject: [PATCH 2/5] #2 restrict cleaning to this session/doc only --- .../components/aixd_DatasetCreate/code.py | 2 +- .../components/aixd_DatasetLoad/code.py | 2 +- .../components/aixd_DatasetSummary/code.py | 2 +- .../components/aixd_Generator/code.py | 2 +- .../components/aixd_ModelDims/code.py | 2 +- .../components/aixd_ModelLoad/code.py | 2 +- .../components/aixd_ModelSetup/code.py | 2 +- .../components/aixd_ModelSummary/code.py | 2 +- .../components/aixd_ModelTrain/code.py | 2 +- .../components/aixd_PlotContours/code.py | 2 +- .../components/aixd_PlotCorrelations/code.py | 2 +- .../components/aixd_PlotDistributions/code.py | 2 +- .../components/aixd_ProjectSetup/code.py | 2 +- src/aixd_grasshopper/gh_ui_helper.py | 21 ++++++++++--------- 14 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/aixd_grasshopper/components/aixd_DatasetCreate/code.py b/src/aixd_grasshopper/components/aixd_DatasetCreate/code.py index 6b0473d..3329be0 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, "create_dataset_object") 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..7dcf9e4 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, "create_dataset_object") 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..a415115 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, "dataset_summary") 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..82940be 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, "run_generation") 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..e9974a7 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, "model_dims") 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..3956781 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, "model_load") 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..a6e4687 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, "model_setup") 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..ec7e68b 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, "model_summary") 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..e585ba8 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, "model_train") 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..e92d1eb 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, "create_dataset_object") if plot: diff --git a/src/aixd_grasshopper/components/aixd_PlotCorrelations/code.py b/src/aixd_grasshopper/components/aixd_PlotCorrelations/code.py index ee7a85e..fec564d 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, "create_dataset_object") if plot: diff --git a/src/aixd_grasshopper/components/aixd_PlotDistributions/code.py b/src/aixd_grasshopper/components/aixd_PlotDistributions/code.py index 7efff09..6e09617 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, "create_dataset_object") if plot: diff --git a/src/aixd_grasshopper/components/aixd_ProjectSetup/code.py b/src/aixd_grasshopper/components/aixd_ProjectSetup/code.py index e5c7fad..73b3dd4 100644 --- a/src/aixd_grasshopper/components/aixd_ProjectSetup/code.py +++ b/src/aixd_grasshopper/components/aixd_ProjectSetup/code.py @@ -10,7 +10,7 @@ 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, "project_setup") if set: clear_sticky(ghenv, st) diff --git a/src/aixd_grasshopper/gh_ui_helper.py b/src/aixd_grasshopper/gh_ui_helper.py index da87b05..9f91be6 100644 --- a/src/aixd_grasshopper/gh_ui_helper.py +++ b/src/aixd_grasshopper/gh_ui_helper.py @@ -82,32 +82,33 @@ 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 keys from the sticky dictionary. Resets all components that used the sticky to hold data. - TODO: clear only keys from this session - Parameters: ----------- ghenv: Grasshopper environment object `GhPython.Component.PythonEnvironment` st: sticky dictionary """ - + ghdoc = ghenv.Component.OnPingDocument() + ghdoc_id = ghdoc.DocumentID.ToString() keys = st.keys() - - st.clear() # TODO: clear only keys from this session - + for key in keys: - guid_str = key.split('_')[0] - reset_component(ghdoc, guid_str) + session_id = key.split("_")[0] + guid_str = key.split("_")[1] + + if session_id == ghdoc_id: + reset_component(ghdoc, guid_str) + st.pop(key) def reset_component(ghdoc, guid_str): From 31c123fc10a170c33abba9a54bbf7bfcedee2017 Mon Sep 17 00:00:00 2001 From: Aleksandra Apolinarska Date: Wed, 10 Apr 2024 14:58:39 +0200 Subject: [PATCH 3/5] #2 unify/correct component_id names --- src/aixd_grasshopper/components/aixd_DatasetCreate/code.py | 2 +- src/aixd_grasshopper/components/aixd_DatasetLoad/code.py | 2 +- src/aixd_grasshopper/components/aixd_DatasetSummary/code.py | 2 +- src/aixd_grasshopper/components/aixd_Generator/code.py | 2 +- src/aixd_grasshopper/components/aixd_ModelDims/code.py | 2 +- src/aixd_grasshopper/components/aixd_ModelLoad/code.py | 2 +- src/aixd_grasshopper/components/aixd_ModelSetup/code.py | 2 +- src/aixd_grasshopper/components/aixd_ModelSummary/code.py | 2 +- src/aixd_grasshopper/components/aixd_ModelTrain/code.py | 2 +- src/aixd_grasshopper/components/aixd_PlotContours/code.py | 2 +- src/aixd_grasshopper/components/aixd_PlotCorrelations/code.py | 2 +- src/aixd_grasshopper/components/aixd_PlotDistributions/code.py | 2 +- src/aixd_grasshopper/components/aixd_ProjectSetup/code.py | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/aixd_grasshopper/components/aixd_DatasetCreate/code.py b/src/aixd_grasshopper/components/aixd_DatasetCreate/code.py index 3329be0..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(session_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 7dcf9e4..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(session_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 a415115..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(session_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 82940be..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(session_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 e9974a7..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(session_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 3956781..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(session_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 a6e4687..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(session_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 ec7e68b..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(session_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 e585ba8..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(session_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 e92d1eb..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(session_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 fec564d..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(session_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 6e09617..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(session_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 73b3dd4..8466155 100644 --- a/src/aixd_grasshopper/components/aixd_ProjectSetup/code.py +++ b/src/aixd_grasshopper/components/aixd_ProjectSetup/code.py @@ -10,7 +10,7 @@ print("The path {} does not exist and will be now created.".format(complete_path)) os.mkdir(complete_path) - cid = component_id(session_id(), ghenv.Component, "project_setup") + cid = component_id(session_id(), ghenv.Component, "ProjectSetup") if set: clear_sticky(ghenv, st) From a0bedaabb63c4f0046d81e19f6cfa761882de03b Mon Sep 17 00:00:00 2001 From: "Aleksandra (Ania) Apolinarska" Date: Wed, 17 Apr 2024 09:43:13 +0200 Subject: [PATCH 4/5] .. Co-authored-by: Gonzalo Casas --- src/aixd_grasshopper/gh_ui_helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/aixd_grasshopper/gh_ui_helper.py b/src/aixd_grasshopper/gh_ui_helper.py index 9f91be6..b23c474 100644 --- a/src/aixd_grasshopper/gh_ui_helper.py +++ b/src/aixd_grasshopper/gh_ui_helper.py @@ -91,8 +91,8 @@ def clear_sticky(ghenv, st): Removes all keys from the sticky dictionary. Resets all components that used the sticky to hold data. - Parameters: - ----------- + Parameters + ---------- ghenv: Grasshopper environment object `GhPython.Component.PythonEnvironment` st: sticky dictionary """ From 4933bf63b8cda3b5c09d304ab6e198887c7e998e Mon Sep 17 00:00:00 2001 From: Aleksandra Apolinarska Date: Wed, 17 Apr 2024 11:38:44 +0200 Subject: [PATCH 5/5] #2 updated: session & component retrieval --- src/aixd_grasshopper/gh_ui_helper.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/aixd_grasshopper/gh_ui_helper.py b/src/aixd_grasshopper/gh_ui_helper.py index b23c474..21cff32 100644 --- a/src/aixd_grasshopper/gh_ui_helper.py +++ b/src/aixd_grasshopper/gh_ui_helper.py @@ -88,7 +88,7 @@ def component_id(session_id, component, name): def clear_sticky(ghenv, st): """ - Removes all keys from the sticky dictionary. + 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 @@ -102,10 +102,22 @@ def clear_sticky(ghenv, st): 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: - session_id = key.split("_")[0] - guid_str = key.split("_")[1] + 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) @@ -120,10 +132,11 @@ def reset_component(ghdoc, 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))