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)
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.
"""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"