Help with Python Error | accessing module

i’m trying to connect a module that provides a straight skeleton algorithm to dynamo.

i’m trying to debug things in VS code using a serialized data from Revit, so i think i have VScode using the embedded python that runs in dynamo.
image

my test code runs in VScode. Dynamo is able to find the module.

i’m trying to bring it over incrementally. when i add in the line that actually calls the function, i get this error:

Warning: RuntimeError : generator raised StopIteration 
['  File "<string>", line 40, in <module>\n', '  File "C:\\Users\\mclough\\src\\polyskel2\\polyskel\\polyskel.py", 
line 437, in skeletonize\n    slav = _SLAV(polygon, holes)\n', 
'  File "C:\\Users\\mclough\\src\\polyskel2\\polyskel\\polyskel.py", 
line 207, in __init__\n    
self._original_edges = [_OriginalEdge(LineSegment2(vertex.prev.point, vertex.point), vertex.prev.bisector, vertex.bisector) for vertex in chain.from_iterable(self._lavs)]\n', 
'  File "C:\\Users\\mclough\\src\\polyskel2\\polyskel\\polyskel.py", 
line 207, in <listcomp>\n    
self._original_edges = [_OriginalEdge(LineSegment2(vertex.prev.point, vertex.point), vertex.prev.bisector, vertex.bisector) for vertex in chain.from_iterable(self._lavs)]\n']

i did debug this error (or a similar one?) in VScode, which seems to be related to changes in python 3.8. Is it maybe possible that Dynamo’s embedded python is somehow looking at an old version of the file? Just seems weird that it runs in VScode, but not in dynamo when both versions of python are the same.

Code from the python node:

# Load the Python Standard and DesignScript Libraries
import sys
import clr
#this is temporary. final path should be the KS 'common lib'
sys.path.append(r'C:\Users\mclough\src\polyskel2\polyskel')

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

#sys.path.append(r'C:\ProgramData\Anaconda3\envs\Dynamo212\Lib\site-packages')

import polyskel
#import skgeom as sg

# The inputs to this node will be stored as a list in the IN variables.
#   input data is serialized geometry of surface polycurves
#   first element is coordinates of the boundary, listed counterclockwise
#   remainder elements describe any holes, listed clockwise.
data = IN[0]

# Place your code below this line
skeletons=[]

for shape in data:
    bdyPoints = []
    boundary = shape.pop(0)
    for b in boundary:
        x,y=b[0]
        bdyPoints.append([float(x),float(y)])

    #remaining entries are the holes
    holes = []
    for curve in shape:
        hlePoints=[]
        for h in curve:
            x,y = h[0]
            hlePoints.append([float(x),float(y)])
        holes.append(hlePoints)
    sk = polyskel.skeletonize(bdyPoints,holes)
    #shapes.append([bdyPoints,holes])
    skeletons.append(sk)
# Assign your output to the OUT variable.
OUT = []

polyskel.py (14.3 KB)
this is the library i’m trying to use: GitHub - yonghah/polyskel: Straight skeleton implementation in Python 3 based on Botffy's polyskel

if i’m reading this error message correctly, is it saying that the RuntimeError occurs in the list comprehension object on line 207?

is that referring to the whole expression between […]? this makes me think it’s saying there’s an issue with the ‘chain.from_iterable’ object, but that’s part of itertools? i don’t understand why that would cause an issue since that should be part of the built-in modules…

Hi,
try to install package with this method

I make a simple test with this implementation

it seems to be working fine

test polyskel

Code Python (simple test )
# https://github.com/Botffy/polyskel
import sys
import clr
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
#
clr.AddReference('GeometryColor')
from Modifiers import GeometryColor
clr.AddReference('DSCoreNodes') 
import DSCore
from DSCore import Color as DSColor
#
clr.AddReference('Python.Included')
import Python.Included as pyInc
path_py3_lib = pyInc.Installer.EmbeddedPythonHome
sys.path.append(path_py3_lib + r'\Lib\site-packages')
# add polyskel.py as module 
sys.path.append(IN[0])
import polyskel
import euclid3 # pip install euclid3
import PIL # pip install Pillow 

def to_Clockwise(polyc: PolyCurve) -> PolyCurve:
    sumCrossZ = 0
    surface = Surface.ByPatch(polyc)
    center = surface.PointAtParameter(0.5, 0.5)
    for c in polyc.Curves():
        pStart = c.StartPoint
        pEnd = c.EndPoint
        vecta = Vector.ByTwoPoints(center, pStart)
        vectb = Vector.ByTwoPoints(center, pEnd)
        cp = vecta.Cross(vectb)
        sumCrossZ += cp.Z
    if sumCrossZ > 0:
        return polyc.Reverse()
    else:
        return polyc


polyCurve = to_Clockwise(IN[1])
ds_color = DSColor.ByARGB(255, 255, 100, 0)

b= []
for curve in polyCurve.Curves():
    b.append((curve.StartPoint.X, curve.StartPoint.Y))
    
##############################################
skeleton=polyskel.skeletonize(b,holes=[])
##############################################
lines = []
for arc in skeleton:
    for sink in arc.sinks:
        pta = Point.ByCoordinates(arc.source.x, arc.source.y, 0)
        ptb = Point.ByCoordinates(sink.x, sink.y, 0)
        #print((arc.source.x, arc.source.y, sink.x, sink.y))
        if pta.DistanceTo(ptb) > 0.1:
            line = Line.ByStartPointEndPoint(pta, ptb)
            lines.append(GeometryColor.ByGeometryColor(line, ds_color))

OUT = lines
1 Like

thanks so much for the help.

I’m pretty sure i used the procedure in the first link to install the packages, but i may have messed it up.
I’ll revisit this. It seemed when i had tried the bottfy package initially i got some incorrect outputs.

I’m trying to get better at reading, understanding, and implementing python code. When i was reviewing both modules, they use the same code at the point i’m getting an error, just different geometric primitives.

did you get a similar issue, or did it just seem to work?

my main confusion is trying to figure out how to create a ‘clone’ of the resources dynamo’s embedded python sees externally so that i can have access to better debugging options. I was able to get this and some other straight skeleton algorithms working in VScode, but when i would port them over to Dynamo, i would get various errors.

what is the reference ‘Python.Included’ doing? I did some preliminary search and i’m not finding a good explanation. Is this part of ironpython? (or pythonnet? it seems like these extensions do the same thing?)

no, just I need to install ‘euclid3’ and 'Pillow ’ packages

It’s a framework (include in Dynamo) enabling import of .NET libraries for Python.Net