2
$\begingroup$

I want to cut some holes in a shape with a python script in Blender (3.3) I have some images here illustrating a simplified version of what I want to do, to remove the cylinder shape from the cube, but without leaving "walls" of geometry where the cylinder cuts through the cube. I just want two holes on either side of the cube.

So starting with this:

enter image description here

and ending something like this:

enter image description here

not this, which is the result of a boolean intersect.

enter image description here

I need to do this all with python scripting, I am experienced with python scripting but new to blender, and honestly am finding the api and workflow very confusing.

$\endgroup$
1
  • $\begingroup$ I don't know python but suspect that you want to use the normal Blender Knife Project equivalent which seems to be here in the Blender python docs. $\endgroup$
    – John Eason
    Nov 25, 2022 at 14:50

1 Answer 1

0
$\begingroup$

I used a circle mesh instead of a cylinder

import bpy

bpy.ops.mesh.primitive_cube_add()
bpy.ops.mesh.primitive_circle_add(radius=0.5, location=(0, 0, 2))

cube = bpy.data.objects["Cube"]
circle = bpy.data.objects["Circle"]
 
ctx = bpy.context
 
old_type = ctx.area.type    
ctx.area.type = 'VIEW_3D'

bpy.ops.view3d.view_axis(type='TOP')

# the view 3d area needs to be updated or else the view won't stay in top view. If you plan on running the script multiple times and all future runs will stay in top view, you can omit this line for future runs.
ctx.area.spaces.active.region_3d.update()

bpy.ops.object.select_all(action='DESELECT')
ctx.view_layer.objects.active = cube
bpy.ops.object.mode_set(mode = "EDIT")
circle.select_set(True)

win      = ctx.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], 'region':region[0]}

bpy.ops.mesh.knife_project(override, cut_through=True)

bpy.ops.mesh.delete(type='FACE')
bpy.ops.object.mode_set(mode = "OBJECT")
ctx.area.type = old_type
$\endgroup$
3
  • $\begingroup$ that doesn't seem to work? I just get a cube and an outline of a circle floating above it? $\endgroup$ Nov 28, 2022 at 9:55
  • $\begingroup$ Also didn't work for me $\endgroup$ Dec 1, 2022 at 4:58
  • $\begingroup$ @HarryMcKenzie I think it should work now after adding the "region_3d.update()" line. Try it out! $\endgroup$ Dec 1, 2022 at 23:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .