I am having issues with figuring out how to connect two grasshopper components programmatically. I want to be able to connect the output of one component to the input of another, which is what I am attempting here:
new_doc = GH_Document()
guidGem = Guid("2f9dce65-e9a1-59c3-8206-e6f2ec1d7a95")
guidRing = Guid("b94ec479-3909-0aac-69de-1abd3b3ae82a")
gem = Grasshopper.Instances.ComponentServer.EmitObject(guidGem)
ring = Grasshopper.Instances.ComponentServer.EmitObject(guidRing)
if gem and ring:
# Add the component to the document
new_doc.AddObject(gem, False)
new_doc.AddObject(ring, False)
output_param = gem.Params.Output[0]
input_param = ring.Params.Input[0]
input_param.AddSource(output_param)
# Save the new document to a file
archive = GH_Archive()
if archive.AppendObject(new_doc, "Definition"):
# Write the archive to a file
gh_file_path = "output.ghx"
success = archive.WriteToFile(gh_file_path, True, False)
if success:
print(f"Grasshopper file successfully saved to: {gh_file_path}")
else:
print("Failed to save the Grasshopper file.")
else:
print("Failed to serialize the Grasshopper document.")
else:
print("Failed to create the Drakon3D component. Please check the GUID.")
I am using Drakon as a plugin, and want to connect the Drakon components I emit through the component server ( I hope that this issue is not Drakon specific ). The code above fails, saying that the IGH_DocumentObject object has no attributes such as Params, which is alright, however I’m failing to find any resources that can point me toward how I can connect components programmatically.
Could someone give me at least an example of how this can be performed, or give me an alternative way of doing all of this?
Edit:
It is important for me to mention that I am using RhinoInside here, and I am not executing this python script in a Rhino environment.
These are my imports:
import rhinoinside
rhinoinside.load(R"C:\Program Files\Rhino 7\System")
import Rhino
gh = Rhino.RhinoApp.GetPlugInObject("Grasshopper")
gh.RunHeadless()
import Grasshopper
from Grasshopper.Kernel import GH_Document
from GH_IO.Serialization import GH_Archive
from System import Guid