Skip to content

Instantly share code, notes, and snippets.

@CGArtPython
Last active July 26, 2023 03:27
Show Gist options
  • Save CGArtPython/cae2dc180834a68568d9576e0621cc56 to your computer and use it in GitHub Desktop.
Save CGArtPython/cae2dc180834a68568d9576e0621cc56 to your computer and use it in GitHub Desktop.
Beginner Blender Python Tutorial: Python Classes example 3 (used in tutorial: https://youtu.be/t2KEolkhIoA)
import bpy
class Shape:
"""
a class to represent a mesh object
"""
# example of a class variable
instance_count = 0
def __init__(self, name):
self.name = name
self.verts = []
self.faces = []
self.mesh_data = None
self.mesh_object = None
Shape.instance_count += 1
def create_mesh_data(self):
"""
create a mesh from the vert and face data
"""
self.mesh_data = bpy.data.meshes.new(f"{self.name}_data")
self.mesh_data.from_pydata(self.verts, [], self.faces)
def create_mesh_object_from_data(self):
"""
create a object using the mesh data
"""
self.mesh_object = bpy.data.objects.new(f"{self.name}_object", self.mesh_data)
def add_mesh_object_into_scene(self):
"""
add object into active scene by linking the object into the
default scene collection
"""
bpy.context.collection.objects.link(self.mesh_object)
def create_mesh_object(self):
"""
create a mesh object from the given verts and faces
add the new mesh object into the scene
"""
self.create_mesh_data()
self.create_mesh_object_from_data()
self.add_mesh_object_into_scene()
def add_into_scene(self, location):
if self.mesh_object == None:
self.create_mesh_object()
self.mesh_object.location = location
else:
print("Warning: can't add mesh object because it is already added into the scene")
class TriangleFacePyramid(Shape):
"""
a class to represent a pyramid mesh object
"""
def __init__(self):
super().__init__(name="pyramid")
# define the coordinates of each vertex
self.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
self.faces = [
(0, 1, 2),
(2, 3, 0),
(2, 1, 4),
(3, 2, 4),
(0, 3, 4),
(1, 0, 4),
]
class Cube(Shape):
"""
a class to represent a cube mesh object
"""
def __init__(self):
super().__init__(name="cube")
# define the coordinates of each vertex
self.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),
]
class SquareFaceCube(Cube):
"""
a class to represent a cube mesh object with square faces
"""
def __init__(self):
super().__init__()
# define faces using the indexes of the vertices
self.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),
]
class TriangleFaceCube(Cube):
"""
a class to represent a cube mesh object with triangle faces
"""
def __init__(self):
super().__init__()
# define faces using the indexes of the vertices
self.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),
]
def main():
square_face_cube = SquareFaceCube()
square_face_cube.add_into_scene(location=(0, 0, 0))
triangle_face_cube = TriangleFaceCube()
triangle_face_cube.add_into_scene(location=(0, 3, 0))
triangle_face_pyramid = TriangleFacePyramid()
triangle_face_pyramid.add_into_scene(location=(0, 6, 0))
print(f"Shape instance count {Shape.instance_count}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment