Skip to content

Instantly share code, notes, and snippets.

@kellbot
Created November 29, 2010 20:45
Show Gist options
  • Save kellbot/720578 to your computer and use it in GitHub Desktop.
Save kellbot/720578 to your computer and use it in GitHub Desktop.
Script for slicing voxelized objects
#!BPY
"""
Name: 'Slicer 1.0'
Blender: 243
Group: 'Misc'
Tooltip: 'Slice voxelized object'
"""
import Blender, BPyMessages, BPyMesh
import copy
from Blender import *
from Blender.Scene import Render
from Blender.Mathutils import \
Matrix, Vector, ProjectVecs, AngleBetweenVecs, TranslationMatrix
def trimToLayer(selected,layerNumber):
toDelete= []
toAdd = []
source = getChildren(selected)
bbox = source[0].getBoundBox()
blockHeight = bbox[1][2] - bbox[0][2]
offset = selected.getBoundBox()[0][2] - blockHeight/2
mesh = selected.getData(mesh=1)
tmesh = NMesh.GetRawFromObject(selected.name)
tmesh.transform(selected.matrix)
#Delete verticies above the current layer
for v in tmesh.verts:
if v.co[2] > blockHeight*layer + offset:
toDelete.append(v.index)
if layerNumber > 1:
#Delete verticies below the current layer
for v in tmesh.verts:
if v.co[2] < blockHeight*layer-1 + offset:
toDelete.append(v.index)
#hack to filter uniques
toDelete = list(set(toDelete))
omesh = NMesh.GetRawFromObject(selected.name)
for v in omesh.verts:
if v.index in toDelete:
toAdd.append(v.co)
mesh.verts.delete(toDelete)
snapshot(layerNumber)
mesh.verts.extend(toAdd)
Window.Redraw()
def snapshot(layer):
scn = Scene.GetCurrent()
context = scn.getRenderingContext()
context.extensions = True
context.renderPath = "C:/"
context.imageType = Render.JPEG
context.render()
context.saveRenderedImage(str(layer)+".jpg")
def getChildren(obj):
children = []
for ob in Blender.Object.Get():
if ob.getParent() == obj:
children.append(ob)
if len(children) != 0: return children
else: return None
##################3
if __name__ == "__main__":
selection = Object.GetSelected();
layers = Draw.PupIntInput("LayersS",1,0,100)
Blender.SaveUndoState('Kill Everything')
print "\nTrimming First Layer"
for s in selection:
for layer in range(1,layers):
trimToLayer(s,layer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment