I want to draw script-calculated curves with the grease pencil. Using this line of python I'm able to redefine the points of an existing stroke, shown below.
So far I can't add a point like this:
newpt = points[200].copy() # error
points.append(newpt) # error
or this:
points.items().append('put various things here') # NO error, but no new points either
where:
points = bpy.data.grease_pencil[0].layers[0].active_frame.strokes[0].points
So instead, I draw a big scribble, find out the length using the interactive python window (in this case it's 427) and then make a new set of coordinates of the same length.
How can I create or at least increase the number of points of a grease pencil stroke? I'm looking for something similar to the way I can make new mesh objects:
# example: make a new mesh object - I wish I could make grease pencil objects like this!!!
newmesh = bpy.data.meshes.new('Newmesh')
newobj = bpy.data.objects.new('Newobj', newmesh)
bpy.context.scene.objects.link(newmesh)
newmesh.from_pydata(verts, [], faces)
import numpy as np
import bpy
pi, twopi = np.pi, 2*np.pi
theta = np.linspace(0, 20*twopi, 427)
theta -= theta.mean()
r = 4 - 2*np.cos(0.1*theta)
y = theta / twopi
x = r*np.cos(theta)
z = r*np.sin(theta)
krazy_koil_points = np.vstack((x, y, z)).T
points = bpy.data.grease_pencil[0].layers[0].active_frame.strokes[0].points
for i, point in enumerate(points):
point.co = krazy_koil_points[i]
Same thing, but without numpy
:
import bpy, math
pi, twopi = math.pi, 2*math.pi
theta = [20*twopi * i / 427. for i in range(427)]
mean = sum(theta)/float(len(theta))
theta = [th - mean for th in theta]
r = [4 - 2*math.cos(0.1*th) for th in theta]
y = [th/twopi for th in theta]
x = [a*math.cos(b) for a, b in zip(r, theta)]
z = [a*math.sin(b) for a, b in zip(r, theta)]
krazy_koil_points = list(zip(x, y, z))
points = bpy.data.grease_pencil[0].layers[0].active_frame.strokes[0].points
for i, point in enumerate(points):
point.co = krazy_koil_points[i]