4
$\begingroup$

I am a blender noob trying to use bpy.ops.mesh.knife_project() in my script. It fails with the message:

RuntimeError: Operator bpy.ops.mesh.knife_project.poll() expected a view3d region & editmesh

Already tried this method. It fails with the same message

Here is the relevant part of my code. insulator is the active mesh object. knife is the curve object to be used as knife.

knife.select = True
insulator.select = True

override = context.copy()
for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        override['area'] = area
        override['space'] = area.spaces[0]
        for region in area.regions:
            if region.type == 'WINDOW':
                override['region'] = region
                break
        break

bpy.ops.view3d.viewnumpad(override, type = 'BOTTOM', align_active = True)
bpy.ops.view3d.view_persportho(override)

#bpy.ops.object.mode_set(mode = 'EDIT', toggle = False)
#bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.knife_project(override, cut_through = True)         

The view changes to bottom and orthographic view, but the script halts at knife_project.

The blender python reference gives very little information about this function.

Any ideas?

$\endgroup$
  • $\begingroup$ I couldn't get it to fully work, but after selecting, you need to set the active object (bpy.context.scene.objects.active = insulator) and set it to edit mode. However, I could only get it to work when the object to be cut was selected in the viewport before running the script. In general though, it is better to avoid bpy.ops see: blender.stackexchange.com/questions/2848/why-avoid-bpy-ops $\endgroup$ – Ray Mairlot Apr 15 '16 at 22:30
  • $\begingroup$ I would love to avoid bpy.ops in this case, but was unable to find a bmesh alternative to knife_project. Do you know of one? $\endgroup$ – John Apr 15 '16 at 23:53
  • $\begingroup$ Sorry, I'm not too knowledgeable in the bmesh area, all I was able to find was bisect_edges, here: blender.stackexchange.com/questions/39540/cut-bmesh-with-python $\endgroup$ – Ray Mairlot Apr 16 '16 at 0:10
2
$\begingroup$

You walked in right direction, but the code should be wrapped in operator execution method, register and call it from Operator Search using chosen view.

I've made this gist from operator_simple.py template, it works from object or edit mode with active object as target and other as knifes. https://gist.github.com/Begalov/763688551b49004d92ef227ddd7694d6

enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.