Component that loads a cluster in ghpython

Hi,

My question is related to this post from the old forum:
https://www.grasshopper3d.com/forum/topics/custom-vb-c-python-etc-component-that-loads-a-cluster-and-or-gh

The post describes how to run and harvest the results from a ghcluster file, but I am struggling to make it work when the cluster require input.

I have tried to create a string component and attach this component to the cluster:

import Grasshopper
s=r"C:\Temp\Cluster.ghcluster"

cluster=Grasshopper.Kernel.Special.GH_Cluster()
cluster.CreateFromFilePath(s)

str_var=Grasshopper.Kernel.Parameters.Param_String()  
str_var.SetPersistentData(Grasshopper.Kernel.Types.GH_String("sample string"))

doc=Grasshopper.Kernel.GH_Document()
doc.Enabled = True
doc.AddObject(cluster, True, 0)
doc.AddObject(str_var, True, 0)

cluster.Params.RegisterInputParam(str_var)

cluster.CollectData()
cluster.ComputeData()

data = cluster.Params.Output[0].VolatileData

empty_tree = Grasshopper.DataTree[object]()
empty_tree.MergeStructure(data, Grasshopper.Kernel.Parameters.Hints.GH_NullHint())

out_var = empty_tree

doc.Enabled = False
doc.RemoveObject(cluster, False)
doc.Dispose()
doc = None

The cluster I am running is supposed to just pass the string variable through.
Output variables that are generated without an input works fine, and I am able to collect these, but output variables that are dependent on input remains equal to “None”.

Best regards
Audun Mathias Øvstebø

#python
import Grasshopper as gh
cluster = gh.Kernel.Special.GH_Cluster()
cluster.CreateFromFilePath(path)
cluster.Params.Input[0].AddVolatileData(gh.Kernel.Data.GH_Path(0), 0, input)
doc = gh.Kernel.GH_Document()
doc.Enabled = True
doc.AddObject(cluster, True, 0)
structure = cluster.Params.Output[0].VolatileData
tree = gh.DataTree[object]()
tree.MergeStructure(structure, gh.Kernel.Parameters.Hints.GH_NullHint())
a = tree
doc.Enabled = False
doc.RemoveObject(cluster, False)
doc.Dispose()
doc = None
//C#
private void RunScript(string path, int input, ref object A)
{
  var cluster = new GH_Cluster();
  cluster.CreateFromFilePath(path);
  cluster.Params.Input[0].AddVolatileData(new GH_Path(0), 0, input);
  var doc = new GH_Document();
  doc.Enabled = true;
  doc.AddObject(cluster, true, 0);
  var structure = cluster.Params.Output[0].VolatileData;
  var tree = new DataTree<object>();
  tree.MergeStructure(structure, new Grasshopper.Kernel.Parameters.Hints.GH_NullHint());
  A = tree;
  doc.Enabled = false;
  doc.RemoveObject(cluster, false);
  doc.Dispose();
  doc = null;
}

LoadCluster.gh (5.1 KB)
Cluster.ghcluster (1.7 KB)

Absolutely perfect, thanks a lot! :grinning: