# Create Default Cube (Exactly) from python code.

Hi All,

I am trying to recreate the default cube without using bpy.ops.

My code almost works (included in the BLEND fiel). I can clearly see the vertex location. But I get no solid faces?

This image shows the same exact mesh construction technique working for a plane, but failing for the cube. I assume this is because of some vertex or face order. So I printed out the order of the vertices and faces for the default cube. The results are in the console window and I have matched my construction code to use those exact values.

What do I need to add to my code to make the faces show up solid?

### Attachments

268_make_default_cube.blend (87.4 KB)

Guessing your face list needs to be a list of lists.

why don’t you use BMesh
would be much easier !

i dont’ see any faces;passing here in your function
you define you faces but not pass to the pydata!

me.from_pydata(v, e, f )

i can try to dig up some very old example for this!
but i did not use any old bpy for at least 1 year!
only Bmesh
which i think is way better and more powerfull!

slautations

Ah, thank you both!

@Ricky: I was passing f instead of faces
@Uncle Entity: The face list does need to be a list of lists.

Here is the result that works for me:

``````
def returnCubeMesh(passed_name):
v = []
e = []

# Points that define the default cube.
vertices = [(-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)]
for vertex in vertices:
v.append(Vector(vertex))

# Default cube face order.
faces = [[4,5,1,0],
[5,6,2,1],
[6,7,3,2],
[7,4,0,3],
[0,1,2,3],
[7,6,5,4],
]

me = bpy.data.meshes.new(passed_name)
me.from_pydata(v, e, faces)
me.validate(verbose = True)  # useful for development when the mesh may be invalid.
return me

``````

even more low level:

``````import bpy
from bpy_extras.io_utils import unpack_list, unpack_face_list

coords = (
(-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 = (
(4, 5, 1, 0),
(5, 6, 2, 1),
(6, 7, 3, 2),
(7, 4, 0, 3),
(0, 1, 2, 3),
(7, 6, 5, 4),
)

len_faces = len(faces)

me = bpy.data.meshes.new('Cube')
me.vertices.foreach_set("co", unpack_list(coords))

def get_loop_starts():
arr = []
i = 0
for f in faces:
arr.append(i)
i += len(f)
return arr

def get_loop_totals():
return list(map(lambda f: len(f), faces))

me.loops.add(sum([len(f) for f in faces]))
me.loops.foreach_set("vertex_index", unpack_face_list(faces))

me.polygons.foreach_set("loop_start", get_loop_starts())
me.polygons.foreach_set("loop_total", get_loop_totals())
#me.polygons.foreach_set("vertices", unpack_face_list(faces))

me.update(calc_edges=True)
me.calc_normals()
#me.validate(True)

ob = bpy.data.objects.new(me.name, me)
So that is what is going on behind the scenes with from_pydata. 