Complete Blender
  Script Python: 
modifying a square mesh of polygons 
3rd part: to automate the creation of the Vertices
Version Française
 Main Index                                           Python Index
previous Python: iterations (2) 
Python: automation... of the faces Next

  (Attention starting from here this text could become unpleasing, and perhaps even: tiresome)

We have thus two problems to solve:

  1. we need two overlapping loops for/next one in the other
  2. it is necessary to distribute the list of vertex which has only one dimension in a two-dimensional array so as to locate and recover the nodes of juxtaposed faces. These faces must form a continuous surface.
For the first problem, Python proposes a solution which consists in creating temporarily and within the limits of the subroutine a new list containing all the necessary values, for that it uses the procedure:

      range(departure_value, end_value, incremention_value)

If the starting value is 0, the end value 100 and the progression 1, range(0.0,100.0,1) will build a list whose value which is located at  the index [ 0 ] will be 0, with the index [ 1 ] will be 1 and so on.
If the progression had been two, it would have been necessary to indicate: range(0.0,100.0,2), the value stored at [ 0 ] would have been:   0.0,
                            in 1: 2,
                            in 2: 4,
                            in 3: 6, etc.
 
# example of loop python
for i=0 in range(0,100,1):
    # Here begins the first subroutine
    # four spaces of indentation
   ...

For the second problem it is necessary to reason a little. Let us imagine one moment a square mesh of polygons made up of 4 faces having 4 vertices. We would have thus 9 vertices to distribute. These vertices are arranged, in a list, in the following way [ 0,1,2,3,4,5,6,7,8 ] . With a rapid sketch one sees clearly that there are three lines of three vertices. The number of lines could be arranged in a variable "I"and the number of the vertices in variable "J" .
 

0 1 2
3 4 5
6 7 8

The question we can ask is the following one :
Which value must one allot to variable I and variable J ? (if we have to use both to reach the first vertex which is at  index 0).
One can pose:  (I relation J) = 0, a priori (0 relation 0)  = 0, and that appears very usable as for the next index,  the same line I continues to worth 0, but it is necessary to increase the value of J to arrive at 1. the relation could be I + J, but is this sufficient ? Indeed the following line I will worth1, but to arrive at index 3 the relation i+j will not give what we hope, as J will worth initially 0. In fact it is necessary to multiply I by 3 to obtain the result we are waiting for. The formula which makes it possible to locate something in this mesh is I*3+j .
But could'nt we replace the value 3 by a variable which could be determined automatically without any intervention and according to the total number of vertices of the list ?. Yes. If the mesh of polygons is square, it will always be the square root of the total number of vertices of the list. (One can arrive at results with whole division and the remainder of whole division when the mesh is not square, but it's another story which we will reserve for another teachware.)
 
Blender import
from Blender NMesh import

# Some mathematical functions of the Python module
# can appear useful,   the python, beeing very sparing,
# charges only the minimum it needs when it is requested
importation math
from math import *

# Creation of the mesh of polygons
me=NMesh.GetRaw()

# Without tackling the problem of the local variables
# and global variables it is better (I prefer)   to declare the
# program counters outside the subroutines 
i=0.0
j=0.0

#   This is an example of what we have just explained above
# one arranges value 9 in a variable maxpoint
maxpoint=9

#  Just applies systematically
#  one obtain the square root of maxpoint with 
#  the function sqrt(). Notice that if the line:
#  " from math importation * " had not been used higher in the script
#  it would have been necessary to write " math.sqrt(maxpoint) "
n=sqrt(maxpoint)

for i=0 in range(0, n, 1):
    # Here begins the first subroutine
    # four spaces of indentation
    for j=0 in range(0.0, n, 1):
        # Here begins the second loop
        # eight spaces of indentation
       v=NMesh.Vert(j, I, 0)
        me.verts.append(v)
        # End of the internal loop
    # End of the external loop

# Let us make a pause before passing 
# to the automatic creation of the faces
# it 's always possible to test 
# this script in present state but you must add 
# NMesh.PutRaw(me, " plane",1)
# and
# Blender.Redraw()

previous Python: iterations (2) 
  Python: automation... of the faces Next
 to the Top