Skip to content

Instantly share code, notes, and snippets.

@CGArtPython
Last active October 31, 2022 06:00
Show Gist options
  • Save CGArtPython/33dd9b6d791da2f6089bf0c845529ea9 to your computer and use it in GitHub Desktop.
Save CGArtPython/33dd9b6d791da2f6089bf0c845529ea9 to your computer and use it in GitHub Desktop.
Beginner Blender Python Tutorial: Python Classes; step by step version Example 3 (used in tutorial: https://youtu.be/t2KEolkhIoA)
import bpy
#######################################################################
# create a cube with square faces
#######################################################################
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),
]
# define faces using the indexes of the vertices
faces = [
(0, 1, 2, 3),
(7, 6, 5, 4),
(4, 5, 1, 0),
(7, 4, 0, 3),
(6, 7, 3, 2),
(5, 6, 2, 1),
]
edges = []
# create a mesh from the vert, edge, and face data
mesh_data = bpy.data.meshes.new("cube_data")
mesh_data.from_pydata(verts, edges, faces)
# create a object using the mesh data
mesh_obj = bpy.data.objects.new("cube_object", mesh_data)
bpy.context.collection.objects.link(mesh_obj)
#######################################################################
# create a cube with triangle faces
#######################################################################
# define the coordinates of each vertex
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),
]
# define faces using the indexes of the vertices
faces = [
(0, 1, 2),
(0, 2, 3),
(6, 5, 4),
(7, 6, 4),
(4, 5, 1),
(4, 1, 0),
(7, 4, 0),
(7, 0, 3),
(6, 7, 3),
(6, 3, 2),
(5, 6, 2),
(5, 2, 1),
]
edges = []
# create a mesh from the vert, edge, and face data
mesh_data = bpy.data.meshes.new("cube_data")
mesh_data.from_pydata(verts, edges, faces)
# create a object using the mesh data
mesh_obj = bpy.data.objects.new("cube_object", mesh_data)
bpy.context.collection.objects.link(mesh_obj)
mesh_obj.location.y = 3
#######################################################################
# create a pyramid with triangle faces
#######################################################################
# define the coordinates of each vertex
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),
(0.0, 0.0, 1.0),
]
# define faces using the indexes of the vertices
faces = [
(0, 1, 2),
(2, 3, 0),
(2, 1, 4),
(3, 2, 4),
(0, 3, 4),
(1, 0, 4),
]
edges = []
# create a mesh from the vert, edge, and face data
mesh_data = bpy.data.meshes.new("pyramid_data")
mesh_data.from_pydata(verts, edges, faces)
# create a object using the mesh data
mesh_obj = bpy.data.objects.new("pyramid_object", mesh_data)
bpy.context.collection.objects.link(mesh_obj)
mesh_obj.location.y = 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment