diff --git a/src/compas_timber/ghpython/components/CT_Joint_Rule_Category/code.py b/src/compas_timber/ghpython/components/CT_Joint_Rule_Category/code.py index 9c5a1b66f..60c813f7f 100644 --- a/src/compas_timber/ghpython/components/CT_Joint_Rule_Category/code.py +++ b/src/compas_timber/ghpython/components/CT_Joint_Rule_Category/code.py @@ -7,22 +7,27 @@ from compas_timber.ghpython import manage_dynamic_params - class CategoryJointRule(component): def RunScript(self, joint_options, *args): - names = [name + " category" for name in joint_options.beam_names] + if joint_options: + names = [name + " category" for name in joint_options.beam_names] + else: + names = None + + manage_dynamic_params(names, ghenv) - manage_dynamic_params(names) + if not args or not names: # check that dynamic params generated + return - if len(args) != len(names): # check that dynamic params are correct + if len(args) != len(names): # check that dynamic params generated correctly self.AddRuntimeMessage(Error, "Input parameter error.") + return create_rule = True for i in range(len(names)): if not args[i]: self.AddRuntimeMessage( - Warning, - "Input parameter {} {} failed to collect data.".format(names[i], "categories"), + Warning, "Input parameter {} {} failed to collect data.".format(names[i], "categories") ) create_rule = False if create_rule: diff --git a/src/compas_timber/ghpython/components/CT_Joint_Rule_Direct/code.py b/src/compas_timber/ghpython/components/CT_Joint_Rule_Direct/code.py index 72f6a9a9d..9b908d035 100644 --- a/src/compas_timber/ghpython/components/CT_Joint_Rule_Direct/code.py +++ b/src/compas_timber/ghpython/components/CT_Joint_Rule_Direct/code.py @@ -11,27 +11,35 @@ class DirectJointRule(component): def RunScript(self, joint_options, *args): + names = None + if joint_options: + names = [name + " category" for name in joint_options.beam_names] - manage_dynamic_params(joint_options.beam_names) + manage_dynamic_params(names, ghenv) - if len(args) != len(joint_options.beam_names): # check that dynamic params are correct + if not args or not names: # check that dynamic params generated + return + + if len(args) != len(names): # check that dynamic params generated correctly self.AddRuntimeMessage(Error, "Input parameter error.") + return beams = [] create_rule = True for i in range(len(joint_options.beam_names)): - if not args[i]: + if not args[i]: # test that input recieved data self.AddRuntimeMessage( Warning, "Input parameter {} failed to collect data.".format(joint_options.beam_names[i]) ) create_rule = False else: + beam_args = [] if not isinstance(args[i], list): - args[i] = [args[i]] - beams.append(args[i]) + beam_args = [args[i]] + beams.append(beam_args) if create_rule: - if len(beams[0]) != len(beams[1]): + if len(beams[0]) != len(beams[1]): # test that beam list lengths match self.AddRuntimeMessage( Error, "Number of items in {} and {} must match!".format( diff --git a/src/compas_timber/ghpython/ghcomponent_helpers.py b/src/compas_timber/ghpython/ghcomponent_helpers.py index 1bd192bda..d415d406f 100644 --- a/src/compas_timber/ghpython/ghcomponent_helpers.py +++ b/src/compas_timber/ghpython/ghcomponent_helpers.py @@ -1,5 +1,6 @@ from Grasshopper.Kernel.GH_RuntimeMessageLevel import Remark from Grasshopper.Kernel.GH_RuntimeMessageLevel import Warning +import Grasshopper def list_input_valid(component, Param, name): @@ -24,7 +25,7 @@ def item_input_valid(component, Param, name): return False -def add_GH_param(name, io): +def add_GH_param(name, io, ghenv): assert io in ("Output", "Input") params = [param.NickName for param in getattr(ghenv.Component.Params, io)] if name not in params: @@ -40,17 +41,18 @@ def add_GH_param(name, io): ghenv.Component.Params.OnParametersChanged() -def clear_GH_params(permanent_param_count = 1): - for param in ghenv.Component.Params.Input: - if param.Index >= permanent_param_count: - ghenv.Component.Params.UnregisterInputParameter(param) +def clear_GH_params(ghenv, permanent_param_count=1): + 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.OnParametersChanged() ghenv.Component.ExpireSolution(False) -def manage_dynamic_params(input_names, permanent_param_count = 1): +def manage_dynamic_params(input_names, ghenv, permanent_param_count=1): if not input_names: # if no joint_options is input - clear_GH_params(permanent_param_count) + clear_GH_params(ghenv, permanent_param_count) return register_params = False if len(ghenv.Component.Params.Input) == len(input_names) + permanent_param_count: @@ -61,6 +63,6 @@ def manage_dynamic_params(input_names, permanent_param_count = 1): else: register_params = True if register_params: - clear_GH_params(permanent_param_count) + clear_GH_params(ghenv, permanent_param_count) for name in input_names: - add_GH_param(name, "Input") + add_GH_param(name, "Input", ghenv) diff --git a/src/compas_timber/ghpython/workflow.py b/src/compas_timber/ghpython/workflow.py index d9869c4da..5a5344a58 100644 --- a/src/compas_timber/ghpython/workflow.py +++ b/src/compas_timber/ghpython/workflow.py @@ -285,7 +285,7 @@ class JointOptions(object): """ - def __init__(self, type, beam_names = [], **kwargs): + def __init__(self, type, beam_names=[], **kwargs): self.type = type self.kwargs = kwargs self.beam_names = beam_names