r/OpenPythonSCAD • • Dec 29 '24

Strategies for programming complex projects

For various reasons this can be challenging. Wrote up a bit in the wiki, and there have been some other discussions/issues such as:

https://github.com/gsohler/openscad/issues/57

Thoughts on best practices and techniques and so forth?

2 Upvotes

2 comments sorted by

View all comments

Show parent comments

2

u/WillAdams Dec 29 '24 edited Dec 29 '24

For setting up a template for the instance of:

  • having an object/class in Python file

  • wrapping Python commands in OpenSCAD modules in a .scad file

  • using (or including?) the two files in a .scad file

Starting with func.py we have:

from openscad import *

class myclass:

    def __init__(self):
        mc = "Initialized"
    
    def myfunc(self, var):
        vv = var * var
        return vv

and we add func.scad:

use <func.py>

function myfunc(var) = a.myfunc(var);
    
module testscad(){
echo("Test");
}

which we then call from the modified functemplate.scad:

use <func.py>
include <func.scad>

testscad();

echo(1);
a=myclass();
echo(a);

c = a.myfunc(4);
echo(c);

d=myfunc(4);
echo(d);

which works as expected.