Create custom module from Springs custom node Lineloopmerge, error: Global Name Curve not defined

Hi,
In a part of a script that I wrote I want to use SPRINGS custom node Lineloopmerge. This works on a single group of curves. The script Im working on produces several groups of curves per floor level that need to be merged. So Im looking for a way to insert the custom node in the loop. This can be done by copying the code from the custom node into my script. But it should also be possible to create a module from it so it can be used in other scripts.
I rewrote the script a little as a function, saved the script in a separate python script using Visual Studio Code and then imported it as module in the script Ive been writing. This works but Im getting the error Global Name Curve not defined
Does anyone know how to solve this?
Underneath a printscreen of the part of lineloopmerge that I rewrote so it can be imported as a module (the mistake is probably here) and a printscreen of my script with a highlight of of the module is imported (import lineloopmerge as llm)

Thanks, Wouter


I think you can leverage the node as is with adjustments of the list levels and lacing, no?

Hello @wouter.hilhorst
in your case, the import of Autodesk.DesignScript.Geometry NameSpace need to be set in the global namespace no inside a function (local namespace)

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

Yes, that would also work. I`m just playing with the possibilities of external modules. They can be reused in other scripts and make a script more organised.

Solved :upside_down_face:
The custom node lineloopmerge can be rewrtitten to function as an external pyhon module!
Which is interesting in my opinion because it can be embedded in a loop inside a python node (less nodes needed) and cleans up the amount of script yout working with. And you also dont have to import the packages anymore.
Underneath the code. I only added the function “def LineLoop(curves, margin):”.
The rest of the code is copied from the Springs package, node LineLoopMerge.

original = curves[:]

out1 = joinCurves(curves, margin)

return ReorderCurves(original,out1[0])

import clr

clr.AddReference(‘ProtoGeometry’)

from Autodesk.DesignScript.Geometry import *

def OrderCurves(cl0,cl1):

def switch1(i, j, cl0 = cl0, cl1 = cl1):

    cl0[i],cl0[j] = cl0[j],cl0[i]

    cl1[i],cl1[j] = cl1[j],cl1[i]

_ln = len(cl1)

xr1 =  xrange(_ln)

int_test = [[cl1[i].DoesIntersect(cl1[j])\

for j in xr1 if j != i] for i in xr1]

countT = map(sum, int_test)

if 0 not in countT:

    if 1 in countT and countT[0] != 1 :

        switch1(0, countT.index(1) )

    for i in xrange(_ln - 1):

        k = i + 1

        if cl1[i].DoesIntersect(cl1[k]) : pass

        else :

            try:

                int_test = [cl1[i].DoesIntersect(cl1[j])\

                for j in xrange(k, _ln)]

                switch1(k, k + int_test.index(True) )

            except : pass

return cl0, cl1

def ClosedCase(cl0,cl1):

pts = [cl1[i-1].Intersect(cl1[i])[0] for i in xrange(len(cl1) )]

return PolyCurve.ByPoints(pts,True).Curves()

def OpenCase(cl0,cl1):

def FarPt(l1, p1):

    pts = (l1.StartPoint,l1.EndPoint)

    return max(pts, key = p1.DistanceTo)

pts = [cl1[i].Intersect(cl1[i+1])[0] for i in xrange(len(cl1) -1)]

pts.append(FarPt(cl0[-1],pts[-1]))

pts.insert(0,FarPt(cl0[0],pts[0]))

return PolyCurve.ByPoints(pts).Curves()

def joinCurves(cl0,th1):

len1 = len(cl0)

if len1 < 2 : return cl0, False

else:

    cl1 = [c.ExtendStart(th1).ExtendEnd(th1) for c in cl0]

    cl0, cl1 = OrderCurves(cl0,cl1)

    _fn = ClosedCase if cl1[0].DoesIntersect(cl1[-1]) and len1 > 2 else OpenCase

    try : return _fn(cl0,cl1), True

    except : return cl0, False

def ReorderCurves(orig, new):

reordered, new = [], list(new)

app1, pop1, PaP = reordered.append, new.pop, Curve.PointAtParameter

for i in xrange(len(orig) ):

    p1 = PaP(orig[i],0.5)

    p2 = [PaP(new[j],0.5) for j in xrange(len(new) )]

    ind1 = p2.index(min(p2, key = p1.DistanceTo) )

    app1(pop1(ind1) )

return reordered

def LineLoop(curves, margin):

original = curves[:]

out1 = joinCurves(curves, margin)

return ReorderCurves(original,out1[0])

Yes that was indeed the solution. I thought that everything in a python module needs to be embedded in a function but that wasnt the case. The problem was that this import then only existst in that function. But when I just put the import outside the functions at the start of the script it actually works.
Thanks!