A Python loop in Dynamo seems to fail randomly for some iteration numbers?

Hi all,
I am moving TS vertices of a TS body several times, to project the TS body onto a Nurbs surface. The user can control the amount of iterations. The Python code is super simple (and probably bad, apologies for that, I am a beginner ) :laughing:.
This works fine in general, but after some iterations, the code produces an “array index out of range” error, where I got no clue where it is coming from.
This bug seems to occur randomly, if I am not moving all CVs, sometimes the issue occurs earlier, sometimes not at all. For me, this seems to indicate that the the problem is either in Python, or the TS funcion .MoveVertices it is calling, and not in my (poor) code. Could someone please have look?


I have created a zip directory, which I am not allowed to upload, and I am also not allowed to upload any files, because I am “new”. Would appreciate it, if someone would make the effort of sending me a PN, so I can send the data.
Frustrated regards,
GG

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
from Autodesk.DesignScript.Geometry.TSpline import *
#The inputs to this node will be stored as a list in the IN variables.
tssurf = IN[0]
verts = IN[1]
reference = IN[2]
iterations = IN[3]

vector_start = []
vector_end = []
max_distance = 0.0

temp_tssurf = tssurf

verts_indices = []
verts_uvn = []

for index, temp_vertex in enumerate(verts):
    verts_indices.append(verts[index].Index)
    verts_uvn.append(verts[index].UVNFrame)


for x in range(0, iterations):

    for index, temp_vertex in enumerate(verts):
        temp_vertex = temp_tssurf.VertexByIndex(verts_indices[index])
        temp_normal = verts_uvn[index].Normal
        vector_start = temp_vertex.PointGeometry
        vector_end = []
        vector_end = Point.Project(vector_start, reference, temp_normal)
        move_vector = Vector.ByTwoPoints(vector_start, vector_end[0])
        
        temp_tssurf = temp_tssurf.MoveVertices([ verts[index] ], move_vector, False)

OUT = temp_tssurf

Hi,

I’ve upgraded your level so you can upload files now

1 Like

Thanks a lot! And just to add: I did some checks before I posted here. I tried to check for null or empty lists, but this didn’t seem to be the case:


nurbs_surface.sat (4.9 KB)
projection_test.dyn (18.2 KB)
ts_surface.sat (1.7 KB)
The ts_surface.sat needs to be renamed to ts_surface.tsm.
@c.poupin could you initiate that .tsm and .tss become legal files to upload here? There are TSpline file formats which Dynamo can read and write.
Thanks,
GG

I’m looking into this, but we have to be cautious. Winds up that extension blocking is sort of a must from a safety standpoint - you’d be surprised at how many spam/malicious links we block every day…

In the meantime you can change the extension to somefile.tsm.txt.

2 Likes

Added .tss and .tsm to the list of approved extensions. This may be rolled back if someone smarter than I comes up with a good reason not to allow them.

1 Like

There’s a lot of unnecessary code. Refactored to eliminate indexing errors

import clr

clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript.Geometry import Point, Vector

tssurf = IN[0]
verts = IN[1]
reference = IN[2]
iterations = IN[3]

new_tssurf = tssurf

for _ in range(iterations):
    for vert in verts:
        normal = vert.UVNFrame.Normal
        vector_start = vert.PointGeometry
        vector_end = Point.Project(vector_start, reference, normal)[0]
        move_vector = Vector.ByTwoPoints(vector_start, vector_end)

        new_tssurf = new_tssurf.MoveVertices([vert], move_vector, False)

OUT = new_tssurf

2 Likes

@Mike.Buttery first of all thanks for looking into this! I know this is a lot to ask, especially with me not writing good code (yet). ;-(

Your code did not do what mine does, but it helped me to find the error: once the TS surface ends up on the reference, the projection attempt becomes null or an empty list. I tried to check for that, but obviously did not do so correctly. Today I did, and now it works fine.

Thanks so much for your quick help here, truly appreciate it! :slight_smile: