Python Script: 
Modifying a Square Mesh ,
Part 2: Iteration 
Version Française
  Main Index                                       Python Index
Previous Python: square mesh (1) 
Python: to automate... vertices (3) Next

The previous tutorial allowed us to create a polygon. However, in 3D modeling  it is extremely rare that one builds an object with only one polygon. Generally, it consists of a complicated unit, a mesh, or a lattice. It is obviously not possible to consider creation of each polygon "by hand". Especially, when it is known that a Blender mesh  can contain more than 65000 polygons. It is for this reason, that the power of the programming language will become apparent, in the possibility of automating this operation. To accomplish this, we need to know how to build an iterative loop in Python.

To create an iterative loop in Python

An iterative loop could correspond to the following pseudo code:
As long as this variable is lower than the end value
       Carry out these operations
  According to : each time through, this variable is increased by an amount and then returns to the line As long as...

With Pascal or the Modula languages, this would produce:
For this_variable end_value do
        carry out_command_1;
        carry out_command_2;

In the C language:
for (this_variable = start_value ;
       this_variable end_value ;
      this_variable ++ )
      run _ command_1 ;
      run _ command_2 ;

FOR this_variable = start_value TO  this_variable end_value
      run _ command_1
      run _ command_2
NEXT this_variable

In these languages, the loops proceed between boundary markers which are clearly defined: limits concerning the number of possible iterations, which as well, limits the number of times that the commands comprising the body of the loop are performed. These limits be brackets or keywords  like " begin/end " or " for/next" , but the result is one and the same.

Alas, Python is optimized, one can even say that it was born to work with lists.  An iterative loop will take a list as limits.  This means that the loop will begin on the first element from the list, then jump to the second element and so on. This operation will be carried out at least once for each element of the list, until the end of the list is reached.

In Python:
for   this_variable in this_list :
      run _ command_1
      run _ command_2 

Moreover, this_variable really is  the actual element of this_list. If this_variable is processed in the body of the loop, this amounts to working directly on the element in question. ie. Besides the element being involved in the loop's control mechanism, it may also be involved in the work being performed in the loop's body.
The limits of the loop's body  are the sign ": " and indentations (indents compared to the margin of the lines) which constitute the body of the loop. The indentations must always be identical and of a comparable nature. One cannot mix : spaces and tabulations. An additional displacement towards the interior of the page indicates to Python that it must take into account a new subroutine. If this subroutine is not declared clearly with the sign ":" in the previous line, it is regarded as a syntax error.

It is not the intention of this tutorial to show the undeniable advantages of proceeding in this way. However, it is clear that it may come as some what of a surprise, especially if you already have some previous programming experience with other languages.

Although, the above is a "step in the right direction" there are a few more obstacles to overcome,  because to work EASILY  with our mesh, we need to reach values which correspond to lines and to the positions in these lines, in fact cells in a bidimensional table ( 2D spaces, x and y coordinates , nothing too difficult to understand).
Previous Python: square mesh (1)
  Python: to automate... vertices (3) Next
 Top of page