Skip to content

Commit d301b3f

Browse files
Reorganization to limit additions to global namespace to only SolidPython-related names. Fix for SolidCode#60
1 parent c50dbc2 commit d301b3f

File tree

5 files changed

+191
-192
lines changed

5 files changed

+191
-192
lines changed

solid/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Some __init__ magic so we can include all solidpython code with:
22
# from solid import *
33
# from solid.utils import *
4-
from .solidpython import *
4+
from .solidpython import scad_render, scad_render_to_file
5+
from .solidpython import scad_render_animated, scad_render_animated_file
56
from .objects import *

solid/objects.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Classes for OpenSCAD builtins
33
"""
44
from .solidpython import OpenSCADObject
5+
from .solidpython import IncludedOpenSCADObject
56

67
class polygon(OpenSCADObject):
78
'''
@@ -533,3 +534,73 @@ def __init__(self, n):
533534
class assign(OpenSCADObject):
534535
def __init__(self):
535536
OpenSCADObject.__init__(self, 'assign', {})
537+
538+
# ================================
539+
# = Modifier Convenience Methods =
540+
# ================================
541+
def debug(openscad_obj):
542+
openscad_obj.set_modifier("#")
543+
return openscad_obj
544+
545+
546+
def background(openscad_obj):
547+
openscad_obj.set_modifier("%")
548+
return openscad_obj
549+
550+
551+
def root(openscad_obj):
552+
openscad_obj.set_modifier("!")
553+
return openscad_obj
554+
555+
556+
def disable(openscad_obj):
557+
openscad_obj.set_modifier("*")
558+
return openscad_obj
559+
560+
561+
# ===============
562+
# = Including OpenSCAD code =
563+
# ===============
564+
565+
# use() & include() mimic OpenSCAD's use/include mechanics.
566+
# -- use() makes methods in scad_file_path.scad available to
567+
# be called.
568+
# --include() makes those methods available AND executes all code in
569+
# scad_file_path.scad, which may have side effects.
570+
# Unless you have a specific need, call use().
571+
def use(scad_file_path, use_not_include=True):
572+
'''
573+
Opens scad_file_path, parses it for all usable calls,
574+
and adds them to caller's namespace.
575+
'''
576+
# These functions in solidpython are used here and only here; don't pollute
577+
# the global namespace with them
578+
from .solidpython import extract_callable_signatures
579+
from .solidpython import new_openscad_class_str
580+
from .solidpython import calling_module
581+
582+
try:
583+
with open(scad_file_path) as module:
584+
contents = module.read()
585+
except Exception as e:
586+
raise Exception("Failed to import SCAD module '%(scad_file_path)s' "
587+
"with error: %(e)s " % vars())
588+
589+
# Once we have a list of all callables and arguments, dynamically
590+
# add OpenSCADObject subclasses for all callables to the calling module's
591+
# namespace.
592+
symbols_dicts = extract_callable_signatures(scad_file_path)
593+
594+
for sd in symbols_dicts:
595+
class_str = new_openscad_class_str(sd['name'], sd['args'], sd['kwargs'],
596+
scad_file_path, use_not_include)
597+
# If this is called from 'include', we have to look deeper in the stack
598+
# to find the right module to add the new class to.
599+
stack_depth = 2 if use_not_include else 3
600+
exec(class_str, calling_module(stack_depth).__dict__)
601+
602+
return True
603+
604+
605+
def include(scad_file_path):
606+
return use(scad_file_path, use_not_include=False)

solid/patch_euclid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import euclid3
22
from euclid3 import *
33

4-
from solid.utils import * # Only needed for EPSILON. Tacky.
4+
from solid.utils import EPSILON
55

66
# NOTE: The PyEuclid on PyPi doesn't include several elements added to
77
# the module as of 13 Feb 2013. Add them here until euclid supports them

0 commit comments

Comments
 (0)