Skip to content

Commit

Permalink
cant make it break
Browse files Browse the repository at this point in the history
  • Loading branch information
obucklin committed Feb 19, 2024
1 parent dc9c070 commit 477c725
Showing 1 changed file with 68 additions and 16 deletions.
84 changes: 68 additions & 16 deletions src/compas_timber/ghpython/ghcomponent_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,23 @@ def item_input_valid(component, Param, name):
return False


def add_GH_param(name, io, ghenv):
def add_GH_param(name, io, ghenv): #we could also make beam_names a dict with more info e.g. NickName, Description, Access, hints, etc. this would be defined in joint_options components
"""Adds a parameter to the Grasshopper component.
Parameters
----------
name : str
The name of the parameter.
io : str
The direction of the parameter. Either "Input" or "Output".
ghenv : object
The Grasshopper environment object.
Returns
-------
None
"""
assert io in ("Output", "Input")
params = [param.NickName for param in getattr(ghenv.Component.Params, io)]
if name not in params:
Expand All @@ -42,27 +58,63 @@ def add_GH_param(name, io, ghenv):


def clear_GH_params(ghenv, permanent_param_count=1):
"""Clears all input parameters from the component.
Parameters
----------
ghenv : object
The Grasshopper environment object.
permanent_param_count : int, optional
The number of parameters that should not be deleted. Default is 1.
Returns
-------
None
"""
changed = False
while len(ghenv.Component.Params.Input) > permanent_param_count:
ghenv.Component.Params.UnregisterInputParameter(
ghenv.Component.Params.Input[len(ghenv.Component.Params.Input) - 1]
ghenv.Component.Params.Input[len(ghenv.Component.Params.Input) - 1],
True
)
ghenv.Component.Params.OnParametersChanged()
ghenv.Component.ExpireSolution(False)
changed = True
if changed:
ghenv.Component.ExpireSolution(True)


def manage_dynamic_params(input_names, ghenv, permanent_param_count=1):
if not input_names: # if no joint_options is input
"""Clears all input parameters from the component.
Parameters
----------
input_names : list(str)
The names of the input parameters.
ghenv : object
The Grasshopper environment object.
permanent_param_count : int, optional
The number of parameters that should not be deleted. Default is 1.
Returns
-------
None
"""
if not input_names: # if no names are input
clear_GH_params(ghenv, permanent_param_count)
return
register_params = False
if len(ghenv.Component.Params.Input) == len(input_names) + permanent_param_count:
for i, name in enumerate(input_names):
if ghenv.Component.Params.Input[i + permanent_param_count].Name != name:
register_params = True
break
else:
register_params = True
if register_params:
clear_GH_params(ghenv, permanent_param_count)
for name in input_names:
add_GH_param(name, "Input", ghenv)
register_params = False
if len(ghenv.Component.Params.Input) == len(input_names) + permanent_param_count: #if param count matches beam_names count
for i, name in enumerate(input_names):
if ghenv.Component.Params.Input[i + permanent_param_count].Name != name: #if param names don't match
register_params = True
break
else:
register_params = True
if register_params:
clear_GH_params(ghenv, permanent_param_count) #we could consider renaming params if we don't want to disconnect GH component inputs
for name in input_names:
add_GH_param(name, "Input", ghenv)
ghenv.Component.ExpireSolution(True)

0 comments on commit 477c725

Please sign in to comment.