Using bezList2Curve

Hi All,

I found this code in the API.
At this page:
http://www.blender.org/documentation/244PythonDoc/Curve-module.html


def bezList2Curve(bezier_vecs):
 def bezFromVecs(vecs):
  bt= BezTriple.New(vecs[0].x, vecs[0].y, vecs[0].z,                        vecs[1].x, vecs[1].y, vecs[1].z, vecs[2].x, vecs[2].y, vecs[2].z)
  bt.handleTypes= (BezTriple.HandleTypes.FREE, BezTriple.HandleTypes.FREE)
  return bt
 # Create the curve data with one point
 cu= Curve.New()
 cu.appendNurb(bezFromVecs(bezier_vecs[0])) # We must add with a point to start with
 cu_nurb= cu[0] # Get the first curve just added in the CurveData
 i= 1 # skip first vec triple because it was used to init the curve
 while i<len(bezier_vecs):
  bt_vec_triple= bezier_vecs<i>
  bt= bezFromVecs(bt_vec_triple)
  cu_nurb.append(bt)
  i+=1
 # Add the Curve into the scene
 scn= Scene.GetCurrent()
 ob = scn.objects.new(cu)
 return ob


I am wondering how to use this function?
My goal is to generate a curve with three points in it.
Here is my code:


#Build a three point curve.
localCurvePoints = []
localCurvePoints.append(Vector(0,100,-100))
localCurvePoints.append(Vector(0,0,0))
localCurvePoints.append(Vector(0,100,100))
localCurveObj = bezList2Curve(localCurvePoints)

The code errors out in [I]bezList2Curve with “AttributeError: ‘float’ object has no attribute ‘x’”

So how do I populate my localCurvePoints array so it will work with this API function and generate a properly formed curve?

Thanks

Judging from the function itself it needs to be a list of vector triplets, i.e. something like
[[[1,0.6,-5],[1,1,-4],[1,1.4,-3]],[[1,1.6,-5],[1,2,-4],[1,2.4,-3]],[[1,2.6,-5],[1,3,-4],[1,3.4,-3]]] would probably result in some kind of z-axis zig-zag pattern that’s flat in the x dimension.

Try this code:

#Build a three point curve.
localCurvePoints = []
localCurvePoints.append([Vector(1,0.6,-5)),Vector(1,1,-4),Vector(1,1.4,-3)])
localCurvePoints.append([Vector(1,1.6,-5)),Vector(1,2,-4),Vector(1,2.4,-3)])
localCurvePoints.append([Vector(1,2.6,-5)),Vector(1,3,-4),Vector(1,3.4,-3)])
localCurveObj = bezList2Curve(localCurvePoints)

I expect the different parts of the vector triplets correspond to the contents of a BezTriple http://www.blender.org/documentation/244PythonDoc/BezTriple.BezTriple-class.html

So how do I create just a three point curve in Blender using python?

Depends on what kind of three point curve you want. The one I posted is a 3 point curve. I suppose you could write a function that converts a list of regular vectors to a list of [handle,vector,handle] triplets. Here’s one that could work with the list of vectors you started out with. Here’s my attempt:

def vecList2bezList(vecs, smoothness):
	smoothness = smoothness * 0.5
	if len(vecs) == 0:
		return [[[0,0,0],[0,0,0],[0,0,0]]]
	if len(vecs) == 1:
		return [[vecs[0],vecs[0],vecs[0]]]
	if len(vecs) == 2:
		return [[vecs[0],vecs[0],vecs[1]],[vecs[0],vecs[1],vecs[1]]]
	newbezlist = [[vecs[0],vecs[0],vecs[0]+smoothness*(2 * vecs[1] - vecs[0] - vecs[2])]]
	for i in xrange(1,len(vecs) - 1):
		newbezlist.append([vecs[i]-smoothness*(vecs[i+1] - vecs[i-1]),vecs[i],vecs[i]+smoothness*(vecs[i+1] - vecs[i-1])])
	newbezlist.append([vecs[-1]+smoothness*(2*vecs[-2] - vecs[-1] - vecs[-3]),vecs[-1],vecs[-1]])
	return newbezlist

For a smoothness of 0.55 the list of vectors you started out with should result in approximately half a circle.

Thanks Boksha, I’ll give it a try.

It worked fine!