Skip to content

Commit f8dac5f

Browse files
committed
Merge pull request #37 from bollwyvl/repr-png
adding _repr_png_ for ipython
2 parents be88a4a + 39f814e commit f8dac5f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

solid/solidpython.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import os, sys, re
1313
import inspect
14+
import subprocess
15+
import tempfile
1416

1517
openscad_builtins = [
1618
# 2D primitives
@@ -602,6 +604,34 @@ def __mul__(self, x):
602604
'''
603605
return intersection()(self, x)
604606

607+
def _repr_png_(self):
608+
'''
609+
Allow rich clients such as the IPython Notebook, to display the current
610+
OpenSCAD rendering of this object.
611+
'''
612+
png_data = None
613+
tmp = tempfile.NamedTemporaryFile(suffix=".scad", delete=False)
614+
tmp_png = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
615+
try:
616+
scad_text = scad_render(self).encode("utf-8")
617+
tmp.write(scad_text)
618+
tmp.close()
619+
tmp_png.close()
620+
subprocess.Popen([
621+
"openscad",
622+
"--preview",
623+
"-o", tmp_png.name,
624+
tmp.name
625+
]).communicate()
626+
627+
with open(tmp_png.name, "rb") as png:
628+
png_data = png.read()
629+
finally:
630+
os.unlink(tmp.name)
631+
os.unlink(tmp_png.name)
632+
633+
return png_data
634+
605635

606636
class IncludedOpenSCADObject(OpenSCADObject):
607637

0 commit comments

Comments
 (0)