Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/fun_cube_bug.py
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeffii/11395698 to your computer and use it in GitHub Desktop.
Save zeffii/11395698 to your computer and use it in GitHub Desktop.
def sv_main(divx=2, divy=2, divz=2, size=1.0):
in_sockets = [
['s', 'divx', divx],
['s', 'divy', divy],
['s', 'divz', divz],
['s', 'size', size]
]
def gen_cube(divx, divy, divz, size):
if 0 in (divx, divy, divz):
return [], []
b = size / 2.0
verts = [
[b, b, -b], [b, -b, -b], [-b, -b, -b],
[-b, b, -b], [b, b, b], [b, -b, b],
[-b, -b, b], [-b, b, b]
]
faces = [[0, 1, 2, 3], [4, 7, 6, 5],
[0, 4, 5, 1], [1, 5, 6, 2],
[2, 6, 7, 3], [4, 0, 3, 7]]
if (divx, divy, divz) == (1, 1, 1):
return verts, faces
# ok, looks like we have some work to do!
import bmesh
import mathutils
from mathutils import Vector
bm = bmesh.new()
[bm.verts.new(co) for co in verts]
bm.normal_update()
for face in faces:
bm.faces.new(tuple(bm.verts[i] for i in face))
bm.normal_update()
dist = 0.0001
section_dict = {0: divx, 1: divy, 2: divz}
for axis in range(3):
num_sections = section_dict[axis]
if num_sections == 1:
continue
step = 1 / num_sections
v1 = Vector(tuple((b if (i == axis) else 0) for i in [0, 1, 2]))
v2 = Vector(tuple((-b if (i == axis) else 0) for i in [0, 1, 2]))
for section in range(num_sections):
mid_vec = v1.lerp(v2, section * step)
plane_no = v2 - mid_vec
plane_co = mid_vec
visible_geom = bm.faces[:] + bm.verts[:] + bm.edges[:]
bmesh.ops.bisect_plane(
bm, geom=visible_geom, dist=dist,
plane_co=plane_co, plane_no=plane_no,
use_snap_center=False,
clear_outer=False, clear_inner=False)
indices = lambda face: [v.index for v in face.verts]
vert = [v.co.to_tuple() for v in bm.verts]
poly = [indices(face) for face in bm.faces]
return vert, poly
vert, poly = gen_cube(divx, divy, divz, size)
out_sockets = [
['v', 'vert', vert],
['s', 'poly', poly]
]
return in_sockets, out_sockets
'''
_, outs = sv_main(1, 3, 2, 4)
verts = outs[0][2]
faces = outs[1][2]
print("verts: ", verts)
print("polygons: ", faces)
import bpy
mesh_data = bpy.data.meshes.new("cube_mesh_data")
mesh_data.from_pydata(verts, [], faces)
mesh_data.update() # (calc_edges=True) not needed here
cube_object = bpy.data.objects.new("Cube_Object", mesh_data)
scene = bpy.context.scene
scene.objects.link(cube_object)
cube_object.select = True
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment