Reuse scad code in python

69 views
Skip to first unread message

Matthieu Hendriks

unread,
Jun 5, 2025, 9:48:40 AMJun 5
to PythonSCAD
Is there a way to (re)use openscad code in a python program?

I tried the os_import no errors but after calling one of the modules openscad freezes until I killed it. I'm working on ubuntu linux.


Guenther Sohler

unread,
Jun 5, 2025, 9:52:31 AMJun 5
to Matthieu Hendriks, PythonSCAD
Just yesterday  fixed a memory corruption error, which  might have crashed pythonscad or not displaying any data.

Are you sure that  your scad model  will render a reasonable time, when  called from within OpenSCAD ?
Maybe you just share your code, then we can spot potential errors ?





On Thu, Jun 5, 2025 at 3:48 PM Matthieu Hendriks <matthieu...@gmail.com> wrote:
Is there a way to (re)use openscad code in a python program?

I tried the os_import no errors but after calling one of the modules openscad freezes until I killed it. I'm working on ubuntu linux.


--
You received this message because you are subscribed to the Google Groups "PythonSCAD" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonscad+...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/pythonscad/9671d487-c42c-45cb-8ee4-fa0552986416n%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Raymond West

unread,
Jun 5, 2025, 11:41:06 AMJun 5
to pytho...@googlegroups.com

Here's a simple OpenSCAD script.

module cu(){
   cube(5);
}
   
module sp(){
   difference (){
      cu();
      sphere(5);
   }
}

//cu();
sp();

if it is saved, as say "testimport.scad" what is the correct pythonscad script to import it and access either module?

Guenther Sohler

unread,
Jun 5, 2025, 2:39:45 PMJun 5
to Raymond West, pytho...@googlegroups.com
Hi,

This is a working example:

from openscad import *

testimport=osuse("testimport.scad")

inst1=testimport.cu()
inst2=testimport.sp()

#inst1.show()
inst2.show()


========
please use "osuse"  instead of osinclude, as you do not need the top level objects




W W

unread,
Jun 5, 2025, 3:13:06 PMJun 5
to PythonSCAD

Admittedly, I think these two functions deserve more "stage time", since importing existing scad files is likely a common occurrence. Especially for users coming from vanilla openscad.

@ray: Do you think osuse() would make the cut for your https://pythonscad.org/pythonscadhelper.html? I would like to vote yes :)

@guenther: I know there's the tutorial pages at https://pythonscad.org/tutorial/site/index.html. Is this hosting code on github? I think it's time it gets a deserving update to include things like osuse().

Side note:  I would go as far as tag osinclude() as deprecated due to how Python works. If a user _really_ wants to render items from a scad file as-is, they can always quickly wrap it in a module in vanilla scad and have pythonscad call the module as a function.

-W

Raymond West

unread,
Jun 5, 2025, 5:16:45 PMJun 5
to pytho...@googlegroups.com

Hi Guenther,

That works fine, couldn't find it on pythonscad.org

Best wishes,

Ray

Raymond West

unread,
Jun 6, 2025, 8:34:26 AMJun 6
to pytho...@googlegroups.com

Hi Willy,

On 05/06/2025 20:13, W W wrote:
@ray: Do you think osuse() would make the cut for your https://pythonscad.org/pythonscadhelper.html? I would like to vote yes :)

It's not mine any more. But, I suppose it may be worth while adding all the valid functions. The difficulty for me, and possibly ChatGPT would be in creating a succinct example for each. If I were to attempt that, perhaps you could check each addition, before it went live, so to speak?

If that was to happen, then maybe Guenther would host the much enlarged html file. The thing is that AI, if it doesn't know the exact answer, tends to make stuff up, instead of really searching out the truth, or admitting it does not 'know' and my knowledge of PythonSCAD is such that I often can not tell the difference. 

I only refer to what is available on pythonscad.org, as I believe that that is where users will initially look, and personally, GitHub, Reddit and the like, I see as sources of secondary information.

Best wishes,

Ray


W W

unread,
Jun 6, 2025, 11:59:05 AMJun 6
to PythonSCAD

Hey Ray,


>  If I were to attempt that, perhaps you could check each addition, before it went live, so to speak?

Let's leverage this google group! I can help out some, but my availability is always inconsistent :)


