[colug-432] python arrays help
Matt Meinwald
meinwald.1 at osu.edu
Mon Dec 22 18:14:38 EST 2014
On 12/22/2014 04:40 PM, Vince Herried wrote:
> for some strange reason I want to create a trefoil knot.
> http://en.wikipedia.org/wiki/Trefoil_knot
>
> In blender environment I want to create vertices and edges.
> I got part of it working to create the vertices but not the edges.
>
> 27 #############
> 28 pi=3.141592654
> 29 Vector = mathutils.Vector
> 30 width = 16
> 31 height = 9
> 32
> 33 verts = []
> 34 edges = []
> 35 faces = []
> 36
> 37 start=0.0
> 38 end=pi/2
> 39
> 40 samples = 35
> 41 sampsize=(end-start)/samples
> 42
> 43
> 44 print("samples=",samples)
> 45 print("start=",start)
> 46 print("end=",end);
> 47
> 48
> 49 # create the vertices and edge loops for cosine
> 50 for t in range (samples+1):
> 51 x = sin(t) + 2*sin(2*t)
> 52 y = cos(t) - 2*cos(2*t)
> 53 z = -sin(3*t)
> 54 print("x=",x,"y=",y,"z=",z)
> 55 v=[Vector(((x * scale_x), (y * scale_y), (z *
> scale_z)))]
> 56 print("length of v=",len(v))
> 57 #verts.append(v)
> 58 verts+=v
> 59 print("length of verts=",len(verts))
> 60 if (t % 2==1):
> 61 edges+=[verts[t-1], verts[t]]
> 62 #edges.append((verts[t-1], verts[t]))
> 63
> 64 print("created the verts and edges")
> 65 print("len of verts=",len(verts),"len of edges=",len(edges))
>
> line 60 - 63 ain't right. I don't know how to add the edges.
>
>
>
> if I run it to create the mesh without the edges all is happy
> if I add in the edges in the mesh add statement blender complains loudly.
> An edge is of course the line between two vertices edge(0) =vert(0)-vert(1)
> or is it edge[0] = vert[0]-vert[1].
>
> Blender add mesh statement looks like:
> 67 mesh = bpy.data.meshes.new(name="Trefoil Knot")
> 68 mesh.from_pydata(verts,[],faces)
>
> or mesh.from_pydata(verts,edges,faces) <<--- complains loudly
> _______________________________________________
> colug-432 mailing list
> colug-432 at colug.net
> http://lists.colug.net/mailman/listinfo/colug-432
>
The += operator iterates through the provided object and
adds each item:
>>> edges=[]
>>> edges+=[1,2]
>>> edges+=[3,4]
>>> edges
[1, 2, 3, 4]
It looks like what you may want is an extra set of [], i.e.
61 edges+=[[verts[t-1], verts[t]]]
>>> edges=[]
>>> edges+=[[1,2]]
>>> edges+=[[3,4]]
>>> edges
[[1, 2], [3, 4]]
What you have on line 62 would work if Blender were able to
work with a list of tuples (as opposed to a list of lists).
If that line doesn't work, it would appear that is not the
case, so you would need to change the () to []:
62 edges.append([verts[t-1], verts[t]])
If these still don't work, it would likely be an issue with
the content rather than the format, and you would need to
look closer at the specific error to determine the cause. A
good way to determine if the code is doing what you want is
to try it out in the Python console first and look at the
contents of the variables, to see if they match what you expect.
More information about the colug-432
mailing list