Python inside OpenSCAD

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Python inside OpenSCAD

Brad Pitcher

Hey guys,

I've hacked together a solution to use pyscad in the actual OpenSCAD editor, where you can use F5 to refresh and render:

http://www.aviary.com/creation?fguid=bd154028-71a3-11e0-867b-12313b101135

Sorry, I couldn't quickly come up with a simple example that couldn't be easily reproduced with .scad. Look how similar the syntax is though!
It just wraps the editor text in a python script which outputs the .scad script, and then compiles as normal. This can still be compatible with .scad script (the program just needs to be able to figure out which type of script is in the editor). Of course, this doesn't change one's ability to use the pyscad libraries independently of the OpenSCAD editor either.

Marius, are you open to having Python be another option for scripting in OpenSCAD? There is still a lot of cleanup that needs to be done, but maybe it could make it in the next major version.

Cheers,
Brad

Reply | Threaded
Open this post in threaded view
|

Re: Python inside OpenSCAD

Christopher Olah
Wow! Very cool!

I've been thinking of doing something similar with ruby, since it has
the nice DSL features. This is super awesome though! I'm very
impressed by how similar the syntax is.

On Thu, Apr 28, 2011 at 11:42 AM, Brad Pitcher <[hidden email]> wrote:

> Hey guys,
>
> I've hacked together a solution to use pyscad in the actual OpenSCAD editor,
> where you can use F5 to refresh and render:
>
> http://www.aviary.com/creation?fguid=bd154028-71a3-11e0-867b-12313b101135
>
> Sorry, I couldn't quickly come up with a simple example that couldn't be
> easily reproduced with .scad. Look how similar the syntax is though!
> It just wraps the editor text in a python script which outputs the .scad
> script, and then compiles as normal. This can still be compatible with .scad
> script (the program just needs to be able to figure out which type of script
> is in the editor). Of course, this doesn't change one's ability to use the
> pyscad libraries independently of the OpenSCAD editor either.
>
> Marius, are you open to having Python be another option for scripting in
> OpenSCAD? There is still a lot of cleanup that needs to be done, but maybe
> it could make it in the next major version.
>
> Cheers,
> Brad
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Python inside OpenSCAD

R. Mark Adams, PhD
In reply to this post by Brad Pitcher
This really rocks!  I love the idea of multiple scripting options in OpenSCAD!

Thanks!
-Mark

On Thu, Apr 28, 2011 at 11:42 AM, Brad Pitcher <[hidden email]> wrote:

> Hey guys,
>
> I've hacked together a solution to use pyscad in the actual OpenSCAD editor,
> where you can use F5 to refresh and render:
>
> http://www.aviary.com/creation?fguid=bd154028-71a3-11e0-867b-12313b101135
>
> Sorry, I couldn't quickly come up with a simple example that couldn't be
> easily reproduced with .scad. Look how similar the syntax is though!
> It just wraps the editor text in a python script which outputs the .scad
> script, and then compiles as normal. This can still be compatible with .scad
> script (the program just needs to be able to figure out which type of script
> is in the editor). Of course, this doesn't change one's ability to use the
> pyscad libraries independently of the OpenSCAD editor either.
>
> Marius, are you open to having Python be another option for scripting in
> OpenSCAD? There is still a lot of cleanup that needs to be done, but maybe
> it could make it in the next major version.
>
> Cheers,
> Brad
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
>
>



--
| R. Mark Adams, Ph.D.       |   "Information is light.      |
| Computational Biologist    |    Information in itself,     |
| http://www.epotential.com  |    about anything, is light." |
| [hidden email]     |       - Tom Stoppard          |

Reply | Threaded
Open this post in threaded view
|

Re: Python inside OpenSCAD

I.T. Daniher
Looks pretty shiny. Originally, Kevin and I were interested in making OpenSCAD editor-agnostic, allowing the use of anything from eclipse to vim without issue. That being said, it's probably a better feature-add than feature-fork and having Python syntax in the editor is probably what most people will use. Thanks!
--
Ian

On Thu, Apr 28, 2011 at 12:03, R. Mark Adams, PhD <[hidden email]> wrote:
This really rocks!  I love the idea of multiple scripting options in OpenSCAD!

Thanks!
-Mark

On Thu, Apr 28, 2011 at 11:42 AM, Brad Pitcher <[hidden email]> wrote:
> Hey guys,
>
> I've hacked together a solution to use pyscad in the actual OpenSCAD editor,
> where you can use F5 to refresh and render:
>
> http://www.aviary.com/creation?fguid=bd154028-71a3-11e0-867b-12313b101135
>
> Sorry, I couldn't quickly come up with a simple example that couldn't be
> easily reproduced with .scad. Look how similar the syntax is though!
> It just wraps the editor text in a python script which outputs the .scad
> script, and then compiles as normal. This can still be compatible with .scad
> script (the program just needs to be able to figure out which type of script
> is in the editor). Of course, this doesn't change one's ability to use the
> pyscad libraries independently of the OpenSCAD editor either.
>
> Marius, are you open to having Python be another option for scripting in
> OpenSCAD? There is still a lot of cleanup that needs to be done, but maybe
> it could make it in the next major version.
>
> Cheers,
> Brad
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
>
>



