r/blenderpython • u/Meta_Riddley • Sep 02 '14
function plotter in blender
Hey, I made two scripts that plot functions. The 2D script plots a function of the form f(x) while the 3D script plots a function of the form (x,y).
2D-Plotter
import bpy
import math
scn = bpy.data.scenes[0]
mesh = bpy.data.meshes.new("me")
ob = bpy.data.objects.new("func",mesh)
start = -1
stop = 1
step = 0.1
#Put your function here:
def function(x):
y=2
return y
k=start
verts=[]
edges=[]
for i in range(0,math.floor((stop-start)/step)+1):
verts.append((k,function(k),0))
if i < math.floor((stop-start)/step):
edges.append((i,i+1))
k=round(k+step,5)
mesh.from_pydata(verts,edges,[])
scn.objects.link(ob)
3D-plotter
import bpy
import math
scn = bpy.data.scenes[0]
mesh = bpy.data.meshes.new("me")
ob = bpy.data.objects.new("func",mesh)
x_a = -1 #Start of x interval
x_b = 1 #End of x interval
y_a = -1 #Start of x interval
y_b = 1 #End of x interval
step = 0.1 #Stepsize
#Put your function in here:
def function(x,y):
z = 2
return z
verts=[]
faces=[]
x = x_a
y = y_a
for j in range(0,math.floor((y_b-y_a)/step)):
for k in range(0,math.floor((x_b-x_a)/step)):
verts.append((x,y,function(x,y)))
x = round(x+step,5)
x = x_a
y = round(y+step,5)
DIM = math.floor((y_b-y_a)/step)
for j in range(0,DIM-1):
for k in range(0,DIM-1):
faces.append((k+j*DIM,(k+1)+j*DIM,(k+1)+(j+1)*DIM,k+(j+1)*DIM))
mesh.from_pydata(verts,[],faces)
scn.objects.link(ob)
Example plots
Tell me what you think of it :) Improvements that can be made?
3
Upvotes
View all comments
Show parent comment
1
u/Meta_Riddley Nov 30 '14
Not sure if it possible to make it a solid curve I would have to think about that a bit.
When it comes to how to use the script in original post, you copy and paste them into the text editor in blender and press run script. You can try the 3D-plotter first remember to set the start and stop intervals the step size and your function. If you run into any problems then just tell me