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):

2 Likes

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)
    {
      comp.AddSource(lastComp);
    }



    //comp.Params.Input[0].AddSource(lastComp);
    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.
Capture

# ported from C#
#source: http://james-ramsden.com/instantiate-grasshopper-component/
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:
            self.make_Slider()
        else:
            pass
            
    def make_Slider(self):
        try:
            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
            self.Params.Input[1].AddSource(slid)
            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

InstantiateCompsPY.gh (6.7 KB)

1 Like

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