OK, many things to cover here…
First, I’m going to take a different route to make your triangles. Instead trying to use MeshToNurb etc. I’m simply going to get the corner points and I’m going to create two new triangular surfaces with them, then delete the original surface. All you need to do then is figure out which direction to create the triangles according to the shortest diagonal. There are lots of levels of sophistication that you can reach here, but I’m going to do it simply with all rhinoscriptsyntax tools.
Since you know some Grasshopper, you may be familiar with some or all of the following. In Grasshopper you would do the same thing just by connecting components with wires, but it actually makes code similar to what follows in the background.
So the first thing you need to know here is the use of variables. Variables are containers in which you can store things, they can be objects, numbers, True/False values, etc. This is the great advantage of scripts over macros, you can store references to things for future re-use. Most rhinoscriptsyntax methods return something and the result can be stored in a variable. For example the line
bord=rs.DuplicateSurfaceBorder(srf)
adds the border curve to the document and stores the ID (reference to the new object) in the variable “bord”.
Now the return of a function can also be several objects which is stored as a list:
pts=rs.CurvePoints(bord)
will return a list of corner points from the border curve. Lists in Python look like this: [...]
Counting starts at 0 (as in Grasshopper) so they will be stored in positions 0 through 4 (first/last points are repeated in closed curves). One can address individual elements of a list with the syntax my_list[i]
where i
is the index number you want to get at. So you will see references below such as pts[0]
, pts[1]
, etc.
The second thing to learn is looping. A loop is a repetitive series of operations, for example to apply a particular procedure to a whole group of objects, one at a time. In this case we’re using a “for” loop to work on each surface individually.
The last thing is conditionals - decision making based on some sort of data. In this case we’re checking the distance between two sets of corner points and creating the triangles along the short diagonal. This is done with the if-else statement.
The whole thing then looks like this:
import rhinoscriptsyntax as rs
def SurfaceSplitter():
#limit your choice to surfaces, allow preselection
SelectedSurfaces = rs.GetObjects("Select objects",8,preselect=True)
if (SelectedSurfaces==None): return
#rs.UnselectObjects(SelectedSurfaces) #you don't need this
#we're going to temporarily suspend redrawing of the screen (goes faster)
rs.EnableRedraw(False)
#we need to create a loop in order to work on each surface individually
for srf in SelectedSurfaces:
#get the border
bord=rs.DuplicateSurfaceBorder(srf)
#get the corner points (there will be 5, first/last are repeats!)
pts=rs.CurvePoints(bord)
#check diagonals, find shortest, create triangular surfaces
if rs.Distance(pts[0],pts[2])<rs.Distance(pts[1],pts[3]):
tri1=rs.AddSrfPt([pts[0],pts[1],pts[2]])
tri2=rs.AddSrfPt([pts[0],pts[2],pts[3]])
else:
tri1=rs.AddSrfPt([pts[0],pts[1],pts[3]])
tri2=rs.AddSrfPt([pts[1],pts[2],pts[3]])
#delete unneeded objects
rs.DeleteObject(srf)
rs.DeleteObject(bord)
#now loop on to the next surface, until all have been done
SurfaceSplitter()
You can go a lot further, for example checking and excluding surfaces that have more than 4 points or non-linear edges, joining the triangles to form a polysurface, etc… But this is good for a start…
Hope this small example helps… --Mitch