How to return two ints from PythonTransform

95 views
Skip to first unread message

oren A

unread,
Feb 15, 2022, 11:36:11 AM2/15/22
to Bonsai Users
Hi, 
I want to return two ints from PythonTransform, something like this (I have much more complicated code, this is just an example of the output that I want):

@returns([int,int])
def process(value):
  if value>1:
    return 1,2
  else:
    return 0,1


I've tried @returns([int,int]), @returns(list), etc.. none of them works.
Thanks

oren A

unread,
Feb 15, 2022, 11:45:48 AM2/15/22
to Bonsai Users
Ok, found a way to do it, (maybe it should be in the PythonTransfrom documentation)

from System import Tuple

@returns(Tuple[int,int])
def process(value):
  if value>1:
    return  Tuple.Create(0,1)
  else:
    return Tuple.Create(5,10)

brunocruz

unread,
Feb 16, 2022, 4:37:06 AM2/16/22
to Bonsai Users
Hi,

Should you want to return an int array (int[]) with 2, or more, elements, you can do:

"
from System import Array

@returns(Array[int])
def process(value):
  Arr = (value, value*2)
  return Array[int](Arr)
"

The elements of the array can then be indexed using the Index operator.

Cheers,
Bruno
Reply all
Reply to author
Forward
0 new messages