I am learning how to create mesh objects using python. The math will be complicated (fsolve and other special functions in Scipy), but I've simplified the problem to just four sides of a box. The first set of code does not run in blender 2.70a installed on my macbook air.
The error message in this case is only "Python script fail, look in the console for now..."
However, if I run the math part in python 2.7.6 first (second code segment below), then paste the output back into blender (third code segment), it works and draws four sides of a box.
This doesn't work:
import bpy
import numpy as np
from math import pi
mesh = bpy.data.meshes.new("Cubit")
object = bpy.data.objects.new("Cubit", mesh)
object.location = (0,0,0) # bpy.context.scene.cursor_location
bpy.context.scene.objects.link(object)
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)
verts = []
faces = []
n_side = 4
n_seg = 1
i_seg = 0
while (i_seg < n_seg+1):
i_vert = 0
zv = 2. * i_seg
while (i_vert < n_side):
xv = np.cos(0.5 * pi * i_vert)
yv = np.sin(0.5 * pi * i_vert)
vert = (xv,yv,zv)
verts.append(vert)
i_vert = i_vert + 1
i_seg = i_seg + 1
i_seg = 0
while (i_seg < n_seg):
i_vert = 0
while (i_vert < n_side):
iv1 = i_seg*n_side + i_vert
iv2 = i_seg*n_side + np.mod(i_vert + 1, n_side)
iv3 = (i_seg + 1)*n_side + np.mod(i_vert + 1, n_side)
iv4 = (i_seg + 1)*n_side + i_vert
face = (iv1,iv2,iv3,iv4)
faces.append(face)
i_vert = i_vert + 1
i_seg = i_seg + 1
mesh = bpy.data.meshes.new("Cubit")
object = bpy.data.objects.new("Cubit", mesh)
object.location = (0,0,0) #bpy.context.scene.cursor_location
bpy.context.scene.objects.link(object)
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)
But if I run this in Python IDLE
import numpy as np
from math import pi
verts = []
faces = []
n_side = 4
n_seg= 1
i_seg = 0
while (i_seg < n_seg+1):
i_vert = 0
zv = 2. * i_seg
while (i_vert < n_side):
xv = np.cos(0.5 * pi * i_vert)
yv = np.sin(0.5 * pi * i_vert)
vert = (xv,yv,zv)
verts.append(vert)
i_vert = i_vert + 1
i_seg = i_seg + 1
i_seg = 0
while (i_seg < n_seg):
i_vert = 0
while (i_vert < n_side):
iv1 = i_seg*n_side + i_vert
iv2 = i_seg*n_side + np.mod(i_vert + 1, n_side)
iv3 = (i_seg + 1)*n_side + np.mod(i_vert + 1, n_side)
iv4 = (i_seg + 1)*n_side + i_vert
face = (iv1,iv2,iv3,iv4)
faces.append(face)
i_vert = i_vert + 1
i_seg = i_seg + 1
print ' verts = ', verts
print ' faces = ', faces
...and then paste the (ugly) output into blender like this, it works fine and draws four sides of a box.
import bpy
import numpy as np
from math import pi
verts = [(1.0, 0.0, 0.0), (6.123233995736766e-17, 1.0, 0.0), (-1.0, 1.2246467991473532e-16, 0.0), (-1.8369701987210297e-16, -1.0, 0.0), (1.0, 0.0, 2.0), (6.123233995736766e-17, 1.0, 2.0), (-1.0, 1.2246467991473532e-16, 2.0), (-1.8369701987210297e-16, -1.0, 2.0)]
faces = [(0, 1, 5, 4), (1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7)]
mesh = bpy.data.meshes.new("Cubit")
object = bpy.data.objects.new("Cubit", mesh)
object.location = (0,0,0) #bpy.context.scene.cursor_location
bpy.context.scene.objects.link(object)
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)