>  The thing is that AI, if it doesn't know the exact answer, tends to make stuff up, instead of really searching out the truth, or admitting it does not 'know' and my knowledge of PythonSCAD is such that I often can not tell the difference. 

Oh yeah, LLMs/transformers are inherently probabilistic.

As for prior art: The github issues for https://github.com/gsohler/openscad/issues (old) and https://github.com/pythonscad/pythonscad/issues (new) should have a lot of repro scripts that can be sourced as examples.

For osuse(), I had https://github.com/gsohler/openscad/issues/70 for instance :)

-W

Raymond West

unread,
Jun 6, 2025, 5:52:49 PMJun 6
to W W, PythonSCAD

Hi,

It seems that the free AI's cannot handle larger files. I tried Claude this time, and it took a bit of prompting to create the attached, pythonscadhelpertest3.html which has not followed my initial instructions. I then tried getting it to use that result, since I'd got it to do a better search window, and merge it again. No luck, just said too large, start another chat. Annoyingly, it cannot access previous chats,  but why have a dog, and bark yourself.😇.

 If anyone could be arsed, it could be manually merged.

 I asked it -

I have a helper for pythondscad as an html file. I need to expand it to include more functions. If send the html file, and a list of functions, can you merge the functions in the same format, generate simple examples, etc, so that it looks like my original html. Pythonscadhelpertest3.html is the layout I want, with the fixed search. Pythonscadhelper.html has the level of detail I want in the examples - It should include print() for numeric output. The idea being that the user only needs copy the example into the editor and run it, no typing required. The text file is the additional functions to be included, either in new or existing sections. Make sure, by splitting the resultant html file into sections, that I get it all.

--
You received this message because you are subscribed to the Google Groups "PythonSCAD" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pythonscad+...@googlegroups.com.
pyhonscadhelpertest3.html
pyhonscadhelper.html
pythonscad functions.txt
Message has been deleted
Message has been deleted
Message has been deleted

W W

unread,
Jun 7, 2025, 1:28:54 PMJun 7
to PythonSCAD
Hey Ray,

Are you trying to have LLM update the html file on your behalf?

-W

W W

unread,
Jun 7, 2025, 1:28:54 PMJun 7
to PythonSCAD
Hey Ray,

You should be able to update the html without having a hard dependency on LLMs.

I would caution on relying LLMs as a critical dependency to update documentation, due to its probabilistic nature.

-W

On Friday, June 6, 2025 at 2:52:49 PM UTC-7 ray wrote:

W W

unread,
Jun 7, 2025, 1:28:54 PMJun 7
to PythonSCAD
Hey Ray,

Are you trying to use LLM to edit the html file?

On Friday, June 6, 2025 at 2:52:49 PM UTC-7 ray wrote:

W W

unread,
Jun 7, 2025, 1:44:12 PMJun 7
to PythonSCAD
Sorry for the spam; google group had been auto deleting replies.

You received this message because you are subscribed to a topic in the Google Groups "PythonSCAD" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pythonscad/I_2pVrkh7LU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pythonscad+...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/pythonscad/c844a4a3-f410-49c2-b201-bfa078084d23n%40googlegroups.com.

Raymond West

unread,
Jun 8, 2025, 7:27:21 AMJun 8
to W W, PythonSCAD

Hi,

After a lot of arguing with a few llm's (they  go awol if you ask why - when they say the list is complete, gives the list, then asks if you want it to add the missing parts), so I've had to cobble together the various results.

Anyway, I think this is all the functions, not sure about the section headings, nor the order, but as it is searchable, maybe not a problem. I've not checked all of it, it needs more eyeballs.

Not sure if there should be more detail, wrt openscad differences - degrees/radians, integers in for loops, PI /pi, True /true etc.

Maybe if you see a problem, you correct it, and perhaps in a week or two, Guenther will put it on the openscad.org site, to replace the existing.

I've found a few functions that for me need a more detailed explanation, both in how to use and the usefulness/purpose.

Best wishes,

Ray

pyhonscadhelper4.html

Guenther Sohler

unread,
Jun 8, 2025, 8:51:21 AMJun 8
to Raymond West, W W, PythonSCAD
Hi Raymond, it\s a great file because it comes with lots of additional useful function descriptions.

But at the same time it contains functions, which are just wrong like

bounding_box does not exist
nimport does not accept scad files
debug is present several times
set_defauts does not exist
Trignonometric functions are missing
... and many more

