Why doesn't this code work?

I got this off a tutorial on the web. ( http://jmsoler.free.fr/didacticiel/blender/tutor/python_script03_en.htm )

I get error messages when I run it.

  1. Where in blender is “the console” where error messages would be articulated? I don’t see it.
  2. Any tips on how to get this to run? The site’s code for a single-square mesh works great.
  3. Would python version be the problem??

import Blender
from Blender NMesh import

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()

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()

Try this:


import Blender
from Blender import NMesh

# 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()
# 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()

Indentation is also a consideration.

  1. on windows, there is a ‘dos box’ that opens together with the blender app itself, on linux, if you start blender from the command line (a.k.a. terminal window) that command line is your console. If you start it from an icon it’s a guess where output ends up, just start it from a terminal window.

  2. sorry, hard to say without a proper error message, so try to find that console first.

The console is the window that opens when you open blender, It should be labeled : Shorcut to blender
(not sure why it’s called that?)

That’s actually how I fixed the code. I didn’t even read it. I just put it in Blender, ran the code and made corrections base on error messages in the console. I got the code working, but never even tried to understand it. The console is indispensable as a debugging tool.

Okay, thanks, all.

FWIW, I did have proper indentation in my python script, it just disappeared when I submitted the post here.

And that code fix works on my WinXP box; will try out some more involved experimentation on my linux box. That’s a good tip about how to get the console running in linux: I had always found that extra “DOS” window for Blender to be a nuisance until now!

I’m going to try to learn Python “through art”, by making things in blender.

to make things more complicated:) there is ‘another console’: in the scripts window you can select ‘interactive python console’ from the system submenu. Just follow the hints as this starts up. In some situations this can be helpfull because can type in python and examine Blender data interactively. (the dos box that is used by the scripts is output only)