Passing objects inside Grasshopper

Hi all,
I was wondering if I can pass objects in between grasshopper c# scripts. I have created this object in one c# componant:
public class Beam
{
public int Index;
public Brep geom;
// Fields, properties, methods and events go here…
}
I have passed it out like this:
Beam b1 = new Beam();
b1.geom = Geometry;
b1.Index = index;

A = b1;

now the output from this c# component looks like this:
Script_Instance+Beam

I am passing this output into another C# component that also includes this class of this object:
Beam n = new Beam();
n = (Beam) y;
A = n.geom;
I am expecting the output to be the geometry of the object that I have passed but then I get the error that I cant cast the the old type to the new type:

  1. [A]Beam cannot be cast to [B]Beam. Type A originates from ‘644ef5d2-b91e-4324-b250-da1c65ecf16e, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’ in the context ‘LoadFrom’ at location ‘C:\Users\Ghaith89\AppData\Local\Temp\644ef5d2-b91e-4324-b250-da1c65ecf16e.dll’. Type B originates from ‘ca2aec6b-88c3-4272-9b19-48c619040360, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’ in the context ‘LoadFrom’ at location ‘C:\Users\Ghaith89\AppData\Local\Temp\ca2aec6b-88c3-4272-9b19-48c619040360.dll’. (line: 59)

Is there a way around to pass new created objects in Grasshopper?

PassingObjects.ghx (37.9 KB) passObjects.3dm (26.4 KB)

There are a number of discussions about this. None are perfect:

https://www.grasshopper3d.com/m/discussion?id=2985220%3ATopic%3A1330719

https://www.grasshopper3d.com/m/discussion?id=2985220%3ATopic%3A1330719

1 Like

Thanks Scott !
passing the instances is what I am looking for. the functionality I can always copy/paste

Hi, Tuple, List and Dictionary can be passed between multiple C# components. Functions can be wrapped by Action and can be passed. Instead of coming up a tricky idea, try managing things by those native data types would be the first choice.

//sender
var newtuple=new Tuple<int,Brep>(Geometry,index);
A=newtuple;

//reciever
var tuple=x as Tuple<int,Brep>;
Brep A=tuple.Item2;
int n=tuple.Item1;

another solution using dictionary is

//sender
var dict=new Dicationary<string,object>();
dict["index"]=index;
dict["brep"]=Geometry;
A=dict;

//reciever
var dict=x as Dictionary<string,object>;
Brep A=dict["brep"] as Brep;
int n=(int) dict["index"];
2 Likes

the receiving script didn’t really work in the dictionary case.
it shows this error

  1. Object reference not set to an instance of an object. (line: 60)
    what input type shall I use in the receiver ?

Sorry, this should work. It seems like a Dictionary is broke down to the keypairs by Grasshopper automatically when exposed to Grasshopper environment. It needs to be wrapped by something to be handed over safely, which is a List in the following code. (this also happens in Python and I usually wraps by a tuple)

int index = 1;
Brep Geometry = new Brep();
//sender
var dict = new Dictionary<string,object>();
dict["index"] = index;
dict["brep"] = Geometry;
A = new List<System.Object>(){dict};

**Change the item access to List and change the type hit to Systfem. Object.

//reciever
var dict = x[0] as Dictionary<string,object>;
A = dict["brep"] as Brep;
n = (int) dict["index"];
3 Likes