I'm trying to figure out how to create a pocket using bmesh (Python Console). The regular workflow to create the example below could be as follows —
Add a Cube in Object Mode, tab into Edit Mode and select an arbitrary Face. Then, use the following steps —
- Extrude the selected Face
- Scale the selected Face
- Extrude the selected Face again
- Translate the selected Face
The question is how to create the same result using bmesh? To get started in the Python Console,
import mathutils
import bmesh
me = bpy.context.edit_object.data
bm = bmesh.from_edit_mesh(me)
Let's just extrude an arbitrary Face and continue from there
f = bm.faces[2]
d = bmesh.ops.extrude_discrete_faces(bm, faces=[f])
f = d['faces'][0]
Now f
refers to the extruded Face. The next step is to scale the extruded Face. To keep things simple, let's assume the normal of the selected Face is parallel to the x-axis. The following code only works as intended if the cube is centered around the origin
bmesh.ops.scale(bm, vec=mathutils.Vector((1,.5,.5)), verts=f.verts)
because bmesh.ops.scale()
seems to use the global (world) coordinates. Is it possibible to use local coordinates instead?
Another option might be to use bmesh.ops.inset_individual()
, though unfortunately it does not create the 4 trapezoid Faces.
The last two steps are straightforward, just another bmesh.ops.extrude_discrete_faces()
followed by bmesh.ops.translate()
.