Open WillAdams opened 23 hours ago
Fortunately this is an easy one. in pythontest.py you included openscad plain into context so you have to write: stock=cube(xxx)
or you can import as import openscad as os
hope this helps
Changing that to:
stock = cube(stocklength, stockwidth, stockthickness)
I still get:
ERROR: Traceback (most recent call last): File "
", line 15, in File "C:\Users\willa\AppData\Local\Programs\Python\Python311\Lib\pythontest.py", line 17, in psetupstock global generategcode ^^^^ NameError: name 'cube' is not defined
Is there a sample/template of a pair of .py file where one has global variables, and the other does branching/3D modeling based on those variables when called from the first file?
I think, that a typical object oriented approach would best fit your needs.
This will eliminate the need of any global variable and even gives you the feeling that you can work with the gcodewriter, not against. Actually I encourage, to use pythonscad in Python mode, because OO approach is another thing, which can barely be immitated in SCAD language.
pythontest.py
from openscad import *
class gcodewriter:
def __init__(self, basename = "", generategcode = False):
self.Base_filename = basename
self.generategcode = generategcode
def open(self):
self.f = open(self.Base_filename + ".nc", "w")
def writeln(self, *arguments):
line_to_write = ""
for element in arguments:
line_to_write += element
self.f.write(line_to_write)
self.f.write("\n")
def psetupstock(self, stocklength, stockwidth, stockthickness, zeroheight, stockorigin):
self.stock = cube([stocklength, stockwidth, stockthickness])
if self.generategcode == True:
self.writeln("(Design File: " + self.Base_filename + ")")
and test.py
from openscad import *
from pythontest import *
gw = gcodewriter("export",True)
gw.open()
gw.psetupstock(1,2,3,"Top","Center")
# Create a cube and a cylinder that overlap
cy = cylinder(15, center = True)
# Substract the cylinder from the cube
diff = gw.stock.difference(cy)
# Display the result
diff.show()
I get:
ERROR: Traceback (most recent call last): File "
", line 4, in NameError: name 'gcodewriter' is not defined
Duplicate of: https://old.reddit.com/r/pythonscad/comments/1fqzw2s/best_practices_for_global_variables_in/
Given a Python file, pythontest.py which contains:
and a test.py file which calls it as:
the result is:
What arrangement of commands and files would allow one to:
?