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
  • $\begingroup$ That worked. Thank you so much $\endgroup$ Commented May 6, 2022 at 12:36
  • $\begingroup$ Can you help me find the documentation for these things? I can't find these options at all. $\endgroup$ Commented May 6, 2022 at 14:21
  • $\begingroup$ maybe this: docs.blender.org/api/current/bpy.types.Spline.html $\endgroup$ Commented May 6, 2022 at 14:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.