Skip to content

Commit da58026

Browse files
committed
adding _repr_png_ for ipython
1 parent 3f53d5b commit da58026

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

solid/solidpython.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,34 @@ def __mul__(self, x):
602602
'''
603603
return intersection()(self, x)
604604

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

606634
class IncludedOpenSCADObject(OpenSCADObject):
607635

0 commit comments

Comments
 (0)