How to set parameter as optional in Python 3 component

I have written some custom script components using the new Python 3 component. In the instructions it is shown that such components can be published directly or with contextual inputs connected to it.
I cannot use the contextual inputs as i need a domain input which is not possible with the selection of contextual inputs. Further i would prefer all my components to be contained within one Grasshopper definition for easier development. Therefore I am not using them.

Publishing the plugin works fine and i can use the components. However the components do not execute without all inputs populated. How do I change this? Will there be a parameter manager for the python modules?

1 Like

Switch the component into .sdk mode. Maybe in a Python 3 component you can then just use **kwargs, or even set defaults.

Otherwise, I know this works in Iron Python components: replace the first optional arg and all subsequent ones with *args, and then zip those together with the arg names from self.Params (or somewhere in the API to filter for ihe Input ones).

Any missing args are simply set to None by Grasshopper - your code within RunScript needs to work with this.

Thanks for replying!

I am afraid, I do not understand. In this simple script how would you implement the *args without it being interpreted as (invalid) parameter name?

import System
import Rhino
import Grasshopper

import rhinoscriptsyntax as rs

class MyComponent(Grasshopper.Kernel.GH_ScriptInstance):
    def RunScript(self, x, y, z):
        if x:
            a = x
        if a and y:
            a = a + y
        if a and z:
            a = a + z
        return a

like this:


      def RunScript(self, *args):
          kwargs = {param.NickName : arg
                    for param, arg in zip(self.Params.Input, args)
                   }

Thank you for the help, but I run into the same issue where *args becomes the value name. I tested it with both the IronPython and the Python3 component. Also it fails as the GH_ScriptInstance object has no object Params.

I’m not able to test this at the moment, but I used a ghpythonlib.componentbase.executingcomponent, not a Grasshopper.Kernel.GH_ScriptInstance.

It is weird how Grasshopper or the component is using z=2 as the input Param name. Maybe that’s one for the devs.

I am also having an issue with this, only when the components are published using the Unified Script Editor. I can get the input parameter to behave as an optional parameter (in the gh definition in which it is scripted) by using ghenv.Component.Params.Input[1].Optional = True but once it is published, it does not allow the component to run. Or I should say it does but frustratingly, not consistently. It seems to work in some of my custom python components and not in others and I cannot identify the difference.

Also probably a clue, if an input is connected and then unconnected, it then is fine with the optional parameter, which makes me think that the problem is trying to get the code to run once to set the optional parameter. ???

Oh interesting. Okay I logged that in to implement

RH-84579 Published component to look at optional property of inputs

RH-84580 Support for optional values in RunScript signature

Okay I made adjustments and improvements to how .Optional property of input parameters is carried to published components. This is gonna be in Rhino 8.14:

Published Script Components

When publishing a Script component, all inputs are now following the Optional state of the original source script component input. The script inputs have been and continue to be marked as Optional by default:

However you can now set the input to Required in the context menu or from inside the script by setting .Optional = False

Optional inputs now show (Optional) in description:

Plugins published before Rhino 8.14, will continue to show the warning on the components to keep the existing behaviour. New plugins will carry the .Optional settings as described above.

Published Contextual Components

When publishing a full Grasshopper definition with contextual inputs as a component, the published component inputs are only marked as optional, if the corresponding contextual input has sources connected to it. No changes here.

I will add these to the documentation website

2 Likes

Actually moved Required to where the rest of variable aspects are. I also need to find a good way to represent these visually on the component maybe:

1 Like

RH-84579 is fixed in Rhino 8 Service Release 14

1 Like