# Create a Disjointed Curve Using a Python Script

I'm trying to create a curve with a python script but I want some of the edges to not be connected. This is what I have so far

import bpy
view_layer = bpy.context.view_layer

coords = [(1,1,0), (2,2,0), (3,0,0)]
curveData = bpy.data.curves.new('myCurve', type='CURVE')
curveData.dimensions = '3D'
curveData.resolution_u = 2
polyline = curveData.splines.new('POLY')
for i, coord in enumerate(coords):
x,y,z = coord
polyline.points[i].co = (x, y, z, i)

curveOB = bpy.data.objects.new('myCurve', curveData)


This gives me a curve

What I'm trying to do is add more points but have some of them not be joined by edges, something like this

I duplicated the curve and joined (Cmd + J) the original and duplicate. That give me just one curve with just one object data.

How can I achieve the same thing in script, I'd like to avoid using bpy.ops.

Just turn the script you have into a function. Then you can reuse it as many times as you want and feed it different coordinates each time. If you use an external variable to keep track of how many new points you've added then you can use that to index into the point array where you left off.

view_layer = bpy.context.view_layer

def create_points(curveData, coords, index=0):
polyline = curveData.splines.new('POLY')
for i, coord in enumerate(coords):
x,y,z = coord
# instead of using i to select the points we use the index
# variable we created outside the function
polyline.points[index].co = (x, y, z, i)
# add to the index, if we start this loop again, then we'll
# be starting at index = 2, 3, 4... then loop again, etc.
index += 1

coords = [[1,1,0], [2,2,0], [3,0,0]]
curveData = bpy.data.curves.new('myCurve', type='CURVE')
curveData.dimensions = '3D'
curveData.resolution_u = 2

# just a loop to offset each segment by 1 on the Z-axis.
for i in range(10):
index = 0
create_points(curveData, coords,index=index)
for c in coords:
c[2] += 1

curveOB = bpy.data.objects.new('myCurve', curveData)