--
| R. Mark Adams, Ph.D.       |   "Information is light.      |
| Computational Biologist    |    Information in itself,     |
| http://www.epotential.com  |    about anything, is light." |
| [hidden email]     |       - Tom Stoppard          |
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://rocklinux.net/mailman/listinfo/openscad

Reply | Threaded
Open this post in threaded view
|

Re: Python inside OpenSCAD

Brad Pitcher
In reply to this post by Brad Pitcher
I'm glad there is some interest in the idea. The code is here: http://github.com/brad/pyscad. It's still kind of hacky/buggy and not for the faint of heart. For instance, every time I run it it segfaults the 7th time I hit F5. Every time! Think of it as a proof of concept, I just wanted to get the idea out there. Anyway, let me know if anyone has trouble getting it working.
I'm hoping this IDE integration can be part of pyscad actually, which is already on your list.
I love the idea of having a ruby bridge too. It would be ideal to have bridges for many popular interpreted languages.
-Brad


On Thu, Apr 28, 2011 at 1:12 PM, Evan Jones <[hidden email]> wrote:
Nice work, Brad!  I've been wanting to unite Python and the OpenSCAD IDE for a while.  Do you have code available? 

As Ian mentioned, a few of us have been working on other-language=> OpenSCAD connections for a while.  Ian's PySCAD approach is the deepest, and I think best in the long term, but there's also an existing Python => OpenSCAD translator, SolidPython, that's similar to what you've done. 

We've put a few of those projects together in the Github SolidCode group, at https://github.com/SolidCode.  I'd love to be able to include your IDE integration there too when the code is available. 

Christopher, I've been wondering about a Ruby => SCAD bridge, too.  My SolidPython code at https://github.com/SolidCode/SolidPython could be translated almost line-for-line into Ruby without too much trouble.  There's a big hash of OpenSCAD builtins and their arguments there that would save you some time making the translation. 

Evan



Date: Thu, 28 Apr 2011 08:42:13 -0700
From: Brad Pitcher <[hidden email]>
Subject: [OpenSCAD] Python inside OpenSCAD
To: [hidden email]
Message-ID: <[hidden email]>
Content-Type: text/plain; charset="iso-8859-1"


Hey guys,

I've hacked together a solution to use pyscad in the actual OpenSCAD editor,
where you can use F5 to refresh and render:

http://www.aviary.com/creation?fguid=bd154028-71a3-11e0-867b-12313b101135

Sorry, I couldn't quickly come up with a simple example that couldn't be
easily reproduced with .scad. Look how similar the syntax is though!
It just wraps the editor text in a python script which outputs the .scad
script, and then compiles as normal. This can still be compatible with .scad
script (the program just needs to be able to figure out which type of script
is in the editor). Of course, this doesn't change one's ability to use the
pyscad libraries independently of the OpenSCAD editor either.

Marius, are you open to having Python be another option for scripting in
OpenSCAD? There is still a lot of cleanup that needs to be done, but maybe
it could make it in the next major version.

Cheers,
Brad

Reply | Threaded
Open this post in threaded view
|

Re: Python inside OpenSCAD

Ralf Schlatterbeck
In reply to this post by Christopher Olah
On Thu, Apr 28, 2011 at 11:57:55AM -0400, Christopher Olah wrote:
> Wow! Very cool!
>
> I've been thinking of doing something similar with ruby, since it has
> the nice DSL features. This is super awesome though! I'm very
> impressed by how similar the syntax is.

Hmm, how about using a comment-line similar to UNIX script hash-tag
line, e.g.
#!scad
#!/usr/bin/python
#!/usr/bin/ruby
that way openscad can find out what external pre-renderer should be
called... this is similar to what the MoinMoin wiki uses for specifying
different renderers for different markup languages...

Ralf
--
Dr. Ralf Schlatterbeck                  Tel:   +43/2243/26465-16
Open Source Consulting                  www:   http://www.runtux.com
Reichergasse 131, A-3411 Weidling       email: [hidden email]
osAlliance member                       email: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Python inside OpenSCAD

kintel
Administrator
In reply to this post by Brad Pitcher
On Apr 28, 2011, at 11:42 AM, Brad Pitcher wrote:

> Marius, are you open to having Python be another option for scripting in OpenSCAD? There is still a lot of cleanup that needs to be done, but maybe it could make it in the next major version.
>
It's interesting to explore, so we should make it at least an experimental feature.

 -Marius


Reply | Threaded
Open this post in threaded view
|

Re: Python inside OpenSCAD

kintel
Administrator
In reply to this post by Ralf Schlatterbeck
On Apr 29, 2011, at 02:19 AM, Ralf Schlatterbeck wrote:

>
> Hmm, how about using a comment-line similar to UNIX script hash-tag
> line, e.g.
> #!scad
> #!/usr/bin/python
> #!/usr/bin/ruby

Interesting idea. A bit unix-centric, but it's pretty standard.
I'd to #!/usr/bin/env <python|ruby|scad> though.

