GHPython: how to return multiple lists of data

Hello friends!

I am trying to write a script in Python to return a tuple of points, a list of integers and a list of lists with curves.
When I return only one list, everything works fine, but if I try to return more than one list, I get only a text mesages like so:

IronPython.Runtime.List
120

where 120 is a value…

or if returning points then instead of points I get this:

<Rhino.Geometry.Point3d object at …

I want to return the actual data as curves, points, values. But not text messages.

Can somebody guide me on what am I doing wrong?
I guess I missed some logic of GHPython…
It may be impossible to return 3 different data structures in one input. But how, for example, can I return 3 data structures from one function into 3 different outputs?

ghpython_err2.gh (24.1 KB)

Single Responsibility is your real problem. Don‘t expect anyone to understand your code or follow your thoughts if you only have a single function called „aggregate“… Furthermore most error messages are telling you what went wrong. But not when. If you not check your input, then the error might occur later (or never with weird output) . Especially within dynamic languages like Python! If you divide your code into smaller pieces with meaningful names and checks then you can likely find the error for yourself. For any comment you made you can thing about wrapping it into a function with an argument check. Print / Logging helps to find logical errors. A point in Rhino is not a Tuple or Array of doubles, but an object (struct) with X,Y,Z properties.

1 Like

Hi, I think I can help.

First of all, the text messages are text messages only because they are connected to a label component. I’m sure they are points, curves and numbers.

I assume what you are struggling with is that the function returns three “things”. That three “things” are retuned as one tuple. And you don’t know how to handle the tuple. (If you know, please just ignore this post)

In case (hoping) I’m not misunderstanding…

You can unpack three values returned from a function by doing this.

pts,crv,distance=aggregate(_boundary,_restrictions)

If you have pts,crv,distance in the output slots, then they will be output from the compoent separately.

If you still want to output the tuple in one piece, a nice trick is to capsulate the tuple by another tuple.

ret=aggregate(_boundary,_restrictions)
dat=(ret,)

If you have ‘dat’ in the output slot, then Grasshopper detects dat as a list, and tries to unwrap it and outputs the elements conained in the list, which is the tuple itself. If you connect the ‘dat’ slot to another Python compoent, the tuple can be passed over safely in one piece. In the new Python component, you can unpack the dat by saying

pts,crv,distance=dat  #assuming the input slot is named dat
1 Like