I am new to Blender scripting and I am getting some unexpected behavior when trying to add a curve through a Python script. I am using this code:
import bpy
import math
import pdb
from mathutils import Vector
# print all objects
for obj in bpy.data.objects:
print(obj.name)
if("Curve" in obj.name):
print("found")
bpy.data.scenes["Scene"].objects.unlink(obj)
bpy.data.objects.remove(obj)
for cur in bpy.data.curves:
print(cur.name)
bpy.data.curves.remove(cur)
# sample data
coords = [(1,0,1), (2,0,0), (3,0,1)]
# create the Curve Datablock
curveData = bpy.data.curves.new('myCurve', type='CURVE')
curveData.dimensions = '3D'
curveData.resolution_u = 2
# map coords to spline
polyline = curveData.splines.new('POLY')
polyline.points.add(len(coords))
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)
curveData.bevel_depth = 0.01
# attach to scene and validate context
scn = bpy.context.scene
scn.objects.link(curveOB)
scn.objects.active = curveOB
And this is the result I get.
I am confused why there are four coordinates. Blender seems to create one additional point at the coordinate origin (0,0,0), connecting it with the last point from my data. Why is this happening and how can I avoid it?
scn.collection.objects.link(curveOB)
(see this link) andbpy.context.view_layer.objects.active = curveOB
(see this link) $\endgroup$