Can I instantiate specific component on the canvas with a script [Python]

And at a particular location?

like this? no I don’t think its possible :stuck_out_tongue:

1 Like

Yes, you can. I can post some better examples when I get back to the office, but there is some stuff in here for creating and placing panel components at specific locations.

Also, this. (could be ported to python):


Cant find the code. Basically its GrasshopperDocument.AddObject(…) or GrasshopperDocument.Objects.Add(…). You can also make them connect to wires etc.

oh here it is, is C# but should be easy to translate:

if (create)
  Random r = new Random(23);
  int min = 0;
  int step = 20;
  int max = step;
  Grasshopper.Kernel.Parameters.Param_GenericObject comp = null;
  Grasshopper.Kernel.Parameters.Param_GenericObject lastComp = null;
  for (int i = 0; i < 1000; i++)

    min += step;
    max += step;
    for (int j = 0; j < 100; j++)
      comp = new Grasshopper.Kernel.Parameters.Param_GenericObject();
      GrasshopperDocument.AddObject(comp, false, GrasshopperDocument.ObjectCount);
      comp.Attributes.Pivot = new System.Drawing.PointF((float) r.Next(min, max), (float) r.Next(0, 2000));

    if (lastComp != null && comp != null)

    lastComp = comp;

1 Like

Thanks Chris

Thanks @TomTom, I’ll try to translate it. I’m going to have some fun during the weekend :smiley:

Ported Python Example from link aboce: SDK mode, Rhino 6
Set y input to list access.
Disclaimer: I think there are some challenges with these types of operations. I forget exactly, but you might want to double check if you need to expire anything.

# ported from C#
from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System
import os
import System.Drawing as sd
import random

class MyComponent(component):
    def RunScript(self, MakeSlider,y):
        if MakeSlider:
    def make_Slider(self):
            randomVal = random.random()
            #instantiate  new slider
            slid = Grasshopper.Kernel.Special.GH_NumberSlider()
            slid.CreateAttributes() #sets up default values, and makes sure your slider doesn't crash rhino
            #customise slider (position, ranges etc)
            inputcount = self.Params.Input[1].SourceCount
            slid.Attributes.Pivot = sd.PointF(self.Attributes.DocObject.Attributes.Bounds.Left - slid.Attributes.Bounds.Width - 30, self.Params.Input[1].Attributes.Bounds.Y + inputcount * 30)
            slid.Slider.Maximum = 10
            slid.Slider.Minimum = 0
            slid.Slider.DecimalPlaces = 2
            slid.SetSliderValue((randomVal * 10));
            #add slider to canvas.
            ghdoc = self.OnPingDocument()
            ghdoc.AddObject(slid, False)
            #Connect the new slider to this component
            if inputcount > 5:
                System.Windows.Forms.MessageBox.Show(str("Okay, let's not get carried away"))
        except Exception, ex:
            self.AddRuntimeMessage(Grasshopper.Kernel.GH_RuntimeMessageLevel.Warning, str(ex))
            print ex (6.7 KB)


Thank you Chris, I’ll check it out.

Much appreciated, saved me a lot of thinking about translating that mess of a language into Python. (@TomTom, I meant the language, not your code :wink: )

no worries! I did it on my lunchbreak. To clarify, the example I posted is from the link to James Ramsden’s C# example.
With that said, I feel like there are some issues, (that I can’t quite remember), that need to be considered when automatically instantiating components…
Proceed with caution!

1 Like

Hey all, I’m bumping this topic for the following reason. I am trying to instantiate point parameters on the grasshopper canvas which seems to kind of be working using an adaptation of a .py file from @chanley (Thanks :slight_smile: ).

I am trying to instantiate the points with preset parameters that might be inputted upstream, say from a different GH component. I’ve been doing some digging through the SDK, but cant seem to find how to “set” the point parameters onto the grasshopper canvas. Here is a screenshot of the code:

Is it possible to get the points to appear on the canvas with preset x,y,z values where one could hypothetically turn on the grasshopper gumball and move them around in rhino space?

Any guidance is appreciated, Thanks!