r/blenderpython • • 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

sin(x)*cos(y)

sinc(r), r=x2+y2

x2 + y2


Tell me what you think of it :) Improvements that can be made?

3 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/CygnusXmegacorp Nov 30 '14 edited Dec 01 '14

the script works just fine man its a blessing :D, altho coloring it smoothly is another challenge but i can figure that out .

i tried the build in plotters and i had no valid results from it. (could be me tho) any tips on how i could make a mandelbrot zoom? in blender or any other program. the script zoom seems to work well but i dont think i could go infinite loop

cant thank u enough for all the responses :)