Skip to content

Instantly share code, notes, and snippets.

@botamochi6277
Created November 13, 2019 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save botamochi6277/44d2b9dd06b6e394d767ca74f9df8abc to your computer and use it in GitHub Desktop.
Save botamochi6277/44d2b9dd06b6e394d767ca74f9df8abc to your computer and use it in GitHub Desktop.
Blender Plot3D
# This script is based on the following discussion.
# https://blenderartists.org/t/how-do-i-create-a-simple-curve-in-python/477260/5
import bpy
import numpy as np
import math
import mathutils
def Lissajous(t, a=1, b=2, delta=0):
x = np.cos(a * t)
y = np.sin(b * t + delta)
return (x, y)
def makeSpline(cu, typ, points):
spline = cu.splines.new(typ)
npoints = len(points)
if typ == 'BEZIER' or typ == 'BSPLINE':
spline.bezier_points.add(npoints - 1)
for (n, pt) in enumerate(points):
bez = spline.bezier_points[n]
(bez.co, bez.handle1, bez.handle1_type,
bez.handle2, bez.handle2_type) = pt
else:
spline.points.add(npoints - 1) # One point already exists?
for (n, pt) in enumerate(points):
spline.points[n].co = pt
return
cu = bpy.data.curves.new("MyCurve", "CURVE")
ob = bpy.data.objects.new("MyCurveObject", cu)
bpy.context.scene.collection.objects.link(ob)
ps = []
for t in np.linspace(0, 2.0 * np.pi, endpoint=True):
x, y = Lissajous(t, a=1, b=2)
ps.append((x, y, np.sin(t), 1))
print(ps)
# scn = bpy.context.scene
# scn.objects.link(ob)
# scn.objects.active = ob
cu.dimensions = "3D"
# makeSpline(cu, "NURBS", [(0, 0, 0, 1), (0, 0, 1, 1),
# (0, 1, 1, 1), (1, 4, 1, 1)])
makeSpline(cu, "NURBS", ps)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment