I am trying to do the following:

  1. use a mesh to knife project onto another mesh
  2. duplicate the faces that have been cut
  3. extrude the duplicated faces by a given amount

in one operation.

I managed to do the knife project path like this:

import bpy

s = bpy.data.objects['sphere']
p = bpy.data.objects['patch']

win      = bpy.context.window
scr      = win.screen
areas3d  = [area for area in scr.areas if area.type == 'VIEW_3D']
region   = [region for region in areas3d[0].regions if region.type == 'WINDOW']

override = {
    'area'  :areas3d[0],
    'scene' :bpy.context.scene,


if bpy.context.mode == 'OBJECT':

bpy.context.view_layer.objects.active = s


I am then able to duplicate the faces belonging to the knife project like this:

def clone_mesh(some_object):
    if bpy.context.active_object.mode == 'EDIT':
        print('in edit mode')
        bm = bmesh.from_edit_mesh(some_object.data)
        bm = bmesh.new()

    new_mesh = bmesh.new()

    onm = {} # old index to new vert map
    for v in [v for v in bm.verts if v.select]:
        nv = new_mesh.verts.new(v.co)
        onm[v.index] = nv
    for f in [f for f in bm.faces if f.select]:
        nfverts = [onm[v.index] for v in f.verts]
    return new_mesh

def obj_from_mesh(new_mesh_data, name):
    new_data = bpy.data.meshes.new(name)
    obj = bpy.data.objects.new(name, new_data)
    bpy.context.scene.collection.objects.link( obj )
    return obj


obj = obj_from_mesh(new_mesh, 'patch')
obj.matrix_world = bpy.context.active_object.matrix_world.copy()

At this point I would like to extrude the faces of obj in the same way as in Face -> Extrude Faces along Normals, but via python.

Is that possible and how?

I tried to work it out from the docs here and this answer but both are a bit terse.

  • $\begingroup$ Can Edit prior (esp unanswered) questions rather than delete and re-ask. Could you please fix win = bpy.context.windowbpy.ops.mesh.duplicate() $\endgroup$
    – batFINGER
    Sep 2, 2021 at 15:20
  • $\begingroup$ @batFINGER - edit done; re edit rather than re-post, I thought it'd be clearer, will do next time, thanks $\endgroup$
    – simone
    Sep 2, 2021 at 15:24
  • $\begingroup$ Did mean to ask about same re prior question. At a certain rep can see deleted posts. Often cases where someone has deleted and re-pasted their answer after down vote. $\endgroup$
    – batFINGER
    Sep 2, 2021 at 15:30
  • $\begingroup$ @batFINGER - not sure I understand your question $\endgroup$
    – simone
    Sep 2, 2021 at 15:36


You must log in to answer this question.

Browse other questions tagged .