Skip to content

Access $preview variable #195

Description

@occivink

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

Activity

  1. jeff-dh commented on Feb 19, 2022

    @jeff-dh
    Collaborator

    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.

  2. jeff-dh commented on Feb 19, 2022

    @jeff-dh
    Collaborator

    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)

  3. etjones commented on Feb 19, 2022

    @etjones
    Contributor
  4. jeff-dh commented on Feb 19, 2022

    @jeff-dh
    Collaborator
  5. jeff-dh commented on Feb 19, 2022

    @jeff-dh
    Collaborator
    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).

  6. jeff-dh commented on Feb 19, 2022

    @jeff-dh
    Collaborator
  7. occivink commented on Feb 20, 2022

    @occivink
    Author

    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.

  8. jeff-dh commented on Feb 20, 2022

    @jeff-dh
    Collaborator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions