GH Python : Create, Connect and delete components in canvas

Hello,

I’m working on a script to instantiate different connected objects (either with a specific name or in a group) a dynamic number of times (depending on a slider value).

My first approach is to on a slider change (delayed value compared with actual one):

  • delete all existing objects starting with a given name
  • get the objects to create
  • create these objects n times
  • connect them correctly

Can this be done using the python component?

I’m able to get the objects, but do not know how to delete/add or connect them…

Best regards

import Grasshopper as gh

ghObjects = ghenv.Component.OnPingDocument().Objects

if number != number_before:
    for idx, obj in enumerate(ghObjects):
        if obj.NickName.startswith("ADD_"):
            print(obj.NickName)

Hi
Yes it’s possible to do what you want,if you search in forum you can find pieces of code you are looking for and then combine them.( Like this : Bringing Fun to Workflow: "TTP224" in Grasshopper)

Hello sadra,

thanks for the quick reply. Yes it seems that you are doing part of it in video 2. But all the code I’m able to find in the forum is related to Rhino objects and not grasshopper components.

Hello sadra,

work is progressing, but I still face a issue:

  • I get this error after one run
"""Grasshopper Script"""
import Grasshopper as gh
from System import Guid, Drawing

components_to_delete = []
components_to_add = []

doc = gh.Instances.ActiveCanvas.Document

if number != number_before:
    for obj in doc.Objects:
        if obj.NickName.startswith("ADD_"):
            components_to_delete.append(obj.Attributes.InstanceGuid)
        if obj.NickName.startswith("XXX_"):
            obj_coord = obj.Attributes.Pivot
            for i in range(0, int(number)):
                obj_dict = dict(name = str(obj.NickName).replace("XXX", "ADD_"+str(i)), guid = str(obj.ComponentGuid), x = obj_coord.X+int(offset_x), y = obj_coord.Y+200*(i+1))
                components_to_add.append(obj_dict)

    for comp in components_to_delete:
        doc = gh.Instances.ActiveCanvas.Document
        delete = ghenv.Component.OnPingDocument().FindObject(comp, True)
        doc.RemoveObject(delete, True)

    canvas = gh.Instances.ActiveCanvas
    if number > 0:
        for comp in components_to_add:
            canvas.InstantiateNewObject(Guid(comp['guid']), Drawing.PointF(comp['x'],comp['y']), True)
            canvas.Document.Objects[canvas.Document.Objects.Count-1].NickName = comp['name']
    
a = "Done"

My code(for video 2) is very simple but can give you some ideas :

import Grasshopper as gh
import System
import System.Drawing as sd 

ghdoc = ghenv.Component.OnPingDocument()

oh1 = gh.Instances.ActiveCanvas.Viewport.MidPoint

def createSlider():
    mySlider = gh.Kernel.Special.GH_ParamViewer()
    mySlider.CreateAttributes()
    mySlider.Attributes.Pivot = System.Drawing.PointF(oh1.X,oh1.Y)
    return mySlider

you can check topics like this : Modify Wires after instantiate component - #2 by Mahdiyar