Selecting a Face and Extruding a Cube in Blender Via Python API
Question: I am working on a project in which I will need to be able to extrude the faces
of a cube via the python API.

I have managed to extrude a plane via the API:

    
    
    import bpy
    
    bpy.data.objects['Cube'].select = True # Select the default Blender Cube
    bpy.ops.object.delete() # Delete the selected objects (default blender Cube) 
    
    #Define vertices and faces
    verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0)]
    faces = [(0,1,2,3)]
    
    # Define mesh and object variables
    mymesh = bpy.data.meshes.new("Plane")
    myobject = bpy.data.objects.new("Plane", mymesh)  
    
    #Set scene of object
    bpy.context.scene.objects.link(myobject)
    
    #Create mesh
    mymesh.from_pydata(verts,[],faces)
    mymesh.update(calc_edges=True)
    
    bpy.context.scene.objects.active = bpy.context.scene.objects['Plane']
    
    bpy.ops.object.mode_set(mode='OBJECT')
    bpy.ops.object.select_all(action='DESELECT')
    bpy.data.objects['Plane'].select = True # Select the default Blender Cube
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate={"value":(0, 0, 2)})
    

I have built my Cube in a similar way but my issue is I can't work out how to
select a face to extrude via the Python API

Please find my Cube Code <http://pastebin.com/PQtMcRAh>

All Help is Appreciated :)


Answer: I'm not too sure what you need here, but if you need this:

[![](http://i.stack.imgur.com/8jJVM.png)](http://i.stack.imgur.com/8jJVM.png)

Then this is the code you need:

    
    
    import bpy
    import bmesh
    
    bpy.data.objects['Cube'].select = True # Select the default Blender Cube
    bpy.ops.object.mode_set(mode='OBJECT')
    bpy.ops.object.delete() # Delete the selected objects (default blender Cube)
    
    #Define vertices, faces, edges
    verts = [(0,0,0),(0,5,0),(5,5,0),(5,0,0),(0,0,5),(0,5,5),(5,5,5),(5,0,5)]
    faces = [(0,1,2,3), (4,5,6,7), (0,4,5,1), (1,5,6,2), (2,6,7,3), (3,7,4,0)]
    
    #Define mesh and object
    mesh = bpy.data.meshes.new("Cube")
    object = bpy.data.objects.new("Cube", mesh)
    
    #Set location and scene of object
    object.location = bpy.context.scene.cursor_location
    bpy.context.scene.objects.link(object)
    
    #Create mesh
    mesh.from_pydata(verts,[],faces)
    mesh.update(calc_edges=True)
    
    bpy.data.objects['Cube'].select = True
    bpy.context.scene.objects.active = bpy.context.scene.objects['Cube'] # Select the default Blender Cube
    
    #Enter edit mode to extrude
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.normals_make_consistent(inside=False)
    
    bm = bmesh.from_edit_mesh(mesh)
    for face in bm.faces:
        face.select = False
    bm.faces[1].select = True
    
    # Show the updates in the viewport
    bmesh.update_edit_mesh(mesh, True)
    
    bpy.ops.mesh.extrude_faces_move(MESH_OT_extrude_faces_indiv={"mirror":False}, TRANSFORM_OT_shrink_fatten={"value":-5, "use_even_offset":True, "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "release_confirm":False})
    

It expands upon your code. To explain:

After you code, it:

  1. Uses `bmesh` to modify the mesh (`bm = bmesh.from_edit_mesh(mesh)`)
  2. Deselect all faces (`for face in bm.faces: face.select = False`)
  3. Selects the top face (`bm.faces[1].select = True`)
  4. Updates the viewport so you can see it (`bmesh.update_edit_mesh(mesh, True)`)
  5. Extrudes the top face by 5 units (`bpy.ops.mesh.extrude_faces_move(MESH_OT_extrude_faces_indiv={"mirror":False}, TRANSFORM_OT_shrink_fatten={"value": -VALUE, "use_even_offset":True, "mirror":False, "proportional":'DISABLED', "proportional_edit_falloff":'SMOOTH', "proportional_size":1, "snap":False, "snap_target":'CLOSEST', "snap_point":(0, 0, 0), "snap_align":False, "snap_normal":(0, 0, 0), "release_confirm":False})`)

In order to change the number of units extruded, you can modify the `VALUE`
variable.

