Skip to content

Importing SolidPython

jeff edited this page Oct 7, 2023 · 6 revisions

SolidPython is a python library and as such it must be imported into a executable python script. This sounds straight forward, but in the history of SolidPython there have been several discussion about how to import SolidPython (SolidPython:#114, #44, #45). Therefor this page explains three different import styles of SolidPython and their pros and cons.

from solid2 import *

this is the "classic" SolidPython style (SolidPython:#114).

from solid2 import *

d = cube(1) - sphere(2)
d.save_as_scad()

The background of this style is the perspective, that SolidPython is kind of a OpenSCAD on steroids. SolidPython started -- and probably still is used quite a lot -- in small script files whos single purpose is to generate 3D models (like OpenSCAD). In this case, it's pretty handy to simply import everything from SolidPython into the global namespace and use all the SolidPython calls in a clean way without having any import overhead.

The drawback of this approach is that it contradicts with the standard python coding style. Furthermore if SolidPython is used in more complex environments with minimal SolidPython fractions it might not be what you want. The solid2 namespace does not collide with default __builtins__.

If you use bosl2 this style becomes critical, because the bosl2 extension contains identifiers which do collide with the default __builtins__ (#45). If you use this style with the bosl2 library, make sure to read #45 and be aware of it!

import solid2 as s

this is probably the style most corresponding to the python standard, but -- at least for me -- creates less readable and clean code.

import solid2 as s

d = s.cube(1) - s.sphere(2)
d.save_as_scad()

Pros: corresponding to python standard

Cons: you always have to type and read a redundant s.

from solid2 import ...

a tradeoff and an alternative -- but I think less convenient -- style is to explicitly import every single identifier you want to use.

from solid2 import cube, sphere

d = cube(1) - sphere(2)
d.save_as_scad()

Pros: you only import into the global namespace what you really need

Cons: import overhead

Clone this wiki locally