discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Re: The nature of Python in OpenSCAD and future plans/features

WF
William F. Adams
Mon, Sep 11, 2023 11:09 AM

Well, it mostly seems to have been much easier than I'd thought it would be:
RapCAD has a command, "writeln" which takes a variable number of arguments and writes them out on a line --- this was easily done using a def'd function:
def writeln(*arguments):    line_to_write = ""    for number in arguments:        line_to_write += number    f.write(line_to_write)

save that to a file: gcodewrite.py
and use it with commands such as:
writeln("G0 X",str(w),"Y",str(d),"Z",str(h))

Which just leaves importing that, and opening/naming the file:
use < gcodewrite.py>
fn = "gcode.nc" f = open(fn, "w")

which seems simple enough, but I'd like to work up a way to wrap that in an if/then in an OpenSCAD file --- the obvious:
if (usepython == true) {use <gcodewrite.py>};
triggers the error:

ERROR: Parser error: syntax error in file gcodepreviewing.scad, line 3

Execution aborted

So, is there a way to conditionally execute such a command in an elegant fashion? I'm about to stuff it into a module, but I don't like that conceptually.
William
-- Sphinx of black quartz, judge my vow.https://designinto3d.com/

Well, it mostly seems to have been much easier than I'd thought it would be: RapCAD has a command, "writeln" which takes a variable number of arguments and writes them out on a line --- this was easily done using a def'd function: def writeln(*arguments):    line_to_write = ""    for number in arguments:        line_to_write += number    f.write(line_to_write) save that to a file: gcodewrite.py and use it with commands such as: writeln("G0 X",str(w),"Y",str(d),"Z",str(h)) Which just leaves importing that, and opening/naming the file: use < gcodewrite.py> fn = "gcode.nc" f = open(fn, "w") which seems simple enough, but I'd like to work up a way to wrap that in an if/then in an OpenSCAD file --- the obvious: if (usepython == true) {use <gcodewrite.py>}; triggers the error: ERROR: Parser error: syntax error in file gcodepreviewing.scad, line 3 Execution aborted So, is there a way to conditionally execute such a command in an elegant fashion? I'm about to stuff it into a module, but I don't like that conceptually. William -- Sphinx of black quartz, judge my vow.https://designinto3d.com/
WF
William F. Adams
Thu, Sep 14, 2023 2:17 AM

Slight adjustments:
file: "C:\Users\willa\OneDrive\Documents\OpenSCAD\libraries\gcodewrite.py"
def writeln(*arguments):    line_to_write = ""    for number in arguments:        line_to_write += number    f.write(line_to_write)
fn = "gcode.nc" f = open(fn, "w")
Not wild about hard-coding the filename, but it works.
In OpenSCAD Graph Editor under "Current Project Settings we make the preamble to be:
//!OpenSCAD
use <gcodewrite.py>
$fa = 4;$fs = 0.0625;

and we launch the Python-enabled version of OpenSCAD having it set to trust all Python files (yes, I know it'll be my fault if someone takes advantage of that, or if I manage to write a program which trashes my system --- that's what backups are for).
I hope that Python support in OpenSCAD will be adjusted to make all this easier --- in particular, I'd like to be able to check a variable and based on it, use or not use the .py file in an OpenSCAD file.
Anyway, we're off to the races!

William
-- Sphinx of black quartz, judge my vow.https://designinto3d.com/