Having a version indicator might also be cool.

 -Marius


Reply | Threaded
Open this post in threaded view
|

Re: Python inside OpenSCAD

chrysn
On Fri, Apr 29, 2011 at 09:00:56AM -0400, Marius Kintel wrote:
> Interesting idea. A bit unix-centric, but it's pretty standard.
> I'd to #!/usr/bin/env <python|ruby|scad> though.

if we use proper shebang lines, i'd try to go for "the full thing"
(i.e., making sure the program can be run independently). that would
mean an additional line for at least a 'from pyscad.direct_display import *'.

<futureproofing-mode>

reason for the direct_display stuff is that we could then hook into the
python exit routine and, if the thing is called on the command line,
offer a reasonable default behavior like showing a minimal openscad
instance.

a more complex future script could then still something like

>>> from pyscad.viewer import Viewer
>>> from pyscad.operators import *
>>>
>>> def build_complex_thing():
>>>     cube()
>>>
>>> if __name__ == "__main__":
>>>     v = Viewer()
>>>     v.add_object(build_complex_thing())
>>>     v.hook_key('s', lambda: v.save_screenshot())
>>>     v.set_camera_on_orbit()
>>>     v.run_standalone()
>>> elif __name__ == "__openscad__":
>>>     pyscad.viewer.main_instance.add_object(build_complex_thing())
and be ready for importing and stuff while a simple mode still is
possible without too much boilerplate code.

</futureproofing-mode>

i'm definitely going to have another look at this (after discarding
pyscad when i had my first look at it as it didn't help me with
introspecting the scene graph as far as i understood)

regards
chrysn

--
To use raw power is to make yourself infinitely vulnerable to greater powers.
  -- Bene Gesserit axiom

signature.asc (198 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Python inside OpenSCAD

Brad Pitcher
>
> if we use proper shebang lines, i'd try to go for "the full thing"
>
> (i.e., making sure the program can be run independently). that would
>
> mean an additional line for at least a 'from pyscad.direct_display import *'.
>

I was thinking more along the lines of keeping it simple by default (wrapping the extra Python on at compile time) to lower the barrier to entry for non-programmers. This way, the Python syntax used in OpenSCAD can be really close to the syntax of .scad script, but the full power of Python is still there. Of course, anyone with the skills and those who shun GUIs can continue using Python directly and thinking of OpenSCAD as more of a library.
Reply | Threaded
Open this post in threaded view
|

Re: Python inside OpenSCAD

chrysn
On Fri, Apr 29, 2011 at 04:13:13PM +0000, [hidden email] wrote:
> I was thinking more along the lines of keeping it simple by default
> (wrapping the extra Python on at compile time) to lower the barrier
> to entry for non-programmers. This way, the Python syntax used in
> OpenSCAD can be really close to the syntax of .scad script, but the
> full power of Python is still there. Of course, anyone with the
> skills and those who shun GUIs can continue using Python directly
> and thinking of OpenSCAD as more of a library.

we'd probably hide the shebang in the editor anwyay, what if we just hid
the one additional line together with it? thus, we can keep it as simple
for the "just trying it out for the first time" user, and as soon as he
un-ticks the "simple mode" checkbox in the edit menu, the import line
shows up (if he changes it or the file uses different imports, the
"simple mode" check box would just go gray)

what i'd like to avoid is a situation as we have it in arduino: there
are .pde files which are just like c++, but lack a single include line
and a main loop statement, and they are a PIT^Wrather unpleasant for
people who just want to use the gcc tool chain to work with. the same
thing could be achieved if the editor detected the typical shape of a
full program (in our case: a single 'from ... include *' line) and
switched to simple mode, and just as well allowed full-featured programs
to be edited, giving power users the possibility to easily work on
simple scripts as well.

chrysn

signature.asc (198 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Python inside OpenSCAD

Brad Pitcher
On Apr 29, 2011 9:45am, chrysn <[hidden email]> wrote:
> we'd probably hide the shebang in the editor anwyay, what if we just hid
> the one additional line together with it? thus, we can keep it as simple
> for the "just trying it out for the first time" user, and as soon as he
> un-ticks the "simple mode" checkbox in the edit menu, the import line
> shows up (if he changes it or the file uses different imports, the
> "simple mode" check box would just go gray)

I like it, but if the shebang is hidden how can the editor know what language is being used? Maybe an option in the menu that defaults to SCAD?
Reply | Threaded
Open this post in threaded view
|

Re: Python inside OpenSCAD

chrysn
On Fri, Apr 29, 2011 at 05:39:11PM +0000, [hidden email] wrote:
> I like it, but if the shebang is hidden how can the editor know what
> language is being used? Maybe an option in the menu that defaults to
> SCAD?

i'd just have options for file/new/{openscad, python, ...}, and a
setting for what openscad should open when starting, and an indicator
somewhere what language is currently used -- i don't think it's the
optimal solution, but it should work.

c

--
There's always a bigger fish.
  -- Qui-Gon Jinn

signature.asc (198 bytes) Download Attachment