Moving Python script into Dynamo environment

Hi,
Due to my lack of Python-knowledge I’m struggling with moving a finalized Python script into Dynamo.
In the original script the variables are defined inside the __init__ method, but after putting it into Dynamo, I these variables will come form Dynamo, so these will be input parameters. So I have to modify this __init__ method but I can’t know how I should do it.

This is the original script part (self.x_all and self.y_all originally come from external CSV files):

class Opticlass:
    def __init__(self) -> None:
        file_path = os.path.dirname(os.path.abspath(__file__))
        self.x_all = np.genfromtxt(file_path+'/s.csv')
        self.y_all = np.genfromtxt(file_path+'/e.csv')
        rs = 10
        re = 10
        self.l_min = 10
        self.angle_change_max_deg = 0.15

This is how I tried to modify it in Dynamo:

x = IN[0]
y = IN[1]
rs = IN[2]
re = IN[3]
l_min = IN[4]
angle_change_max_deg = IN[5]

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

with adoc.LockDocument():
    with adoc.Database as db:

        with db.TransactionManager.StartTransaction() as t:
            
            import numpy as np
            
            class Opticlass:
                def __init__(self) -> None:
                    self.x_all = np.asarray(x)
                    self.y_all = np.asarray(y)
                    self.l_min = l_min
                    self.angle_change_max_deg = angle_change_max_deg
            t.Commit()
            pass

What errors are you getting? That should tell you where the problem lies. You can use variables in a class definition so that’s not the problem. If you’re using the old version of Python you won’t be able to define a return type for the class, which shouldn’t be a big deal.

Hi,
here an example


# Load the Python Standard and DesignScript Libraries
import sys
import clr

# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

# Import references from Civil3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

class Opticlass:
    def __init__(self, x_all : list, y_all : list) -> None:
        self.x_all = x_all
        self.y_all = y_all
        rs = 10
        re = 10
        self.l_min = 10
        self.angle_change_max_deg = 0.15

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor

with adoc.LockDocument():
    with adoc.Database as db:

        with db.TransactionManager.StartTransaction() as t:
            # Place your code below
            # 
            #
            myobject = Opticlass(list(range(3)), list(range(5)))
            # Commit before end transaction
            #t.Commit()
            pass

# Assign your output to the OUT variable.
OUT = myobject.x_all, myobject.y_all
1 Like