Skip to content

Commit

Permalink
#17 unified set & get method
Browse files Browse the repository at this point in the history
  • Loading branch information
funkchaser committed May 1, 2024
1 parent cf42081 commit eb0ec98
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 42 deletions.
40 changes: 7 additions & 33 deletions src/aixd_grasshopper/generate_dataset_rhinoscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

import rhinoscriptsyntax as rs

from aixd_grasshopper.gh_ui_helper import TYPES, find_component_by_nickname, http_post_request
from aixd_grasshopper.gh_ui_helper import TYPES
from aixd_grasshopper.gh_ui_helper import find_component_by_nickname
from aixd_grasshopper.gh_ui_helper import ghparam_get_values
from aixd_grasshopper.gh_ui_helper import ghparam_set_values
from aixd_grasshopper.gh_ui_helper import http_post_request

try:
import Rhino
Expand All @@ -30,39 +34,9 @@
session_id = ghdoc.DocumentID.ToString()


# -------------------------------------------------------------------------------
# GH interface


def set_values(component, vals):
"""
Data type of vals must match the type of the component.
See TYPES list.
"""
ghtype = TYPES[component.TypeName]

component.Script_ClearPersistentData()
if not isinstance(vals, list):
vals = [vals]
for v in vals:
component.PersistentData.Append(ghtype(v))
component.ExpireSolution(False)


def get_values(component):
component.CollectData()
component.ComputeData()

if not component.VolatileData:
return None

return [x.Value for x in component.VolatileData[0]]


# -------------------------------------------------------------------------------
# API app


def generate_dp_samples(n_samples):
return http_post_request("generate_dp_samples", {"session_id": session_id, "n_samples": n_samples})

Expand Down Expand Up @@ -95,15 +69,15 @@ def analysis_callback(ghdoc, dp_samples, pa_names):
# if dp_name == 'uid': continue
component_name = "GENERATED_{}".format(dp_name)
component = find_component_by_nickname(ghdoc, component_name)
set_values(component, dp_vals)
ghparam_set_values(component, dp_vals, expire=False)

pa_dict = {k: [] for k in pa_names}
# pa_dict['uid']=uid
for pa_name in pa_names:

component_name = "REAL_{}".format(pa_name)
component = find_component_by_nickname(ghdoc, component_name)
pa_vals = get_values(component)
pa_vals = ghparam_get_values(component, compute=True)
if isinstance(pa_vals, list):
if len(pa_vals) == 1:
pa_vals = pa_vals[0] # unpack from list
Expand Down
19 changes: 10 additions & 9 deletions src/aixd_grasshopper/gh_ui_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,28 +143,29 @@ def find_component_by_nickname(ghdoc, component_nickname):
# set & get values methods (rhinopythonscript style)


def set_value(component, val):
component.Script_ClearPersistentData()
component.AddPersistentData(val)
component.ExpireSolution(True)


def set_values(component, vals):
def ghparam_set_values(component, vals, expire=True):
"""
Data type of vals must match the type of the component.
See TYPES list.
"""
ghtype = TYPES[component.TypeName]

component.Script_ClearPersistentData()
if not isinstance(vals, list):
vals = [vals]
for v in vals:
component.PersistentData.Append(ghtype(v))
component.ExpireSolution(True)
component.ExpireSolution(expire)


def get_values(component):
def ghparam_get_values(component, compute=False):
if compute:
component.CollectData()
component.ComputeData()

if not component.VolatileData:
return None

return [x.Value for x in component.VolatileData[0]]


Expand Down

0 comments on commit eb0ec98

Please sign in to comment.