Connecting Curves to Random Points Loop

Hey, so I’m very much a beginner when it comes to python scripting, and I’m trying to create a loop for a grid of points where I can created curves that will be randomly generated between these points. I got the grid to work, but I’m currently testing with just lines and I can’t seem to get the lines to connect to these points randomly, here’s the current script:

import rhinoscriptsyntax as rs
import random

xvalues = range(40)
yvalues = range(5,30,1)
points =
for i in xvalues:
for j in yvalues:
point = rs.AddPoint(i,j,0)
points.append(point)

newPointsx =
for i in range(20):
newPointx = random.choice(points)
newPointsx.append(newPointx)
newPointsy =
for j in range(20):
newPointy = random.choice(points)
newPointsy.append(newPointy)

rs.AddLine(newPointsx,newPointsy)

I could be writing this completely wrong, so any help and guidance would be appreciated.

Is this in native Rhino or Grasshopper?

You’re pretty close.
In native Rhino, there is no implied looping so the last line

rs.AddLine(newPointsx,newPointsy)

will not work - you cannot feed it two synchronous lists and expect it to work as in GH, it only takes two points. So, you need to explicitly loop through. Also, rs.AddPoint() adds a point object to the Rhino document, what you want is actually a RhinoCommon point3d object which has the point coordinates but doesn’t create a point object in the Rhino document. For this, instead of using rs.AddPoint(), you can use rs.coerce3dpoint(). So it looks more or less like this:


import rhinoscriptsyntax as rs
import random

xvalues = range(40)
yvalues = range(5,30,1)
points = []
for i in xvalues:
    for j in yvalues:
        point = rs.coerce3dpoint([i,j,0])
        points.append(point)
        
#if you want to add the point objects to the document,uncomment the following
#rs.AddPoints(points)

newPointsx = []
for i in range(20):
    newPointx = random.choice(points)
    newPointsx.append(newPointx)
newPointsy = []
for j in range(20):
    newPointy = random.choice(points)
    newPointsy.append(newPointy)
#loop through the two lists and add lines
for i in range(len(newPointsx)):
    rs.AddLine(newPointsx[i],newPointsy[i])

Lastly to format code here place it between 3 backticks like this:

```python
paste code here
```

You can also qualify the formatting with the language name as I did above after the first 3 backticks.

Thank you very much, I really appreciate your help. Yes this is native Rhino, not grasshopper. I’m trying to expand onto this by having circles that delete the exterior of the lines, with the inside being filled with the lines originally created. I added the circles, but I’m not sure where to move forward and what command to use when it comes to trimming intersections and such

import random

xvalues = range(1,400,5)
yvalues = range(1,200,5)
points = []
for i in xvalues:
    for j in yvalues:
        point = rs.coerce3dpoint([i,j,0])
        points.append(point)
        
newPointsx = []
for i in range(1200):
    newPointx = random.choice(points)
    newPointsx.append(newPointx)
newPointsy = []
for j in range(1200):
    newPointy = random.choice(points)
    newPointsy.append(newPointy)
for i in range(len(newPointsx)):
    line = rs.AddLine(newPointsx[i],newPointsy[i])
newCircles = []
for i in range(600):
    newCircle = random.choice(points)
    newCircles.append(newCircle)
for i in range(len(newCircles)):
    radius = random.randint(1,9)
    circle = rs.AddCircle(newPointsx[i],radius)

This is the code so far

Well, once you’re into splitting/trimming that’s a whole new level. As you know from the native Rhino interface, those operations rely on user screen picks to decide what to keep and what to delete.

To do this script-wise without user intervention, you will need to master some things about intersections and the like, especially how curve parameters work. Both rs.TrimCurve() and rs.SplitCurve() require curve parameters for where to split the input curves and those parameters come from the curve-curve intersections.

If you find the intersection between a line and a circle, it will be none, one or two parameters - depending on how they intersect or not - if they fully intersect, you will have two parameters and you can use rs.TrimCurve() with the two in ascending order to trim away the parts outside the circle… I don’t have time tonight to put up some code though.

Well, I have time to wait, so I’m not in a rush, thanks for the help. How would I be able to find the parameters of each line to circle intersection, as it seems from browsing the forums and internet that it can only be manually picked and not looped through the random lines and circles I generated?