1
$\begingroup$

I managed to use the bisect operator from a python script like this:

bpy.ops.mesh.bisect(plane_co=(curx, cury, curz+dz),
                    plane_no=(0, 0, 1),
                    use_fill=False,
                    clear_inner=False,
                    clear_outer=False,
                    threshold=0.0001,
                    xstart=0,
                    xend=0,
                    ystart=0,
                    yend=0,
                    cursor=1002)

It works but nothing works after such in the script. To make it work I need to be in edit mode and have the mesh already selected. (if someone can also explain how to just change to edit mode and do a select all, or even how to select the part I want even better) it's almost as if this command is clearing the selection so it does not work anymore. Do i need to wait for some process or update something before using the same command (with other parameters of course)?

$\endgroup$
3
  • $\begingroup$ Hi stelarfox, providing a sample (and usable) script to run on default cube would help to answer this q. $\endgroup$
    – batFINGER
    Dec 15 '16 at 4:48
  • $\begingroup$ @batFINGER , i did that (thats why i manege to make it work) my problem is only, that i want to do more than 1 cut in the same script, thats the part where all fails. $\endgroup$
    – stelarfox
    Dec 15 '16 at 18:37
  • $\begingroup$ What I'm saying is if I can post the code in the question straight into the text editor, run it and it shows the issue, it's a lot easier. The code above is just the operator call, with some undefined variables. $\endgroup$
    – batFINGER
    Dec 16 '16 at 2:54
2
$\begingroup$

Using bmesh

rather than drive yourself nuts with trying to use what is basically a UI operator (IMO), can use the bmesh equivalent bmesh.ops.bisect_plane.

Here is a test script, to run while object is in edit mode. It bisects all faces / edges of edit object with a plane at (0, 0, 0) normal z axis, in the meshes local space.

import bpy
import bmesh
context = bpy.context

# run this in edit mode
ob = context.edit_object
mesh = ob.data
bm = bmesh.from_edit_mesh(mesh)

# select all faces
for f in bm.faces:
    f.select = True

edges = [e for e in bm.edges]
faces = [f for f in bm.faces]
geom = []
geom.extend(edges)
geom.extend(faces)

result = bmesh.ops.bisect_plane(bm,
                              dist=0.01,
                              geom=geom,
                              plane_co=(0, 0, 0),
                              plane_no=(0, 0, 1))
print(result)
bmesh.update_edit_mesh(mesh)

Or alternatively in object mode

import bpy
import bmesh
context = bpy.context
# run this in edit mode
ob = context.object
mesh = ob.data
bm = bmesh.new()
bm.from_mesh(mesh)

# select all faces
for f in bm.faces:
    f.select = True

edges = [e for e in bm.edges]
faces = [f for f in bm.faces]
geom = []
geom.extend(edges)
geom.extend(faces)

result = bmesh.ops.bisect_plane(bm,
                              dist=0.01,
                              geom=geom,
                              plane_co=(0, 0, 0),
                              plane_no=(0, 0, 1))
print(result)
bm.to_mesh(mesh)
mesh.update()

The plane coord and normal passed to the operator are in local space of the mesh. To use global coordinates

local_coord = ob.matrix_world.inverted() * global_coord

Can change the geom input into the operator

from math import radians
edges = [e for e in bm.edges if e.select] # use selected edges
# faces with a normal orthogonal to z axis
faces = [f for f in bm.faces
               if abs(f.normal.angle((0, 0, 1)) - radians(90)) < 0.001]
$\endgroup$
4
  • $\begingroup$ not sure whats wrong but doing that and simple its breaking my model, it seems the extend its causing that may be but, doesn ot work without it. $\endgroup$
    – stelarfox
    Dec 15 '16 at 18:38
  • $\begingroup$ do i need also the coordinates change if my object is at 0,0,0 rotated in 0,0,0 and scaled as 1,1,1? should not work the same in that condition? because i truly tried and just makes a mess not sure whats wrong with it. or it does work in view coordinate even so? $\endgroup$
    – stelarfox
    Dec 15 '16 at 18:45
  • $\begingroup$ ok it only failed in object mode, the option in edit mode worked thanks $\endgroup$
    – stelarfox
    Dec 15 '16 at 18:50
  • $\begingroup$ AFAIC both examples, in both modes, succesfully bisect the default cube at the local xy (z=0) plane. All geom.extend is doing, is extending the geom list with all edges and then all faces as input for the operator. As I pointed out the coordinates the operator uses are local coordinates. Moving, scaling, rotating an object doesn't change the local coordinates of the verts. That is what apply (location, rotation, scale) does. Test this by adding default cube, select it, move it, rotate it, scale it, run script, and it will still add the bisect edge in same place. $\endgroup$
    – batFINGER
    Dec 16 '16 at 3:08

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.