As previously stated, I will publish  once again this file to pythonscadhelper.html because of the previously stated reasons
(and it not was no great experience to me either, my mind is not compatible with creating copies)

But I am happy ro receive PR's , patch files or permission to "cherry pick "cool docs" from the given file


PS: I wished I had some access to openscad,org, but probably you wanted to write pythonscad.org ?




Raymond West

unread,
Jun 8, 2025, 12:07:05 PMJun 8
to PythonSCAD

On 08/06/2025 13:51, Guenther Sohler wrote:
Hi Raymond, it\s a great file because it comes with lots of additional useful function descriptions.

But at the same time it contains functions, which are just wrong like

bounding_box does not exist
nimport does not accept scad files
debug is present several times
set_defauts does not exist
Trignonometric functions are missing
... and many more

As previously stated, I will publish  once again this file to pythonscadhelper.html because of the previously stated reasons
(and it not was no great experience to me either, my mind is not compatible with creating copies)

But I am happy ro receive PR's , patch files or permission to "cherry pick "cool docs" from the given file


PS: I wished I had some access to openscad,org, but probably you wanted to write pythonscad.org ?

Yeh, I'm seeing treble, from trying manually to merge three html files into one. Anyway, you've got the bones of it, alter it as you wish. It needs going through in detail as mentioned

I've not checked all of it, it needs more eyeballs.
I also mentioned it may be worth while drawing out the differences between pythonscad and openscad (True/true, etc)

I noticed you'd improved the initial splash screen. It now lists and I can select and open either python or scad, without having to preselect. I think the scad examples in that pane, should be replaced with .py examples, but I've no idea if that example pane is ever used... However, when in the editor, and I select 'new', it defaults to scad. In the console, it displays links and licencing wrt openscad,  maybe also you need to change/add to that. Then, animate it and get it to play music and recite poetry....😂

W W

unread,
Jun 8, 2025, 1:29:58 PMJun 8
to PythonSCAD
Assuming the goal is leveraging LLMs to help use PythonSCAD...

May I offer an alternative take?

I have some success testing Google's NotebookLM sourcing https://www.reddit.com/r/OpenPythonSCAD/wiki/index as data source. 

https://notebooklm.google.com/notebook/1c41ffbd-069b-4d28-9b89-26d94251be06 as a quick example testing if LLM can dig through that wiki page for exporting solid as a named STL (the notebook should be openable by anyone with link. Standard Google permission system).

If my understanding of the ultimate goal is sound, this alternative is:
  • Update the single page wiki on reddit (or anywhere that is simple text). Expand with code snippets to make it richer.
  • Use NotebookLM (or any RAG based LLM application of user's choice). It can be suggested on the wiki itself.
  • PythonSCAD users can interrogate LLM for usage examples and apply them to their code.
This inverts the idea of using LLM to populate documentation. Instead, the documentation can be curated by humans for corrective and style and have LLM map users' intent to corresponding PythonSCAD functions that _may_ work for them. 

This shifts the probability risk to the user to verify if the suggested PythonSCAD function works for their intent or not.

Thoughts?
-W

W W

unread,
Jun 8, 2025, 1:31:12 PMJun 8
to PythonSCAD
In case folks dont want to "login with Google" to view the NotebookLM, this is the quick snippet NotebookLM can provide:

Q: Is there a function for exporting each solid to a specific STL name?

A:

Based on the sources, OpenPythonSCAD does have a function for exporting objects to files where you can specify the filename and the format is detected from the suffix, such as .stl
.
The command is export(obj:PyOpenSCAD, file:str)
. This function exports the result to a file, and the file format is automatically detected based on the file name suffix
.
To export each solid to a specific STL name, you would likely use the export() function in conjunction with the separate() function
. The separate() function splits an object into a list of geometrically distinct objects. You could then iterate through the list of objects returned by separate() and use the export() command on each individual object, providing a unique filename for each one
.
For example, if you had a single OpenPythonSCAD object combined_solids that contained multiple distinct parts, you could potentially do something like:
1.
Separate the combined object: list_of_solids = separate(combined_solids)
.
2.
Loop through the list and export each solid: for i, solid in enumerate(list_of_solids): export(solid, f"solid_{i}.stl") (Note: this specific loop structure is based on general Python practice and is not directly shown in the sources, but the necessary functions separate and export are provided
).
Reply all
Reply to author
Forward
0 new messages