5
$\begingroup$

I'm just trying to make a closed curve using just python for a script that I'm writing. This is what I have so far:

curveData = bpy.data.curves.new('myCurve', type='CURVE')
curveData.dimensions = '3D'

coords = [(0,0,0), (2,0,0), (2,-1,0), (0,-1,0)]
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, 1)

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

# attach to scene and validate context
view_layer = bpy.context.view_layer
curveOB = bpy.data.objects.new('myCurve', curveData)
view_layer.active_layer_collection.collection.objects.link(curveOB)

enter image description here

Just need a way to connect the first and the last point. Please note the solution should work with bezier curves as well not just polyline curves. Because ultimately I'm trying to make a closed bezier curve, this is just for an example.

$\endgroup$

1 Answer 1

6
$\begingroup$

You could simply add the line polyline.use_cyclic_u = True and use it to create the polygon as cyclic:

import bpy

curveData = bpy.data.curves.new('myCurve', type='CURVE')
curveData.dimensions = '2D'
curveData.fill_mode = 'BOTH'

coords = [(0,0,0), (2,0,0), (2,-1,0), (0,-1,0)]
polyline = curveData.splines.new('POLY')
polyline.points.add(len(coords)-1)
polyline.use_endpoint_u = True
polyline.use_cyclic_u = True

for i, coord in enumerate(coords):
    x,y,z = coord
    polyline.points[i].co = (x, y, z, 1)

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

# attach to scene and validate context
view_layer = bpy.context.view_layer
curveOB = bpy.data.objects.new('myCurve', curveData)
view_layer.active_layer_collection.collection.objects.link(curveOB)
$\endgroup$
3

You must log in to answer this question.

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