# RuntimeError: internal error setting the array

I'm trying to write a function to create an object made just out of vertices which is a spiral, kind of like these:

Here's my code:

def spiral(iterations=2,segs=20):
verts = []
faces = []
z=0
position = bpy.context.scene.cursor_location
print(bpy.context.scene.cursor_location)
gap = 360 / segs
for i in range(iterations):
for i in range(0, segs + 1):
deg = i * gap
verts.append((x,y))
for i in range(iterations):
faces.append((i, i+1))
mesh = bpy.data.meshes.new("Spiral")
object = bpy.data.objects.new("Spiral", mesh)

object.location = bpy.context.scene.cursor_location
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)


But for some reason it returns this in the Terminal:

<Vector (0.0000, 0.0000, 0.0000)>
Error: Array length mismatch (expected 126, got 84)
Traceback (most recent call last):
File "/Users/jacobgarby/Documents/Blender Things/Blender      Files/python_meshes.blend/Text", line 48, in <module>
File "/Users/jacobgarby/Documents/Blender Things/Blender   Files/python_meshes.blend/Text", line 45, in spiral
self.vertices.foreach_set("co", vertices_flat)
RuntimeError: internal error setting the array
Error: Python script fail, look in the console for now...


Vertices need an x, y and z coordinate. z was set to zero and not used. Then there was faces in mesh.from_pydata(...) instead of edges, of which only a couple were added as you were iterating through iterations. On top of that you were using i as an iterator in both your inner and outer loop.

To convert between degrees and radians you can use math.degress(rad) or math.radians(deg)

Here is some code I put together a while back to arrange objects into spirals, edited with yours to create a spiral mesh.

import bpy
from math import sin, cos, pi
def spiral(segs=30):
verts = []
edges = []
z=0
n = 3
position = bpy.context.scene.cursor_location
print(bpy.context.scene.cursor_location)
a = 0.5
for i in range(segs):
t = n * 2 * pi * (i)/ segs
# parametric eqn of a helix
x, y, z = (a * sin(t), a  * cos(t),  a * t / n)

verts.append((x,y,z))

for i in range(len(verts) - 1):
edges.append((i, i+1))
mesh = bpy.data.meshes.new("Spiral")
object = bpy.data.objects.new("Spiral", mesh)
print(verts, edges)
object.location = bpy.context.scene.cursor_location