#=====================================================
# python_tute_part03.py
# Update : 2026/05/03 for the site redesign in 2026,
# may still work on Blender version 2.49.
#=====================================================
import Blender
from Blender import NMesh # :/ hum, sorry for this.

# Some mathematical functions from the Python module
# would be very useful,   but since Python is very efficient,
# it only loads the minimum that it requires. 
# Thus, we must request that the math module be imported.
import math
from math import *

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

# Without tackling the problem of local and global variables,
# it is better (I prefer) to declare and initialize 
# the variables outside of the subroutines. 
i=0
j=0

# This is an example of what was  explained in the text.
# The value 9 is assigned to the variable totalvert.
totalvert = 9

#  Applying the results of our discussion systematically,
#  the square root of totalvert is obtained with 
#  the function sqrt(). Notice that if the line:
#  " from math import * " had not been used in the script above,
#  it would have been necessary to write " math.sqrt(totalvert) "
n = sqrt(totalvert)

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

# Let's take a break before continuing 
# with automating face creation.
# It's always possible to test this script 
# in its present state, but you must add: 
# NMesh.PutRaw(me, "plane", 1)
# and
# Blender.Redraw()

# [ snip... ] 

# we resume at the exact location in the source 
# where we left off from in the previous part.

# Translate the value of n from a floating point 
# (decimal) value to an integer (whole number). 
# n0=len(range(0, n))
n0=int(n)

for i in range(0, n-1): 
      # The first subroutine begins here, 
      # with four spaces of indentation 
      for j in range(0, n-1): 
          # The second loop begins here, 
          # with eight spaces of indentation 
          f=NMesh.Face() 
          f.v.append(me.verts[i*n0+j ]) 
          f.v.append(me.verts[i*n0+j+1 ]) 
          f.v.append(me.verts[(i+1)*n0+j+1 ]) 
          f.v.append(me.verts[(i+1)*n0+j ])
          # After specifying the vertice co-ordinates for the
          # face, it is added to the faces list of " me ".
          me.faces.append(f)
          # End of the internal loop 
      # End of the external loop 

NMesh.PutRaw(me, " plane", 1) 
Blender.Redraw()
# To test the Python file