5
$\begingroup$

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

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)
    deg2rad = math.pi / 180
    gap = 360 / segs
    for i in range(iterations):
        for i in range(0, segs + 1):
            deg = i * gap
            rad = deg * deg2rad
            x, y = math.cos(rad) * 5, math.sin(rad) * 5
            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
    bpy.context.scene.objects.link(object)
    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
  File "/Users/jacobgarby/Downloads/blender-  2/2.76.app/Contents/MacOS/../Resources/2.76/scripts/modules/bpy_types.py",    line 414, in from_pydata
    self.vertices.foreach_set("co", vertices_flat)
RuntimeError: internal error setting the array
Error: Python script fail, look in the console for now...
$\endgroup$

1 Answer 1

5
$\begingroup$

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.

Also look into the addon add_curve_extra_objects and in particular add_curve_spirals.py

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
    bpy.context.scene.objects.link(object)
    mesh.from_pydata(verts,edges, [])
    mesh.update(calc_edges=True)

spiral()
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .