Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

July 04, 2011

Making a cube using from_pydata

Some primitives, generally geometry is built using triangles (tris) or quadrangles (quads)

Quads

import bpy

verts = [(1.0, 1.0, -1.0),
         (1.0, -1.0, -1.0),
        (-1.0, -1.0, -1.0),
        (-1.0, 1.0, -1.0),
         (1.0, 1.0, 1.0),
         (1.0, -1.0, 1.0),
        (-1.0, -1.0, 1.0),
        (-1.0, 1.0, 1.0)]

faces = [(0, 1, 2, 3),
         (4, 7, 6, 5),
         (0, 4, 5, 1),
         (1, 5, 6, 2),
         (2, 6, 7, 3),
         (4, 0, 3, 7)]

mesh_data = bpy.data.meshes.new("cube_mesh_data")
mesh_data.from_pydata(verts, [], faces)
mesh_data.update() # (calc_edges=True) not needed here

cube_object = bpy.data.objects.new("Cube_Object", mesh_data)

scene = bpy.context.scene  
scene.objects.link(cube_object)  
cube_object.select = True  

Tris

verts = [
(-0.285437,-0.744976,-0.471429),
(-0.285437,-0.744976,-2.471429),
(1.714563,-0.744976,-2.471429),
(1.714563,-0.744976,-0.471429),
(-0.285437,1.255024,-0.471429),
(-0.285437,1.255024,-2.471429),
(1.714563,1.255024,-2.471429),
(1.714563,1.255024,-0.471429)]

faces =  [
(4,5,1),
(5,6,2),
(6,7,3),
(4,0,7),
(0,1,2),
(7,6,5),
(0,4,1),
(1,5,2),
(2,6,3),
(7,0,3),
(3,0,2),
(4,7,5)]

import bpy  
  
mesh_data = bpy.data.meshes.new("cube_mesh_data")  
mesh_data.from_pydata(verts, [], faces)  
mesh_data.update() # (calc_edges=True) not needed here  
  
cube_object = bpy.data.objects.new("Cube_Object", mesh_data)  
  
scene = bpy.context.scene    
scene.objects.link(cube_object)    
cube_object.select = True