Creating a default value for a GHPY compiled component

Hi Giulio, I was looking for a way to add a Default value by GHPY but no lack.
As you can understand, I would like to add a default string value i.e. “S235” or a float. 210000.
What is the correct way?
Can you be kind to show also how to add a default vector? i.e. rg.Vector3d(0,0,0)?

Best,
Marco

def RegisterInputParams(self, pManager):
    p = Grasshopper.Kernel.Parameters.Param_String()
    self.SetUpParam(p, "matName", "matName", "Name of the material.")
    p.Access = Grasshopper.Kernel.GH_ParamAccess.item
    self.Params.Input.Add(p)
    
    p = Grasshopper.Kernel.Parameters.Param_Number()
    self.SetUpParam(p, "E", "E", "Young's Modulus [MPa].")
    p.Access = Grasshopper.Kernel.GH_ParamAccess.item
    self.Params.Input.Add(p)

Hi @mp1

do you plan on using this in a GHPY compiled component? I would use GHPYs as absolute last resort, when you don’t want to publish source code.

If you are creting a larger library, I’d suggest to switch to compiled C# in VS.

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Hi @piac.
I didn’t want want to spend time learning C# and I am compiling a project by .ghpy at the moment.
I have realised that maybe in the future c# will be better but I am at the very end of a project.
Is it hard to have a default value that the user can see?
I am applying default value inside the script but I was tempted to have a more elegant way.

Hi Marco @mp1

I moved the conversation to a new topic, so it might be useful also to other users.

OK; then it makes sense.

We need to dig a little deeper into the Grasshopper SDK. Here is how:

def RegisterInputParams(self, pManager):
    p = Grasshopper.Kernel.Parameters.Param_String()
    self.SetUpParam(p, "matName", "matName", "Name of the material.")
    p.Access = Grasshopper.Kernel.GH_ParamAccess.item
    p.SetPersistentData(Grasshopper.Kernel.Types.GH_String("Test"))
    self.Params.Input.Add(p)
    
    p = Grasshopper.Kernel.Parameters.Param_Number()
    self.SetUpParam(p, "E", "E", "Young's Modulus [MPa].")
    p.Access = Grasshopper.Kernel.GH_ParamAccess.item
    p.SetPersistentData(System.Array[float]([42.0]))
    self.Params.Input.Add(p)

There’s nothing bad at treating None as a particular value. Python has the feature of null values also for numbers.

I hope this helps,
Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

3 Likes