SolidCode/SolidPython

Access $preview variable

occivink opened this issue · 8 comments

Hi,

I would like to access the OpenScad $preview variable (introduced in version 2019.05), so that I can use a lower number of segments during preview. I looked around in the SolidPython code, but as far as I can tell it is not exposed in any way.

Thanks in advance

It is not supported atm.

And the support is not trivial because the value of the $preview variable is unknown at python (SolidPython) runtime (because all the $openScadVariables are only evaluated in OpenSCAD render time, which is after the solid python runtime).

But, we spend some thoughts on how some kind of support for these features could look like.

You could have a look at #178, #181, #180, #183. Those issues point towards the direction on how it could "somehow" be possible to use the $preview variable. I guess it'll all fall back to the scad_inline idea from #178.

Another hint:

There's technically no way to get to something like:

if some_new_feature("$preview"):
    #blablub
else:
   #foobar

The path that could have success would be something like:

sphere(....,segments = scad_inline("$preview ? 8 : 64"))

(which might(!) work more or less out of the box if you grab scad_inline from #178)

from solid import *

class ConstantOpenSCADObject(IncludedOpenSCADObject):
    def __init__(self, code):
        self._get_include_path = lambda x : ''
        super().__init__("not reander able", {}, "blablub")
        self.include_string = ''
        self.code = " " + code + " "

    def _render(self, render_holes=42):
        return self.code
 
def scad_inline(code):
    return ConstantOpenSCADObject(code)

s = sphere(10, segments = scad_inline("$preview ? 4 : 16"))

scad_render_to_file(s)

This works (but scad_inline is really a hack).

Thanks for the clarifications. I now see that it is indeed tricky to solve properly due to the late (i.e. after compiling to openscad) evaluation of $preview. I would be happy with the proposed scad_inline solution.