Programmatically creating new C# & Python script components

Going to put this in the next Rhino. I would appreciate your feedback and improvement ideas:

# r "RhinoCodePluginGH.rhp"                 // this is necessary to access script component types
using System;
using Grasshopper;
using Grasshopper.Kernel;

// namespaces to access script components and variable parameter
using RhinoCodePluginGH.Components;
using RhinoCodePluginGH.Parameters;

// this creates a python 3 component.
// could also be:
// CSharpComponent.Create
// IronPython2Component.Create
Python3Component c = Python3Component.Create("MyScript", @"
# here is your custom python script going
# into the component
");

var p = new ScriptVariableParam("first")    // this is the variable name for script
{
    PrettyName = "First Input",             // pretty name is human-readable
    ToolTip = "This is the first input",
    Optional = true,
    Access = GH_ParamAccess.list,
    AllowTreeAccess = true,
};
p.TypeHints.Select(typeof(double));         // .TypeHints is new helper to set type hints
p.CreateAttributes();
c.Params.RegisterInputParam(p);

p = new ScriptVariableParam("second")
{
    PrettyName = "Second Input",
    ToolTip = "This is the second input",
    Optional = true,
    AllowTreeAccess = true,
};
p.TypeHints.Select("Point3dList");          // .TypeHints helper can set hints by name as well
p.CreateAttributes();
c.Params.RegisterInputParam(p);

p = new ScriptVariableParam("output")
{
    Hidden = true,
};
p.CreateAttributes();
c.Params.RegisterOutputParam(p);

c.VariableParameterMaintenance();           // necessary step after changing component parameters


// get and set script on a component
c.TryGetSource(out string source);
c.SetSource("# new source code to be set into component");

// special parameters can be accessed and enabled if necessary
c.UsingStandardOutputParam = true;
c.GraftStandardOutputLines = true;

// marshalling settings can be changed as well
c.MarshGuids = false;

// parameters can also be applied or collected from the source
c.SetParametersToScript()        // this updates RunScript signature to match component params
c.SetParametersFromScript()      // this updates the component parameters from RunScript signature
3 Likes