Help wanted: Read object attributes in GH

Hello all
I’m trying to read the object attributes through a c# plugin in GH Rhino 8, but I simply cannot get started. I’m passing in model objects as IGH_GeometricGoo, but I do not have access to read anything based on this. I’m guessing I need to cast to GH_ModelObject, but that is not available in the current grasshopper api (it was in the WIP).

Can anybody help me to get started?
Thank you very much in advance!

Do you have an example file?

Thanks Felix
Not really. I thought I could do like below when traversing IGH_GeomtricGoo. Normally I will cast the geometry to whatever I want based on type, so I thought I could cast it to ModeObject, but the cast always fails:

foreach(IGH_GeometricGoo geometry in geometries)
{
    if(geometry.CastTo(out ModelObject mo))
    {
        Debug.WriteLine(mo.name);
        Debug.WriteLine(mo.layer);
    }
    else
    {
          Debug.WriteLine("Cast failed");
    }
}

The strange thing is that in the WIP of Rhino 8 I can also access GH_ModelObject, but not in the latest sdk.

I’m still not quite sure what the exact problem is, but I quickly checked on extracting data from a model object in a plugin, and I was able to get data out of it.

input

pManager.AddGenericParameter("ObjectModel", "ObjectModel", "ObjectModel", GH_ParamAccess.item);

and for solving:

//Get inputs for list => List<GH_ObjectWrapper> 
GH_ObjectWrapper objectModel= new GH_ObjectWrapper();
DA.GetData("ObjectModel", ref objectModel);
  
//Value and ScriptVariable both worked in my case
var model1= objectModel.Value;
var model2= objectModel.ScriptVariable();

//Now i can acces the properties
  

Do you take the input as a tree?

1 Like

Yes, as a tree.
I just have no idea how to read object attributes :slight_smile:
I want to to something like this where I add attributes to a ObjectModel and then on receiving this in my plugin, as an ObjectModel, read the various attributes that has been added. Maybe I’m misunderstanding the entire concept.

Not on my pc anymore but if you want i can giving you an example with trees tomorrow.

But with trees you use I think gh_treestructure<gh_objectwrapper> and use script variable to get the value. But without being on the pc its quite the guess.

But yeah with none gh native things the wrapper worked always pretty good

Thanks Felix! It’s no problem with the tree structure I do that already. It’s just the extraction of object attributes.

EDIT: Doh, I had the input as AddGeometryParameter which was converting to the underlying geometry of the input and therefore I was never getting the ModelObject. Changing to generic and using GH_ObjectWrapper fixed it. I can now see the ModelObject :slight_smile:

Thank you for your help Felix!!

1 Like

is your solution different from the following approach - with code example:

if you code is different - please share a snippet, so other finding this topic have an easier start.
thanks -tom

Thomas -

With Rhino 8 there are the new Grasshopper Datatypes. These are essentially Rhino.objects with a thin Grasshopper Wrapper around them. Here is the class for them:

They can be contain attributes and can be converted to a Rhino.Object if needed.

Of course you have found the Model Object Components to deal with them in Grasshopper.

While the Class structures are documented. If you need more examples, we might be able to look around for something. Are you writing code in a GH C# component or in a Rhino C# script?

In concept I think you have the correct idea. These are GH ready datatypes that should make it easier to get and send data thru Grasshopper:


python_modelobject.gh (11.5 KB)
I apologize for the Python, for me it is a bit easier.

Code to walk into the ModelObject:

"""Grasshopper Script"""
xt=type(x)
xo=x.ObjectType
xl=x.Layer
xln=xl.Name


GHtype=xt
LayerName=xln
ObjectType=xo

1 Like

Thank you very much Scott for the help. I’m in c#, but no probelm :slight_smile:. I can now read the ModelObject correctly. My problem was that I was fetching the object with the geometry parameter input which only revealed the underlying geometry even with the GH_ObjectWrapper. Changing it to generic parameter I can now grap the ModelObject and access the attributes.
I’m very happy with these new changes to this part of GH. It has a lot of potential.
Regards Thomas