Skip to content

Commit 991029a

Browse files
Removed code duplication in scad_render_to_file() and scad_render_animated_file
1 parent feed5ba commit 991029a

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

solid/solidpython.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ def scad_render( scad_object, file_header=''):
206206
includes = ''.join(include_strings) + "\n"
207207
scad_body = root._render()
208208
return file_header + includes + scad_body
209-
210-
def scad_render_animated_file( func_to_animate, steps=20, back_and_forth=True, filepath=None, file_header='', include_orig_code=True):
209+
210+
def scad_render_animated(func_to_animate, steps=20, back_and_forth=True, filepath=None, file_header=''):
211211
# func_to_animate takes a single float argument, _time in [0, 1), and
212212
# returns an openscad_object instance.
213213
#
@@ -264,30 +264,20 @@ def scad_render_animated_file( func_to_animate, steps=20, back_and_forth=True, f
264264
rendered_string += ( "if ($t >= %(time)s && $t < %(end_time)s){"
265265
" %(scad_str)s\n"
266266
"}\n"%vars())
267+
return rendered_string
267268

268-
# TODO: Remove code duplication from here to end of method: taken
269-
# from scad_render_to_file(). -ETJ 28 Mar 2013
270-
calling_file = os.path.abspath( calling_module().__file__)
271-
272-
if include_orig_code:
273-
rendered_string += sp_code_in_scad_comment( calling_file)
274-
275-
# This write is destructive, and ought to do some checks that the write
276-
# was successful.
277-
# If filepath isn't supplied, place a .scad file with the same name
278-
# as the calling module next to it
279-
if not filepath:
280-
filepath = os.path.splitext( calling_file)[0] + '.scad'
281-
282-
f = open( filepath,"w")
283-
f.write( rendered_string)
284-
f.close()
269+
def scad_render_animated_file( func_to_animate, steps=20, back_and_forth=True, filepath=None, file_header='', include_orig_code=True):
270+
rendered_string = scad_render_animated(func_to_animate, steps, back_and_forth, file_header)
271+
return _write_code_to_file(rendered_string, filepath, include_orig_code)
285272

286273
def scad_render_to_file( scad_object, filepath=None, file_header='', include_orig_code=True):
287274
rendered_string = scad_render( scad_object, file_header)
275+
return _write_code_to_file(rendered_string, filepath, include_orig_code)
276+
288277

278+
def _write_code_to_file(rendered_string, filepath=None, include_orig_code=True):
289279
try:
290-
calling_file = os.path.abspath( calling_module().__file__)
280+
calling_file = os.path.abspath( calling_module(stack_depth=3).__file__)
291281

292282
if include_orig_code:
293283
rendered_string += sp_code_in_scad_comment( calling_file)
@@ -308,7 +298,8 @@ def scad_render_to_file( scad_object, filepath=None, file_header='', include_ori
308298

309299
f = open( filepath,"w")
310300
f.write( rendered_string)
311-
f.close()
301+
f.close()
302+
return True
312303

313304
def sp_code_in_scad_comment( calling_file):
314305
# Once a SCAD file has been created, it's difficult to reconstruct

0 commit comments

Comments
 (0)