Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created December 10, 2016 14:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/2e75f223434b72abdd387c0eb56bae2f to your computer and use it in GitHub Desktop.
Save zeffii/2e75f223434b72abdd387c0eb56bae2f to your computer and use it in GitHub Desktop.
import bmesh
from sverchok.utils.sv_bmesh_utils import pydata_from_bmesh, bmesh_from_pydata
def average(verts):
vx, vy, vz = 0, 0, 0
for v in verts:
vx += v[0]
vy += v[1]
vz += v[2]
numverts = len(verts)
return vx/numverts, vy/numverts, vz/numverts
def chop(mesh, normal, point):
'''
not an optimal implementation, but simple enough to be educational.s
input:
takes a (what it assumes is) a closed mesh as input
outputs:
a collection of vert, edge, face lists, that hold 2 sublists each
one for each section / bisection.
'''
verts, edges, faces = mesh
verts_list, edges_list, faces_list = [], [], []
def inject_mesh(bm):
v, e, f = pydata_from_bmesh(bm)
verts_list.append(v)
edges_list.append(e)
faces_list.append(f)
def wrev(n):
d = n[0]*2, n[1]*2, n[2]*2
return n[0]-d[0], n[1]-d[1], n[2]-d[2]
for n in [normal, wrev(normal)]:
bm = bmesh_from_pydata(verts, edges, faces)
geom_in = bm.verts[:] + bm.edges[:] + bm.faces[:]
res = bmesh.ops.bisect_plane(
bm, geom=geom_in, dist=0.00001,
plane_co=point, plane_no=n, use_snap_center=False,
clear_outer=True, clear_inner=False
)
# lifted from bisect
surround = [e for e in res['geom_cut'] if isinstance(e, bmesh.types.BMEdge)]
fres = bmesh.ops.edgenet_prepare(bm, edges=surround)
bmesh.ops.edgeloop_fill(bm, edges=fres['edges'])
inject_mesh(bm)
del bm
return verts_list, edges_list, faces_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment