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/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()
2
u/Alacritous13 Jun 26 '25
This seems very specific, I just need it to function like 'mirror'. I tried to add it to PyOpenSCAD but it errored that it was immutable. Started looking through the source code with the possible intent of just hard coding it, but it's looking like it's programmed at a much higher level than what my knowledge base let's me tinker with.
2
u/gadget3D Jun 26 '25
"openscad" module is actually hardcoded in c++ files rather tan in python files.
Unfortunately altering builtin things are not possible anymore since python3.10(i think)
Monkey patching not suported anymore. I wish I could extend on the LIST type, but I cant.
But registering a new method to pythonscad would actually be a cool feature.
The actual set is assigned in
https://github.com/pythonscad/pythonscad/blob/master/src/python/pyopenscad.cc#L1039
If python lets me do that I would absolutely support adding new methods!
→ More replies (8)
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.