Generate a grid of vertices and faces

I’ve been trying to learn how to use python in blender and i just need an example of how you would generate a grid X vertices wide and Y vertices long and then fill the faces. I’ve been trying to do it myself but become unstuck when it gets to filling the faces.

thanks

im no expert on python (meaning i know nothing of it) but shouldnt you just use the mesh grid? its alot easier

iliketosayblah: :smiley: this won’t help him in his python training :wink:

hunter551: do you use loops in the script? maybe u could make a face each time 4 or 3 vertices are created…

or index the vertices in a smart way that you will always know which verts make a face

I would recommend to compute the vertices coordinates first, and then create faces afterwards.

For the face creation, Python multiplication (), division (/) and modulo operator (%) can be very handy in retrieving vertices from a list.
Say you have a X
Y grid and you start numbering along the X axis, so that the first vertex in the list is (0,0), the 2nd one (1,0), etc., until you reach X, and then the next vertices are (0,1), (1,1), (2,1), etc.
In that case:

  • grid vertex (x,y) is vertex number (X*y + x) in the list
  • vertex number N in the list belongs to grid vertex(N/X,N%X)

Here’s some code to do what you want. Hope this is helpful.

#!BPY

DIM = 7
points = []
faces = []

import Blender
from Blender import Scene, Mesh
scn = Scene.GetCurrent()
me = Mesh.New()
for x in range(DIM):
    for y in range(DIM):
        points.append([x,y,0])

me.verts.extend(points)

for x in range(DIM-1):
    for y in range(DIM-1):
        faces.append([x*DIM+y,x*DIM+y+1,(x+1)*DIM+y+1,(x+1)*DIM+y])

me.faces.extend(faces)

ob = scn.objects.new(me,"Grid")

Hi Dudecon
Can you please guide me a python script for
A Line of 20 vertex and parallel a line again with 20 vertex
For example Vertex 0…19 in line one and vertex 20… 39 in second line.
How to create a face using 0,1,20,21 vertex
then again 1,2,21,22
looping like that?
http://kkrawal.files.wordpress.com/2007/11/howto.jpg

His script does exactly that :wink:
Just change DIM to 20