r/OpenPythonSCAD • u/Alacritous13 • Jun 26 '25
How do I add new processes in .Process notation?
I thought I had figured out the differences and capacitates of pythonSCAD well enough to move onto my first project with it, but this is proving incorrect. I need to rebuild my SCAD library before I can really do anything. I've got it imported using 'osuse', and so far everything is working fine. I have to give it a local definition, to avoid having to constantly call the overarching library, but this is a small issue.
I have this in a scad file named ALib:
module MirrorAnd(m=[1,0,0]){
children();
mirror(m)
children();
}
And I have this in a py file:
from openscad import *
ALib=osuse("ALib.scad")
MirrorAnd=ALib.MirrorAnd
c=cube(4);
c=c.translate([2,0,0])
c=MirrorAnd(c,[1,0,0])
c.show();
This works fine, I could alternatively rebuild the function as:
def MirrorAnd(self,vec):#{
return union(self,self.mirror(vec))
#}
My issue is that I want to use it in the format of c.MirroAnd(vec) instead of MirroAnd(c,vec). I've been playing around with this and have been unable to figure out how to do it. This isn't the only function I need to do this for, with other ones being much more integral to my code practices.
Edit: I've gotten some stuff to work using the forbiddenfruit library. Given the name, its quite apparent that this solution is of a dubious quality, but it works and thats all that matters.
2
u/rebuyer10110 Jun 26 '25
If I understanding you correctly: You want your module to be a method of built-in cube?
I dont think that's doable, other than doing Python monkey patching.
Your options are either
c=MirrorAnd(c,[1,0,0])or you build your own wrapper class like you mentioned.This is quite typical of object oriented programming paradigm. If you want the equivalent of C# extension methods, you can monkey patch cube class yourself (but it is typically...hacky).
Something like:
Cube.MirrorAnd = lambda selff, vec: MirrorAnd(selff, vec)(You will need to test it out).
And note that this is now prohibited for built-in python types. Because it goes against conventional paradigm for object oriented programming.
2
u/gadget3D Jun 26 '25
You could do an enclosing python class
class MySCAD:
def __init__(self):
self.obj=cube(1)
def show(self):
self.obj.show()
def mymirror(self):
dup = MySCAD()
dup.obj = self.obj.mirror([1,0,0])
return dup
a=MySCAD()
b=a.mymirror()
b.show()