generate cube from matrix

python - 2017-08-30 09:40:47
  create a cube from raw coord
# from http://www.sinestesia.co/blog/tutorials/python-cube-matrices/

import bpy
import math
from mathutils import Matrix

# -----------------------------------------------------------------------------
# Settings
name = ''Cubert''

# -----------------------------------------------------------------------------
# Utility Functions

def vert(x,y,z):
    """ Make a vertex """

    return (x, y, z)


# -----------------------------------------------------------------------------
# Cube Code

verts = [vert(1.0, 1.0, -1.0),
         vert(1.0, -1.0, -1.0),
         vert(-1.0, -1.0, -1.0),
         vert(-1.0, 1.0, -1.0),
         vert(1.0, 1.0, 1.0),
         vert(1.0, -1.0, 1.0),
         vert(-1.0, -1.0, 1.0),
         vert(-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)]


# -----------------------------------------------------------------------------
# Add Object to Scene

mesh = bpy.data.meshes.new(name)
mesh.from_pydata(verts, [], faces)

obj = bpy.data.objects.new(name, mesh)
bpy.context.scene.objects.link(obj)

bpy.context.scene.objects.active = obj
obj.select = True