You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Calls to OpenSCAD code imported into SolidPython can only be used as CSG objects #111
When function is imported, especially function that renders into array, generated code is incorrect.
Example:
import file transform.scad which contain function
function translation(translation=[], x=0, y=0, z=0) = [
[1, 0, 0, is_undef(translation[0]) ? x : translation[0])],
[0, 1, 0, is_undef(translation[1]) ? x : translation[1])],
[0, 0, 1, is_undef(translation[2]) ? x : translation[2])],
[0, 0, 0, 1]
];
use transform scad:
c = cube([20, 20, 30])
t = translation(x=10, y=20, z=30)
d = multmatrix(t)(c)
print(scad_render(d))
Nope, not a regression. There's actually nothing in the code to use imported OpenSCAD code as parameters to functions (e.g. multmatrix( translation([xyz])(c) rather than members of the body of an OpenSCAD block, like union()(translation([x,y,z])). But... that's an artificial limitation. Working on a fix
changed the title [-]Functions are not rendered correctly[/-][+]Functions as arguments to OpenSCAD objects are not rendered correctly[/+]on Jul 6, 2020
This suggestion will work for a single OpenSCAD object, like n-e-g's original prompt. But if the argument to multmatrix() were a whole tree object, then you would need to have arbitrarily long OpenSCAD code (including blocks & semicolons) in the arguments to other OpenSCAD functions. I'm not convinced that's a feature that would add a lot to this system.
So, removing the semicolon from a single object works. Using the original example:
Assuming we could fix the indentation & internal semicolons, this still seems pretty convoluted to me. I'm inclined to close this as a wontfix, but maybe there are some reasons for this feature I haven't thought through all the way. Can you tell me why you'd find it useful to pass OpenSCAD objects as function arguments to others?
@etjones, here is my usecase:
BOSL library contains modules for gears. Now, as I make a couple of gears, I need to know how far away to position them. To achieve that, BOSL has a function pitch_radius which accepts the same agruments as gear module that creates a gear. So in OpenSCAD I write code like this:
and get 2 perfectly spaced gears (or, similarly, places to put axes). My problem here is having ugly copypaste of <gearX_parameters>. So, I turned to SolidPython.
I suspect this approach (module + some functions returning parameters of this module) may be widespread in existing OpenSCAD code as there is no way to get derived values (like "radius of a gear with X teeth and pitch of Y") without making such functions. And these functions will probably pretty often be used in transforms.
offtopic: I solved this problem for now by partially porting the OpenSCAD gear code to python and exposing the values as additional attributes of the OpenSCADObject instances I return. This way, however, is not really sustainable for everyone since such porting cound be a pretty annoying pain in the back.
Hmm. In both the original issue example and in yours, we're calling OpenSCAD code to return non-solid values, a matrix and a number respectively. It absolutely makes sense that we'd want to be able to use those pre-made calculations without having to re-write them in Python.
...Aaaand, I'm not really sure how to make that work just yet. Currently a line like:
since __add__() is overloaded to create a union() in SolidPython. We might be able to get around that if we allowed imported SCAD objects to be cast to floats, say. But sometimes a SCAD function returns a number, sometimes a data structure like the matrix in the original example, and sometimes a 3D OpenSCAD object like involute_gears.gear() does.
The assumption in SolidPython has always been that imported SCAD objects are CSG objects. This issue points out why that was a mistaken assumption. Hmm...
@etjones
Whoops, I accidentally made it even more complicated then I thought...
This case has a relatively easy workaround to avoid arithmetics: do 2 translate()s
My hack worked only for non-arithmetic case where gears were moved along different axes:
changed the title [-]Functions as arguments to OpenSCAD objects are not rendered correctly[/-][+]Calls to OpenSCAD code imported into SolidPython can only be used as CSG objects[/+]on Jul 6, 2020
I've released a fix for this issue, now available via Pip in SolidPython v1.0.3. The fix doesn't resolve some of the deeper issues I described above of composite objects all specified in arguments to some other object, but for most uses of "This calculation was done in OpenSCAD and I want to use it in SolidPython", this should take care of things. Sorry for the long, long wait
Activity
etjones commentedon Jun 3, 2019
Yep, that's definitely not right. I think this is a recent regression. Should be a quick fix.
etjones commentedon Jun 3, 2019
Nope, not a regression. There's actually nothing in the code to use imported OpenSCAD code as parameters to functions (e.g. multmatrix( translation([xyz])(c) rather than members of the body of an OpenSCAD block, like union()(translation([x,y,z])). But... that's an artificial limitation. Working on a fix
Equidamoid commentedon Jul 4, 2020
@etjones any news? Or maybe some a dev branch to play around with? =)
Equidamoid commentedon Jul 4, 2020
I took a shot at it and... removing one semicolon is hard.
If someone wants a quick and dirty hack, here you go:
Getting rid of
[:-1]the proper way is a bit too much for me right here right now :([-]Functions are not rendered correctly[/-][+]Functions as arguments to OpenSCAD objects are not rendered correctly[/+]etjones commentedon Jul 6, 2020
This suggestion will work for a single OpenSCAD object, like n-e-g's original prompt. But if the argument to
multmatrix()were a whole tree object, then you would need to have arbitrarily long OpenSCAD code (including blocks & semicolons) in the arguments to other OpenSCAD functions. I'm not convinced that's a feature that would add a lot to this system.So, removing the semicolon from a single object works. Using the original example:
would yield:
Despite some unclear indentation, this would work.
But if the transform we pass to multmatrix is more complex:
yields arbitrarily complex OpenSCAD code in the args to multmatrix:
Assuming we could fix the indentation & internal semicolons, this still seems pretty convoluted to me. I'm inclined to close this as a wontfix, but maybe there are some reasons for this feature I haven't thought through all the way. Can you tell me why you'd find it useful to pass OpenSCAD objects as function arguments to others?
Equidamoid commentedon Jul 6, 2020
@etjones, here is my usecase:
BOSL library contains modules for gears. Now, as I make a couple of gears, I need to know how far away to position them. To achieve that, BOSL has a function
pitch_radiuswhich accepts the same agruments asgearmodule that creates a gear. So in OpenSCAD I write code like this:and get 2 perfectly spaced gears (or, similarly, places to put axes). My problem here is having ugly copypaste of
<gearX_parameters>. So, I turned to SolidPython.Here this transforms to something like this:
, which doesn't work.
Equidamoid commentedon Jul 6, 2020
I suspect this approach (module + some functions returning parameters of this module) may be widespread in existing OpenSCAD code as there is no way to get derived values (like "radius of a gear with X teeth and pitch of Y") without making such functions. And these functions will probably pretty often be used in transforms.
offtopic: I solved this problem for now by partially porting the OpenSCAD gear code to python and exposing the values as additional attributes of the
OpenSCADObjectinstances I return. This way, however, is not really sustainable for everyone since such porting cound be a pretty annoying pain in the back.etjones commentedon Jul 6, 2020
Thanks for that! I think that's a strong argument in favor of solving this bug. I'll take a look at this today.
etjones commentedon Jul 6, 2020
Hmm. In both the original issue example and in yours, we're calling OpenSCAD code to return non-solid values, a matrix and a number respectively. It absolutely makes sense that we'd want to be able to use those pre-made calculations without having to re-write them in Python.
...Aaaand, I'm not really sure how to make that work just yet. Currently a line like:
yields this OpenSCAD:
since
__add__()is overloaded to create aunion()in SolidPython. We might be able to get around that if we allowed imported SCAD objects to be cast to floats, say. But sometimes a SCAD function returns a number, sometimes a data structure like the matrix in the original example, and sometimes a 3D OpenSCAD object likeinvolute_gears.gear()does.The assumption in SolidPython has always been that imported SCAD objects are CSG objects. This issue points out why that was a mistaken assumption. Hmm...
Equidamoid commentedon Jul 6, 2020
@etjones
Whoops, I accidentally made it even more complicated then I thought...
This case has a relatively easy workaround to avoid arithmetics: do 2
translate()sMy hack worked only for non-arithmetic case where gears were moved along different axes:
Doing proper expression handling feels like waaaay too much work (probably comparable to rewriting OpenSCAD to just use python directly :D)
[-]Functions as arguments to OpenSCAD objects are not rendered correctly[/-][+]Calls to OpenSCAD code imported into SolidPython can only be used as CSG objects[/+]Tests for accepting imported OpenSCAD calculations as arguments. This r…
Bumping to 1.0.3, which contains a fix for #111 & #157
etjones commentedon Nov 16, 2020
I've released a fix for this issue, now available via Pip in SolidPython v1.0.3. The fix doesn't resolve some of the deeper issues I described above of composite objects all specified in arguments to some other object, but for most uses of "This calculation was done in OpenSCAD and I want to use it in SolidPython", this should take care of things. Sorry for the long, long wait