3
$\begingroup$

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')
polyline.points.add(len(coords)-1)
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)
view_layer.active_layer_collection.collection.objects.link(curveOB)

This gives me a curve

enter image description here

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

enter image description here

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

enter image description here

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

$\endgroup$

1 Answer 1

3
$\begingroup$

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')
    polyline.points.add(len(coords)-1)
    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)
view_layer.active_layer_collection.collection.objects.link(curveOB)

Result:

Curves with offset points

$\endgroup$
1
  • $\begingroup$ I feel a bit stupid now. I should've figured that out myself. Thanks a lot though. $\endgroup$ May 12, 2022 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .