0
$\begingroup$

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 = {
    'window':win,
    'screen':scr,
    'area'  :areas3d[0],
    'region':region[0],
    'scene' :bpy.context.scene,
}

s.select_set(True)
p.select_set(True)

if bpy.context.mode == 'OBJECT':
    bpy.ops.object.editmode_toggle()

bpy.context.view_layer.objects.active = s

bpy.ops.mesh.knife_project(override)

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)
    else:
        bm = bmesh.new()
        bm.from_mesh(some_object.data)

    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]
        new_mesh.faces.new(nfverts)
    return new_mesh


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

new_mesh=clone_mesh(bpy.context.active_object)

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.

$\endgroup$
4
  • $\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

0

You must log in to answer this question.

Browse other questions tagged .