Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active July 17, 2017 12:15
Show Gist options
  • Save zeffii/9a38c6ab91431b6b1b8714472a3dadba to your computer and use it in GitHub Desktop.
Save zeffii/9a38c6ab91431b6b1b8714472a3dadba to your computer and use it in GitHub Desktop.
boolean script node
# boolean.py
import numpy as np
import sys
import ast
import bpy
import tempfile
temp_dir = tempfile.gettempdir()
def read_pydata(fullpath):
mesh_dict = np.load(fullpath).item()
return mesh_dict['verts'], mesh_dict['faces']
def write_pydata_from_mesh(mesh, filepath):
fdict = {}
fdict['verts'] = [v.co[:] for v in mesh.vertices]
fdict['faces'] = [[v for v in f.vertices] for f in mesh.polygons]
np.save(filepath, fdict) # will auto add .npy, and make binary file
def object_from_pydata(pydata, name):
mesh = bpy.data.meshes.new(name)
mesh.from_pydata(pydata[0], [], pydata[1])
mesh.update()
obj = bpy.data.objects.new(name, mesh)
scene = bpy.context.scene
scene.objects.link(obj)
return obj
def main():
try:
argv = sys.argv
argv = argv[argv.index("--") + 1:]
print(argv)
boolop, filenameA, filenameB, filenameC = argv
pydataA = read_pydata(filenameA + '.npy')
pydataB = read_pydata(filenameB + '.npy')
obj_a = object_from_pydata(pydataA, 'A')
obj_b = object_from_pydata(pydataB, 'B')
mod = obj_a.modifiers.new(name='sv_bool', type='BOOLEAN')
mod.object = obj_b
mod.operation = boolop
context = bpy.context
scene = context.scene
apply_modifiers = True
settings = 'PREVIEW'
new_mesh = obj_a.to_mesh(scene, apply_modifiers, settings)
write_pydata_from_mesh(new_mesh, filenameC) # automatic + '.npy')
bpy.data.meshes.remove(new_mesh)
except Exception as err:
print('failed', repr(err))
sys.exit(0)
if __name__ == '__main__':
main()
"""
in verts_a v
in faces_a s
in verts_b v
in faces_b s
in operation s d=0 n=2
out verts_out v
out faces_out s
"""
import tempfile
import os
import numpy as np
import subprocess
temp_dir = tempfile.gettempdir()
fullpath_to_boolean_script=r"C:\Users\zeffi\Desktop\GITHUB\blender_boolean.py"
def write_pydata(filepath, mesh):
fdict = {}
fdict['verts'] = mesh[0]
fdict['faces'] = mesh[1]
np.save(filepath, fdict) # will auto add .npy, and make binary file
def read_pydata(filepath):
mesh_dict = np.load(filepath).item()
return mesh_dict['verts'], mesh_dict['faces']
mhash = str(hash(self))
for idx, (va, fa, vb, fb) in enumerate(zip(verts_a, faces_a, verts_b, faces_b)):
filenameA = os.path.join(temp_dir, mhash + "_A")
filenameB = os.path.join(temp_dir, mhash + "_B")
filenameC = os.path.join(temp_dir, mhash + "_C")
try:
write_pydata(filepath=filenameA, mesh=(va, fa))
write_pydata(filepath=filenameB, mesh=(vb, fb))
boolop = ['INTERSECT', 'DIFFERENCE', 'UNION'][operation % 3]
cmd = [
"blender.exe",
"--background",
"-noaudio",
"--factory-startup",
"--python",
fullpath_to_boolean_script,
"--",
boolop,
filenameA,
filenameB,
filenameC
]
# this might depend on the operating system... or taste.
subprocess.run(cmd)
filepath = filenameC + '.npy'
print(filepath)
verts_c, faces_c = read_pydata(filepath=filepath)
verts_out.append(verts_c)
faces_out.append(faces_c)
except Exception as err:
print('failed', repr(err))
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment