Last active
          October 31, 2022 06:00 
        
      - 
      
- 
        Save CGArtPython/6c378dad7b58ae7b6ed55949239f11c4 to your computer and use it in GitHub Desktop. 
    Beginner Blender Python Tutorial: Python Classes; step by step version 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 | |
| ####################################################################### | |
| # create a cube with square 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, 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 | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment