Pass method name as input in Python

Hello!

Trying to get method names from Python inputs, no luck yet:

import clr
import time
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('DSCoreNodes')
from DSCore import *

from System.Threading.Tasks import *
clr.AddReference("System.Core")
import System
clr.ImportExtensions(System.Linq)

arg = IN[0]

#WORKING OK
test1 = Surface.ByPatch(arg)
test2 = Curve.Patch(arg)

#Trying to get Curve.Patch or Surface.ByPatch from IN[1] string
surf = IN[1]
exec(surf(arg))

Can someone help, please?
Pass_Names.dyn (8.4 KB)

1 Like

@Vladimir ,

you need to print(OUT)

import clr
import time
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('DSCoreNodes')
from DSCore import *

from System.Threading.Tasks import *
clr.AddReference("System.Core")
import System
clr.ImportExtensions(System.Linq)

arg = IN[0]

#WORKING OK
test1 = Surface.ByPatch(arg)

test2 = Curve.Patch(arg)

#Trying to get Curve.Patch or Surface.ByPatch from IN[1] string

OUT = test1,test2

1 Like

Hi @Draxl_Andreas!

I need ability to run Surface.ByPatch or Curve.Patch, taking it directly from IN[1] input strings:
“Surface.ByPatch” or “Curve.Patch”.
I can make some IFs, of course, but maybe there’s some nice trick.

@Vladimir ,

hmmm… in this direction?

arg = IN[0]
operator = IN[1]

OUT = []

#WORKING OK
test1 = Surface.ByPatch(arg)
test2 = Curve.Patch(arg)

if operator == True:
	OUT.append(test1)
else:
	OUT.append(test2)
#Trying to get Curve.Patch or Surface.ByPatch from IN[1] string

No)

I need Python to recognize my string “Curve.Patch” as Curve.Patch from Autodesk.DesignScript.Geometry.
So i want do something like test1 = IN[1](arg) and get the surface.

What’s your reasoning for this and not a simpler If statement?

To keep code simple. What if i have 10 different inputs?

@Vladimir ,

you can use if and elif or double or trible if sentence…

Keep in mind “simple” doesn’t always mean “short”. I’d say an If statement is the simplest solution. You don’t even have to use python if you just use Function Apply and pass on whatever function you need at that time.

1 Like