Last active
November 5, 2017 16:59
-
-
Save cbaretzky/df440b37f83c30a300904d92bc2dcb0d to your computer and use it in GitHub Desktop.
quick and dirty voxelizer for blender outputs ascii with ones and zeros #badcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
from mathutils import * | |
from math import * | |
import time | |
import os | |
#written in 2012, first python program, first time using blender API | |
#originally the ray_cast function was used but it was not well documented and highly inaccurate | |
#code checks for consistency of the returned values (watertight mesh) | |
bpy.data.scenes['Scene'].frame_current = 22 | |
objectname = bpy.context.scene.objects.active.name | |
def voxelline (pos0,pos1,objectname,disk): | |
origin=Vector(pos0) | |
destin=Vector(pos1) | |
length=(origin-destin).length | |
ob = bpy.data.objects[objectname].to_mesh(bpy.context.scene,True,'RENDER') | |
direc=Vector(destin-origin) | |
faces = ob.polygons | |
fu=[] | |
layer=0 | |
intersects=[] | |
line = '' | |
for face in faces: | |
hit = 0 | |
vindex=list(face.vertices) | |
one=Vector(ob.vertices[vindex[0]].co) | |
two=Vector(ob.vertices[vindex[1]].co) | |
three=Vector(ob.vertices[vindex[2]].co) | |
if len(vindex)>3: | |
four=Vector(ob.vertices[vindex[3]].co) | |
intersec1 = (geometry.intersect_ray_tri(one,two,four,direc,origin,True)) | |
intersec2 = (geometry.intersect_ray_tri(one,three,four,direc,origin,True)) | |
if intersec1: | |
if intersec1: | |
intersects.append([(intersec1-origin).length,face.normal,face.index]) | |
hit = 1 | |
if intersec2: | |
if bool(intersec2) & (hit==0): | |
intersects.append([(intersec2-origin).length,face.normal,face.index]) | |
hit =1 | |
if (hit==0): | |
intersec = (geometry.intersect_ray_tri(one,two,three,direc,origin,True)) | |
if intersec: | |
intersects.append([(intersec-origin).length,face.normal,face.index]) | |
hit = 1 | |
#if hit==1: | |
#bpy.context.scene.objects.active.data.polygons[face.index].select = True | |
intersects.sort() | |
inout= int(layer>0); | |
temp = 0 | |
realintersects= [] | |
#check if in plane & kick out | |
for intersect in intersects: | |
kill = 0 | |
if temp: | |
if (((intersect[0]-temp[0])<0.00001)& ((temp[1]*intersect[1])>0.99999)): | |
kill = 1 | |
if (kill == 0): | |
if (bool(realintersects)): | |
for oldintersec in realintersects: | |
if (((oldintersec[0]-intersect[0])<0.0000001)& ((intersect[1]*oldintersec[1])>0.9999999)): | |
kill = 1 | |
if kill == 0: | |
temp = intersect | |
#bpy.context.scene.objects.active.data.polygons[intersect[2]].select = True | |
realintersects.append(intersect) | |
#check if close together | |
intersects = realintersects | |
realintersects=[] | |
for intersect in intersects: | |
kill = 0 | |
if temp: | |
temp2=intersect | |
nmax=len(faces[intersect[2]].vertices)+len(faces[temp[2]].vertices) | |
tempv = set(faces[intersect[2]].vertices) | |
tempv= tempv.union(set(faces[temp[2]].vertices)) | |
ndiff=nmax-len(tempv) | |
if ((ndiff>1)&((temp[1]*temp2[1])>0)&(((temp[1]*direc)*(temp2[1]*direc))>0)): | |
kill = 1 | |
if kill == 0: | |
temp = intersect | |
bpy.context.scene.objects.active.data.polygons[intersect[2]].select = True | |
realintersects.append(intersect) | |
intersects=realintersects | |
#print(intersects) | |
#print(len(intersects)) | |
layer=0 | |
vox = 0 | |
for intersect in intersects: | |
currentmax = intersect[0]*disk/length | |
inout=int(layer>0) | |
print(intersect[2]) | |
if layer<0: | |
print('Something went wrong') | |
while -1<vox<round(currentmax)<disk: | |
line+=str(inout)+' ' | |
vox += 1 | |
layer+= (((direc*intersect[1])<0)*2-1) | |
inout=int(layer>0) | |
while -1<vox<disk: | |
line+=str(inout)+' ' | |
vox += 1 | |
return line | |
a =bpy.context.scene.objects.active | |
filepath = 'path/to/file'; | |
#bpy.context.scene.objects.active.active_shape_key.value = .2 | |
disk=16 | |
diskx=disk; | |
disky=disk; | |
diskz=disk; | |
verti = bpy.context.scene.objects.active.data.vertices | |
face = bpy.context.scene.objects.active.data.polygons | |
(xmin , xmax , ymin , ymax , zmin ,zmax )= ( verti[0].co[0],verti[0].co[0],verti[0].co[1],verti[0].co[1],verti[0].co[2],verti[0].co[2]) | |
for vert in verti: | |
xmin = min (xmin, vert.co[0]) | |
ymin = min (ymin, vert.co[1]) | |
zmin = min (zmin, vert.co[2]) | |
xmax = max (xmax, vert.co[0]) | |
ymax = max (ymax, vert.co[1]) | |
zmax = max (zmax, vert.co[2]) | |
dim=((xmax-xmin),(ymax-ymin),(zmax-zmin)) | |
ldim = list(dim) | |
for T in range(0, 1): | |
F = T*25 | |
bpy.context.scene.objects.active.active_shape_key.value = F | |
bpy.context.scene.update() | |
file = filepath+str(F)+'.txt' | |
f = open(file, 'a') | |
for x in range (0, diskx): | |
print(str(x/diskx*100)) | |
for y in range (0, disky): | |
bpy.context.scene.update() | |
pos0 = (((x+.5)/diskx*dim[0]+xmin),((y+.5)/disky*dim[1]+ymin),(zmin)) | |
pos1 = (pos0[0],pos0[1],zmax) | |
bpy.data.scenes['Scene'].frame_current = 22 | |
objectname = bpy.context.scene.objects.active.name | |
a=voxelline (pos0,pos1,objectname,diskz) | |
f.write(a) | |
f.flush() | |
os.fsync(f.fileno()) | |
f.write('\n') | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment