Translate script from python to c#

Hi all,

i try to translate a working python script to c# and some structures difficult for me to achieve, especially the recursive function structur.

In python

def RecursiveFunction(x):  # x = list of unsorted curves
    do something
RecursiveFunction(x)  # list of sorted curves

Thanks for help

List of sorted curves?
Sorted how?

  • by X coordinate?
  • by Y coordinate?
  • by Z coordinate?
  • by curvature?
  • by Length?
  • by connectivity to one another (like in a polycurve)?

Being new to csharp myself, I think this article is better:

There’s no auto translation available.

Other than that a few notes on recursion (single or nested):

  1. ALWAYS mind the termination: meaning that (until you get fully the gist) in tandem with your exit clause … use a PlanB as well: like some loop counter: this can save you a lot of frustration and a halted PC.
  2. ALWAYS use as much public declarations as possible: avoiding the locomotive effect when calling the recursion Method(s).
  3. Monitor your results in a tree: i.e. sample results in a branch per loop and read the previous branch, that way you can trace back the “history” of the loops (or “animate” them if you like)

I can provide you a ton of C#'s that do recursion (from simple to very complex) … but for the start describe explicitly the task that you are after and a start-up C# could be yours in no time (containing 3 as above).

Thanks for the links.

List of sorted curves?
Sorted how?

Sorted by intersection.
First all LineEndPoint Intersection and second all lines with intersection between LineEndPoints.
The goal is to calculate all weigth points for a booth rig like Dancergraham mentioned in this post Result forces on a grid.

This represents the rig above the booth where all light instalations are fixed.

This is the code to get the curve order (thats what i try to make in c#)

import Rhino as rh

C = [] # Sorted rig

# Order to calculate 
def calculation_order(x):
    if len(x) >= 0:
        for j in x:
            L = []
            for e in x:
                if e != j:
                    CrvEnd = rh.Geometry.CurveEnd.Both
                    ej = rh.Geometry.Curve.Trim(j,CrvEnd,2)
            if any(L) == False:
                return calculation_order(x)

And this is the code to calculate the weight for all endpoints.
Only the endpoints with a higher count than 1 are calculatet.

import Rhino as rh

L = []
D = {}

for i in C:
    SP,EP = i.PointAtStart,i.PointAtEnd
    D[SP] = []
    D[EP] = []

J = []

for i in range(0,len(C)):
    # create Start and Endpoint weights for all rig parts
    SP,EP = C[i].PointAtStart,C[i].PointAtEnd
    weight = (rh.Geometry.Curve.GetLength(C[i])*z)/2
    for j in D:
        if rh.Geometry.Curve.GetLength(C[i]) > rh.Geometry.Curve.ClosestPoint(C[i],j,1)[1] > 0:
            print i
            SP_factor = rh.Geometry.Point3d.DistanceTo(SP,j)/rh.Geometry.Curve.GetLength(C[i])
            EP_factor = 1 - SP_factor
            SP_weight = sum(D.get(j))*EP_factor
            EP_weight = sum(D.get(j))*SP_factor

D1 = {}
SL = (set(L))-(set(J))

for i in SL:
    if i in D:
        D1[i] = sum(D[i])

Dv = D1.values()
Dk = D1.keys()

I try to make this in c# just for learning purpose and to make fast loading compiled tools for grasshopper

This is my first try to do it in c#

a function is written like this:

public/private returntype functionhead(parameters)
      function goes inside

and gets called like this:


Just put the function

//<Custom additional Code>
//</Custom additional Code>

and the calling inside the RunScript function where your code currently is.


Thanks for the example will check that out

Ok here i try in a very simple exampel your advice and it works…but i have difficulties to fill a list with the data from the recursive function.

In python a list can be created outside the recursive function and filled from inside the function like

L = []
x = 0
def test(x):
  if x < 10:
     return test(x+1)

Here is the example with c# how can a list be filled like in the python example


Can you implement the equivalent to this, avoiding problems with global / nonlocal scope?

L = []
x = 0

def test(y, mylist):
  if y < 10:
    mylist = test(y+1,mylist)
  return mylist

L = test(x, L)
print L

Hi Graham,

thanks for help i could figure out what my mistake was with the great video from

So i rewrote my script to the same structure like in the sreenshot and understood better what rgr was written.

public class Script_Instance : GH_ScriptInstance

  private void RunScript(int x, object y, ref object L)
    List <int> D = test(x);
    foreach (int i in D)


  // <Custom additional code> 

  List <int> L = new List <int> ();
  List <int> test(int x)
    if (x > 10)
      return L;
    return test(x + 1);

  // </Custom additional code> 

This example works as i expected very cool.
So thanks for all the response and help.