Last active
          October 31, 2022 06:01 
        
      - 
      
- 
        Save CGArtPython/1a53e97727768ba36dbb65b8368b1358 to your computer and use it in GitHub Desktop. 
    Beginner Blender Python Tutorial: Python Classes example 2 (used in tutorial: https://youtu.be/t2KEolkhIoA) 
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import bpy | |
| class Cube: | |
| """ | |
| a class to represent a cube mesh object | |
| """ | |
| def __init__(self): | |
| # 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), | |
| ] | |
| self.faces = [] | |
| self.mesh_data = None | |
| self.mesh_object = None | |
| def create_mesh_data(self): | |
| """ | |
| create a mesh from the vert and face data | |
| """ | |
| self.mesh_data = bpy.data.meshes.new("cube_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("cube_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 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)) | |
| if __name__ == "__main__": | |
| main() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment