Interacting with IGH_DocumentObject from Python (Rhino8)

Hi, I’m trying to set the value of a Number Slider using Rhino 8 in Grasshopper. I can do this successfully using a script component running Python2, but Python3 fails running the same code. It appears the underlying C# interface is different between these two scripting environments.

Here is the discrepency, calling this returns lists of two different types:
ghenv.Component.OnPingDocument().Objects

In IronPython2 returns a list of direct usable component types like: GH_NumberSlider
In Python3 returns a list of IGH_DocumentObject interfaces

So in IronPython2 this call works:
obj.Slider.Value

but in Python3 the same call fails with:
‘IGH_DocumentObject’ has no attribute ‘Slider’

Is there some way to cast the object in the Python 3 code so that obj is a reference to a usable: GH_NumberSlider?

See example here:
231107_slider_experiment.gh (13.4 KB)

@Jonathan_Garrison Thanks for reporting this. This is primarily due to the fact that Python 3 needs to wrap dotnet objects and .Objects technically enumerates IGH_DocumentObject. So they get wrapped as that interface type in Pythonnet which does not allow accessing members of the wrapped type. I do have a ticket for this already and will fix:

RH-79621 Pythonnet does not wrap return interface types correctly

1 Like

Thank you!

1 Like