"if main" functionality?

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

"if main" functionality?

Jamie Bainbridge
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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "if main" functionality?

MichaelAtOz
Administrator
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
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
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!
Reply | Threaded
Open this post in threaded view
|

Re: "if main" functionality?

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

> -----Original Message-----
> From: Discuss [mailto:[hidden email]] On Behalf Of MichaelAtOz
> Sent: Wed, 29 Nov 2017 12:55
> To: [hidden email]
> 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
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: "if main" functionality?

Jamie Bainbridge
In reply to this post by MichaelAtOz
On 29 November 2017 at 11:54, MichaelAtOz <[hidden email]> 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!

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org