Slight adjustments: file: "C:\Users\willa\OneDrive\Documents\OpenSCAD\libraries\gcodewrite.py" def writeln(*arguments):    line_to_write = ""    for number in arguments:        line_to_write += number    f.write(line_to_write) fn = "gcode.nc" f = open(fn, "w") Not wild about hard-coding the filename, but it works. In OpenSCAD Graph Editor under "Current Project Settings we make the preamble to be: //!OpenSCAD use <gcodewrite.py> $fa = 4;$fs = 0.0625; and we launch the Python-enabled version of OpenSCAD having it set to trust all Python files (yes, I know it'll be my fault if someone takes advantage of that, or if I manage to write a program which trashes my system --- that's what backups are for). I hope that Python support in OpenSCAD will be adjusted to make all this easier --- in particular, I'd like to be able to check a variable and based on it, use or not use the .py file in an OpenSCAD file. Anyway, we're off to the races! William -- Sphinx of black quartz, judge my vow.https://designinto3d.com/
WF
William F. Adams
Fri, Sep 15, 2023 3:02 PM

Unfortunately, it seems that it isn't possible to call the defined writeln function as written from OpenSCAD, so this will have to wait for either an updating of the Python sub-system in OpenSCAD, or the Python sub-system supporting the Customizer which would then allow me to re-write the entire thing in Python.
Couldn't tell before because I hadn't toggled the "generategcode" Boolean --- when I did, OpenSCAD crashed, I suspect because of an endless report of errors at not being able to call the module.
All I want to do is write out text and variables as numbers from OpenSCAD modules --- why is this so hard?
William

On Wednesday, September 13, 2023 at 10:17:17 PM EDT, William F. Adams <willadams@aol.com> wrote:  

Slight adjustments:
file: "C:\Users\willa\OneDrive\Documents\OpenSCAD\libraries\gcodewrite.py"
def writeln(*arguments):    line_to_write = ""    for number in arguments:        line_to_write += number    f.write(line_to_write)
fn = "gcode.nc" f = open(fn, "w")
Not wild about hard-coding the filename, but it works.
In OpenSCAD Graph Editor under "Current Project Settings we make the preamble to be:
//!OpenSCAD
use <gcodewrite.py>
$fa = 4;$fs = 0.0625;

and we launch the Python-enabled version of OpenSCAD having it set to trust all Python files (yes, I know it'll be my fault if someone takes advantage of that, or if I manage to write a program which trashes my system --- that's what backups are for).
I hope that Python support in OpenSCAD will be adjusted to make all this easier --- in particular, I'd like to be able to check a variable and based on it, use or not use the .py file in an OpenSCAD file.
Anyway, we're off to the races!

William
-- Sphinx of black quartz, judge my vow.https://designinto3d.com/

Unfortunately, it seems that it isn't possible to call the defined writeln function as written from OpenSCAD, so this will have to wait for either an updating of the Python sub-system in OpenSCAD, or the Python sub-system supporting the Customizer which would then allow me to re-write the entire thing in Python. Couldn't tell before because I hadn't toggled the "generategcode" Boolean --- when I did, OpenSCAD crashed, I suspect because of an endless report of errors at not being able to call the module. All I want to do is write out text and variables as numbers from OpenSCAD modules --- why is this so hard? William On Wednesday, September 13, 2023 at 10:17:17 PM EDT, William F. Adams <willadams@aol.com> wrote: Slight adjustments: file: "C:\Users\willa\OneDrive\Documents\OpenSCAD\libraries\gcodewrite.py" def writeln(*arguments):    line_to_write = ""    for number in arguments:        line_to_write += number    f.write(line_to_write) fn = "gcode.nc" f = open(fn, "w") Not wild about hard-coding the filename, but it works. In OpenSCAD Graph Editor under "Current Project Settings we make the preamble to be: //!OpenSCAD use <gcodewrite.py> $fa = 4;$fs = 0.0625; and we launch the Python-enabled version of OpenSCAD having it set to trust all Python files (yes, I know it'll be my fault if someone takes advantage of that, or if I manage to write a program which trashes my system --- that's what backups are for). I hope that Python support in OpenSCAD will be adjusted to make all this easier --- in particular, I'd like to be able to check a variable and based on it, use or not use the .py file in an OpenSCAD file. Anyway, we're off to the races! William -- Sphinx of black quartz, judge my vow.https://designinto3d.com/