I am trying to do the following:
- use a mesh to knife project onto another mesh
- duplicate the faces that have been cut
- 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.
win = bpy.context.windowbpy.ops.mesh.duplicate()
$\endgroup$