primitive cube mesh with python

Hi there

I’m just starting out with Blender and Python.

How do I create a primitive cube mesh with a Python script?

I found this script to create a new lamp object, but I can’t seem to adapt it to create a primitive mesh

import Blender

object = Blender.Object.New (‘Lamp’)
lamp = Blender.Lamp.New (‘Spot’)
object.link (lamp)
scene = Blender.Scene.getCurrent ()
scene.link (object)

Blender.Redraw()

please help
thansk in advance

import Blender
from Blender import *

meshOb = Blender.Object.New(‘Mesh’)
meshData = Blender.NMesh.New()

vert = Blender.NMesh.Vert()
vert.co[0] = 0
vert.co[1] = 0
vert.co[2] = 0
meshData.verts.append(vert)

vert = Blender.NMesh.Vert()
vert.co[0] = 2
vert.co[1] = 0
vert.co[2] = 0
meshData.verts.append(vert)

vert = Blender.NMesh.Vert()
vert.co[0] = 1
vert.co[1] = 2
vert.co[2] = 0
meshData.verts.append(vert)

meshData.update()

meshOb.link(meshData)

scene = Blender.Scene.GetCurrent()
scene.link(meshOb)

Blender.Window.Redraw()

i got an error on that script.

Really? That’s strange. What’s your error?

I have written a tutorial exactly for cube creation in PythonAPI. Unfortunatelly it’s in Slovak language, but you can find there complete script to create cube in Python.

i just get three verticies in an equilateral triangle, no edges, and it is flat.

 import Blender
from Blender import NMesh

vertices_list=[	
	[0.0,0.0,0.0],
        [0.0,1.0,0.0],
	[1.0,1.0,0.0],
	[1.0,0.0,0.0],
	[0.0,0.0,1.0],
	[0.0,1.0,1.0],
	[1.0,1.0,1.0],
	[1.0,0.0,1.0]
]

faces_list=[[0,1,2,3],
	[4,5,6,7],
	[0,4,7,3],
	[1,2,6,5],
	[0,1,5,4],
	[3,7,6,2]]

A_CUBE_MESH=NMesh.GetRaw()

for coordinate in vertices_list:
    A_VERTEX=NMesh.Vert(coordinate[0], coordinate[1], coordinate[2])
    A_CUBE_MESH.verts.append(A_VERTEX) 

for thisface in faces_list:
    A_FACE=NMesh.Face()
    for vertexpos in thisface:
        A_FACE.append(A_CUBE_MESH.verts[vertexpos])     
    A_CUBE_MESH.faces.append(A_FACE)

NMesh.PutRaw(A_CUBE_MESH,'Cube')
Blender.Redraw()

thank you!

script works perfectly

 import Blender
from Blender import NMesh

vertices_list=[	
	[0.0,0.0,0.0],
        [0.0,1.0,0.0],
	[1.0,1.0,0.0],
	[1.0,0.0,0.0],
	[0.0,0.0,1.0],
	[0.0,1.0,1.0],
	[1.0,1.0,1.0],
	[1.0,0.0,1.0]
]

faces_list=[[0,1,2,3],
	[4,5,6,7],
	[0,4,7,3],
	[1,2,6,5],
	[0,1,5,4],
	[3,7,6,2]]

A_CUBE_MESH=NMesh.GetRaw()

for coordinate in vertices_list:
    A_VERTEX=NMesh.Vert(coordinate[0], coordinate[1], coordinate[2])
    A_CUBE_MESH.verts.append(A_VERTEX) 

for thisface in faces_list:
    A_FACE=NMesh.Face()
    for vertexpos in thisface:
        A_FACE.append(A_CUBE_MESH.verts[vertexpos])     
    A_CUBE_MESH.faces.append(A_FACE)

NMesh.PutRaw(A_CUBE_MESH,'Cube')
Blender.Redraw()

[/quote]