Bézier curves code

Here’s the code for manually interpolating between points using Bézier’s equations if anyone’s interested. You can easily use this to generate a curvy mesh with a custom resolution…

The view the second posting for updated code

``````import Blender
from Blender import NMesh
from math import pow

def Bezier3(p1,p2,p3,mu):
#_________________________________________
#Three control point Bezier interpolation
#Parameters: p1 to p3- 3 point tuple [x,y,z]
#mu ranges from 0 to 1 (start to end of curve)
#_________________________________________
p = [0,0,0]

mu2 = mu * mu
mum1 = 1 - mu
mum12 = mum1 * mum1
for z in range(3):
p[z] = p1[z] * mum12 + 2 * p2[z] * mum1 * mu + p3[z] * mu2

return(p);

def Bezier4(p1,p2,p3,p4,mu):
#_________________________________________
#Four control point Bezier interpolation
#Parameters: p1 to p4- 3 point tuple [x,y,z]
#mu ranges from 0 to 1 (start to end of curve)
#_________________________________________
p = [0,0,0]
mum1 = 1 - mu
mum13 = mum1 * mum1 * mum1
mu3 = mu * mu * mu

for z in range(3):
p[z] = mum13*p1[z] + 3*mu*mum1*mum1*p2[z] + 3*mu*mu*mum1*p3[z] + mu3*p4[z]
return(p)

def Bezier(p,mu):
#_________________________________________
#General Bezier curve
#Parameters: p array of points ( etc [[x1,y1,z1],[x2,y2,z2],...] )
#mu ranges from 0 to 1 (start to end of curve)
#_________________________________________
b = [0,0,0]
n = len(p)
muk = 1
munk = pow(1-mu,n)

for k in range(n):
nn = n
kn = k
nkn = n - k
blend = muk * munk
muk *= mu
munk /= (1-mu)
while nn &gt;= 1:
blend *= nn
nn -=1
if kn &gt; 1:
blend /= float(kn)
kn-=1
if nkn &gt; 1:
blend /= float(nkn)
nkn-=1
for z in range(3):
b[z] += p[k][z] * blend
return(b)

me = NMesh.GetRaw()

verts = [[8,-1,-20],[-9,2,8],[10,36,-3],[8,8,8],[10,4,-10],[0,0,0],[10,10,2]]

points = 300

for y in range(points):
#vertex = Bezier3(verts[1],verts[2],verts[3], float(y)/s)
#vertex = Bezier4(verts[1],verts[2],verts[3],verts[4], float(y)/s)
vertex = Bezier(verts,float(y)/s)
vert = NMesh.Vert(vertex[0],vertex[1],vertex[2])
me.verts.append(vert)

NMesh.PutRaw(me)
``````

There was an error in the general Bézier function that made the last point go towards [0,0,0]. The following line needed to be inserted before munk was defined:

``````mu *= (.4*pow(len(p),.15))
``````

So the functions should now read:

``````import Blender
from Blender import NMesh
from math import pow

def Bezier3(p1,p2,p3,mu):
#_________________________________________
#Three control point Bezier interpolation
#Parameters: p1 to p3- 3 point tuple [x,y,z]
#mu ranges from 0 to 1 (start to end of curve)
#_________________________________________
p = [0,0,0]

mu2 = mu * mu
mum1 = 1 - mu
mum12 = mum1 * mum1
for z in range(3):
p[z] = p1[z] * mum12 + 2 * p2[z] * mum1 * mu + p3[z] * mu2

return(p);

def Bezier4(p1,p2,p3,p4,mu):
#_________________________________________
#Four control point Bezier interpolation
#Parameters: p1 to p4- 3 point tuple [x,y,z]
#mu ranges from 0 to 1 (start to end of curve)
#_________________________________________
p = [0,0,0]
mum1 = 1 - mu
mum13 = mum1 * mum1 * mum1
mu3 = mu * mu * mu

for z in range(3):
p[z] = mum13*p1[z] + 3*mu*mum1*mum1*p2[z] + 3*mu*mu*mum1*p3[z] + mu3*p4[z]
return(p)

def Bezier(p,mu):
#_________________________________________
#General Bezier curve
#Parameters: p array of points ( etc [[x1,y1,z1],[x2,y2,z2],...] )
#mu ranges from 0 to 1 (start to end of curve)
#IMPORTANT, the last point is not computed
#_________________________________________
b = [0,0,0]
n = len(p)
muk = 1
mu *= (.4*pow(len(p),.15))
munk = pow(1-mu,n)

for k in range(n):
nn = n
kn = k
nkn = n - k
blend = muk * munk
muk *= mu
munk /= (1-mu)
while nn &gt;= 1:
blend *= nn
nn -=1
if kn &gt; 1:
blend /= float(kn)
kn-=1
if nkn &gt; 1:
blend /= float(nkn)
nkn-=1
for z in range(3):
b[z] += p[k][z] * blend
return(b)

me = NMesh.GetRaw()

verts = [[8,-1,-20],[-9,2,8],[10,36,-3],[8,8,8],[10,4,-10],[0,0,0],[10,10,2]]

points = 30

for y in range(points):
#vertex = Bezier3(verts[1],verts[2],verts[3], float(y)/s)
#vertex = Bezier4(verts[1],verts[2],verts[3],verts[4], float(y)/s)
vertex = Bezier(verts,float(y)/points)
vert = NMesh.Vert(vertex[0],vertex[1],vertex[2])
me.verts.append(vert)

NMesh.PutRaw(me)
``````

Thank you.