How to get OpenSCAD to create a 3D model from a list

8 views
Skip to first unread message

William Adams

unread,
Jul 10, 2025, 9:01:12 PMJul 10
to PythonSCAD
Given a Python file:

    import sys
    import math

    from openscad import *

    class gcodereview:

        def __init__(self):
            self.mc = "Initialized"
            self.mpx = float(0)
            self.mpy = float(0)
            self.mpz = float(0)
            self.tpz = float(0)
            self.currenttoolnum = 102
           
        def xpos(self):
            return self.mpx

        def ypos(self):
            return self.mpy

        def zpos(self):
            return self.mpz

        def setupstock(self, stockXwidth,
                     stockYheight,
                     stockZthickness,
                     zeroheight,
                     stockzero,
                     retractheight):
            self.toolpaths = []
            stock = cube([stockXwidth, stockYheight, stockZthickness])            
            self.stock = stock.translate([0,0,-stockZthickness])

        def rapid(self, ex, ey, ez):
            self.mpx = ex
            self.mpy = ey
            self.mpz = ez

        def xpos(self):
            return self.mpx

        def ypos(self):
            return self.mpy

        def zpos(self):
            return self.mpz
           
        def setxpos(self, ex):
            self.mpx = ex

        def setypos(self, ey):
            self.mpy = ey

        def setzpos(self, ez):
            self.mpz = ez
           
        def cutline(self, ex, ey, ez):
            tool = cylinder(d = 1.5875, h = 12)
            tpb = tool.translate([self.xpos(), self.ypos(), self.zpos()])
            tpe = tool.translate([ex, ey, ez])
            toolpath = hull(tpb, tpe)
            self.mpx = ex
            self.mpy = ey
            self.mpz = ez
            self.toolpaths.append(toolpath)

        def stockandtoolpaths(self):
            part = self.stock.difference(self.toolpaths)
            show(part)

which when used with a small Python file:

    from openscad import *

    from gcodereview import *

    gcp = gcodereview()

    gcp.setupstock(100, 100, 5, "Top", "Lower-Left", 8.5)

    gcp.rapid(50,50,0)

    gcp.cutline(100,100,-5)

    gcp.stockandtoolpaths()

Generates a 3D model as expected:

gcr_template.png

and if one sets up an OpenSCAD Library:

    use <gcodereview.py>

    module setupstock(stockXwidth, stockYheight, stockZthickness, zeroheight, stockzero, retractheight) {
        gcp.setupstock(stockXwidth, stockYheight, stockZthickness, zeroheight, stockzero, retractheight);
    }

    module rapid(ex, ey, ez){
        gcp.rapid(ex,ey,ez);
    }
           
    module cutline(ex, ey, ez){
        gcp.cutline(ex, ey, ez);
    }

    module stockandtoolpaths(){
        gcp.stockandtoolpaths();
    }

which calls the same commands:

    use <gcodereview.py>
    include <gcodereview.scad>

    gcp = gcodereview();

    setupstock(100, 100, 5, "Top", "Lower-Left", 8.5);

    rapid(50,50,0);

    cutline(100,100,-5);

    stockandtoolpaths();

one gets:

>Compiling design (CSG Tree generation)...

>Rendering Polygon Mesh using Manifold...

>WARNING: No top level geometry to render


and the CSG tree is described as:

    group();

    group();

    group();

    group();


Is there something about calling the commands from OpenSCAD which prevents the list in a variable in a class not being used to generate a 3D model?


William




Guenther Sohler

unread,
Jul 11, 2025, 2:07:33 AMJul 11
to William Adams, PythonSCAD
  I think this is again with OpenSCAD inherited contexts
. You do all the stuff INSIDE the module and all things work fine INSIDE the module
, as long as you stay INSIDE the module.
Once you exit the module you see the unmodified context again

you can test my theory by coding all stuff plain and without modules ;)

--
You received this message because you are subscribed to the Google Groups "PythonSCAD" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonscad+...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/pythonscad/1e85c226-b936-4545-b883-41b4d417316an%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

William Adams

unread,
Jul 11, 2025, 7:22:43 AMJul 11
to PythonSCAD
On Friday, July 11, 2025 at 2:07:33 AM UTC-4 guenther wrote:
  I think this is again with OpenSCAD inherited contexts
. You do all the stuff INSIDE the module and all things work fine INSIDE the module
, as long as you stay INSIDE the module.
Once you exit the module you see the unmodified context again

you can test my theory by coding all stuff plain and without modules ;)

Yeah, things worked back when I did it thus --- but I've been told that things need to be in a class, and the class has to serve up lists, and then the lists combined with the stock as a single operation --- how do I reference doing that in Python from OpenSCAD code?

William
 

William Adams

unread,
Jul 11, 2025, 9:11:33 AMJul 11
to PythonSCAD
Cogitating on that a bit, we seem to have something of a dichotomy:

    def showstockandtoolpaths(self):
        part = self.stock.difference(self.toolpaths)
        return part

when called as:

gcp.showstockandtoolpaths()

doesn't show anything (I guess it would need to be wrapped up in a variable and then show() applied to it).

but when wrapped up in OpenSCAD:

module showstockandtoolpaths(){
    gcp.showstockandtoolpaths();
}

and called as:

showstockandtoolpaths();

then one gets the expected/desired result, so I guess we're back in business, and we can even keep the same command names, just add a second Python command and then use a bit of mis-direction in the OpenSCAD file.

Thanks!

William

William Adams

unread,
Jul 11, 2025, 9:20:09 AMJul 11
to PythonSCAD
Wrote up my understanding in the subreddit wiki as:

## return vs. show

Note that there are two distinct mechanisms for getting a 3D model:

 - Python requires that the 3D model be placed in a show() command which will then cause it to be parsed and provided to the program to be displayed as a result of that explicit command
 - OpenSCAD requires that the 3D model be returned to it so that it may then be displayed based on the request implicit in the return

Does that seem correct? 

Hopefully that being there will allow me to remember/look this up at need.

William

Matthieu Hendriks

unread,
Jul 11, 2025, 9:45:32 AMJul 11
to William Adams, PythonSCAD
I think, IMHO,  it's better to make a choice do PythonOpenScad or do Openscad

#
# functions
def createObject1() 
   object=cube(10)
  return object

def createObject2()
  ...
  return object
#
# main 
show([createObject1(),createObject2()])


Op vr 11 jul 2025 om 15:20 schreef William Adams <william.fra...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "PythonSCAD" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonscad+...@googlegroups.com.
Message has been deleted

William Adams

unread,
Jul 11, 2025, 6:34:45 PMJul 11
to PythonSCAD
I think having two separate Python defs and only wrapping one up in OpenSCAD will be clearer/cleaner/more straight-forward.

William
Reply all
Reply to author
Forward
0 new messages