discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

"if main" functionality?

JB
Jamie Bainbridge
Tue, Nov 28, 2017 11:28 PM

Is there a functionality similar to Python's 'if name ==
"main"' which will run only if you're in the actual file, and will
be ignored if the file is include'd from another?

I have this sort of file layout:


config.scad

// variables here

part1.scad

include <config.scad>
module part1() {
// part based on config variables
}

part2.scad

include <config.scad>
module part2() {
// another part based on config variables
}

render.scad

include <part1.scad>
include <part2.scad>
// the final "good" assembled view of the parts
transform([x,y,z]) rotate([a,b,c]) color("blue") part1();
transform([x,y,z]) color("red") part2();

I would like to see part1() rendered in the default position only when
I'm actually in part1.scad.

Once I am in render.scad, I don't want to see part1() rendered like
that, I only want to see the final "good" transformed/rotated/colored
instance of the part.

I tried a few methods like a "rendering = true/false" variable and
"use" instead of "include" but I couldn't find an automated method. I
suspect there is some solution using correct syntax which is not
immediately obvious to me.

Can this be done in some way?

Jamie

Is there a functionality similar to Python's 'if __name__ == "__main__"' which will run only if you're in the actual file, and will be ignored if the file is include'd from another? I have this sort of file layout: ---------------------------------------- ## config.scad // variables here ## part1.scad include <config.scad> module part1() { // part based on config variables } ## part2.scad include <config.scad> module part2() { // another part based on config variables } ## render.scad include <part1.scad> include <part2.scad> // the final "good" assembled view of the parts transform([x,y,z]) rotate([a,b,c]) color("blue") part1(); transform([x,y,z]) color("red") part2(); ---------------------------------------- I would like to see part1() rendered in the default position only when I'm actually in part1.scad. Once I am in render.scad, I don't want to see part1() rendered like that, I only want to see the final "good" transformed/rotated/colored instance of the part. I tried a few methods like a "rendering = true/false" variable and "use" instead of "include" but I couldn't find an automated method. I suspect there is some solution using correct syntax which is not immediately obvious to me. Can this be done in some way? Jamie
M
MichaelAtOz
Wed, Nov 29, 2017 1:54 AM

To clarify, you want to have a part1() and part2() call in each file
respectively, but don't want to have those calls made when render
include()'s them?

The use<> should have worked to do that.

Alternatively you could put this in render.scad ($variables are dynamic
scope)
$main=1;

Then in the part file
if ($main!=1) part1();

If you don't like the warning add $main=0; to the part file.


Admin - PM me if you need anything, or if I've done something stupid...

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”  Fight it! http://www.ourfairdeal.org/  time is running out!

Sent from: http://forum.openscad.org/

To clarify, you want to have a part1() and part2() call in each file respectively, but don't want to have those calls made when render include()'s them? The use<> should have worked to do that. Alternatively you could put this in render.scad ($variables are dynamic scope) $main=1; Then in the part file if ($main!=1) part1(); If you don't like the warning add $main=0; to the part file. ----- Admin - PM me if you need anything, or if I've done something stupid... Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above. The TPP is no simple “trade agreement.” Fight it! http://www.ourfairdeal.org/ time is running out! -- Sent from: http://forum.openscad.org/
MM
Michael Marx
Wed, Nov 29, 2017 2:14 AM

Actually there won't be a warning, unknown $variables don't get a warning...

-----Original Message-----
From: Discuss [mailto:discuss-bounces@lists.openscad.org] On Behalf Of MichaelAtOz
Sent: Wed, 29 Nov 2017 12:55
To: discuss@lists.openscad.org
Subject: Re: [OpenSCAD] "if main" functionality?

To clarify, you want to have a part1() and part2() call in each file
respectively, but don't want to have those calls made when render
include()'s them?

The use<> should have worked to do that.

Alternatively you could put this in render.scad ($variables are dynamic
scope)
$main=1;

Then in the part file
if ($main!=1) part1();

If you don't like the warning add $main=0; to the part file.


Admin - PM me if you need anything, or if I've done something stupid...

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent
possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple "trade agreement."  Fight it! http://www.ourfairdeal.org/  time is running
out!

Sent from: http://forum.openscad.org/


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

Actually there won't be a warning, unknown $variables don't get a warning... > -----Original Message----- > From: Discuss [mailto:discuss-bounces@lists.openscad.org] On Behalf Of MichaelAtOz > Sent: Wed, 29 Nov 2017 12:55 > To: discuss@lists.openscad.org > Subject: Re: [OpenSCAD] "if main" functionality? > > To clarify, you want to have a part1() and part2() call in each file > respectively, but don't want to have those calls made when render > include()'s them? > > The use<> should have worked to do that. > > Alternatively you could put this in render.scad ($variables are dynamic > scope) > $main=1; > > Then in the part file > if ($main!=1) part1(); > > If you don't like the warning add $main=0; to the part file. > > > > ----- > Admin - PM me if you need anything, or if I've done something stupid... > > Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent > possible under law, I have waived all copyright and related or neighbouring rights to this work. > Obviously inclusion of works of previous authors is not included in the above. > > The TPP is no simple "trade agreement." Fight it! http://www.ourfairdeal.org/ time is running > out! > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
JB
Jamie Bainbridge
Wed, Nov 29, 2017 3:29 AM

On 29 November 2017 at 11:54, MichaelAtOz oz.at.michael@gmail.com wrote:

To clarify, you want to have a part1() and part2() call in each file
respectively, but don't want to have those calls made when render
include()'s them?

The use<> should have worked to do that.

Ah, it actually does work. I couldn't reproduce the incorrect
behaviour when making a little reproducer from scratch. I reviewed my
part code and found I'd both use'd and include'd one of the part
files, and that was the part I was testing the render with. My
mistake. "use <part1.scad>" does exactly what I was after.

Alternatively you could put this in render.scad ($variables are dynamic
scope)
$main=1;

Then in the part file
if ($main!=1) part1();

This also works perfectly.

Thank you very much for your help!

On 29 November 2017 at 11:54, MichaelAtOz <oz.at.michael@gmail.com> wrote: > To clarify, you want to have a part1() and part2() call in each file > respectively, but don't want to have those calls made when render > include()'s them? > > The use<> should have worked to do that. Ah, it actually does work. I couldn't reproduce the incorrect behaviour when making a little reproducer from scratch. I reviewed my part code and found I'd both use'd and include'd one of the part files, and that was the part I was testing the render with. My mistake. "use <part1.scad>" does exactly what I was after. > Alternatively you could put this in render.scad ($variables are dynamic > scope) > $main=1; > > Then in the part file > if ($main!=1) part1(); This also works perfectly. Thank you very much for your help!