Python script output

Hi,

How can I assign two values to the output tabs A and B of the Python Script Editor in the “Script Instance template”?

"""Grasshopper Script Instance"""
import System
import Rhino
import Grasshopper

import rhinoscriptsyntax as rs
import math

class MyComponent(Grasshopper.Kernel.GH_ScriptInstance):
    def RunScript(self, x: int, y: int):
        # Calculate results
        result1 = x + y
        result2 = x * y
        
        # Assign results to output variables
        A = result1  # First output
        B = result2  # Second output

Return both A, and B from the RunScript method:

class MyComponent(Grasshopper.Kernel.GH_ScriptInstance):
    def RunScript(self, x: int, y: int):
        ...
        return A, B

@eirannejad Oh, I see. Thank you for the reply, but can it be intuitive, like the C# script editor? What if I have a series of return variables? Then, I always have to take care of their order, which will be very frustrating.

“Intuitive” is subjective. RunScript follows idiomatic function calls for each language. A function that takes two arguments and returns two values is defined similar to:

C#

void Foo(int x, int y, out int A, out int B); 

Python

def Foo(x: int, y: int) -> Tuple[int, int]:
    ...
    return (A, B)

Accepting a dictionary {"A": A, "B": B} as the return value would help with the “order” problem but that would break any script that has one output parameter of type dictionary.

The only other way this could work is to inject A and B as variables on the MyComponent class. Then you would have to reference them as self.A and self.B which is even more typing, and it would break the pattern that is already established and been in use for many years in the older scripting component.