Hiya Folks…
Im doing some automation generating OpenSCAD files and I wanted to have my output .png objects colored based upon a value of another parameter… In my simplistic dream world, I would do like below and put the logic in the .scad file. I could also try and deal with another step in my templating where I output the “myvar” variable directly, but I would prefer not to for various reasons if it is indeed possible in .scad.
The ‘_myvar’ parameter may have values of 1, 2 and 3 for example…
Im trying to do something like below, but I clearly have scope issues and Im confused… Is there a way to accomplish this concept or am I barking up an empty tree?
_color = “black”;
if (_myvar == 1) _color = “red”;
else if (_myvar == 2) _color=”green”;
else if (_myvar == 3) _color = “blue”;
color(_color) cube();
NOTE: in this simple example, I would just put the ‘color(_color) cube()’ into the if/elseif blocks, but of course, in the real world, that is much more complex rendering so it doesn’t fit well inside of a large if/elseif tree.
Thank you…. Jess
_color =
(_myvar == 1) ? "red"
: (_myvar == 2) ? "green"
: (_myvar == 3) ? "blue"
: "black";
color(_color) cube();
On 23 January 2018 at 14:36, Jess Askey jess@askey.org wrote:
Hiya Folks…
Im doing some automation generating OpenSCAD files and I wanted to have my
output .png objects colored based upon a value of another parameter… In my
simplistic dream world, I would do like below and put the logic in the
.scad file. I could also try and deal with another step in my templating
where I output the “myvar” variable directly, but I would prefer not to for
various reasons if it is indeed possible in .scad.
The ‘_myvar’ parameter may have values of 1, 2 and 3 for example…
Im trying to do something like below, but I clearly have scope issues and
Im confused… Is there a way to accomplish this concept or am I barking up
an empty tree?
_color = “black”;
if (_myvar == 1) _color = “red”;
else if (_myvar == 2) _color=”green”;
else if (_myvar == 3) _color = “blue”;
color(_color) cube();
NOTE: in this simple example, I would just put the ‘color(_color) cube()’
into the if/elseif blocks, but of course, in the real world, that is much
more complex rendering so it doesn’t fit well inside of a large if/elseif
tree.
Thank you…. Jess
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
I would use array syntax, like this:
myvar = 1;
color_ = ["red", "green", "black"];
color(color_[myvar]) cube();
or if you want to use a more general conditional scheme, you can use the
conditional operator:
myvar = 1;
color_ = myvar==1?"red":myvar==2?"green": "black";
color(color_) cube();
The rule is: There are only constants in OpenSCAD. You can define, but not
alter the value of a constant.
--
Sent from: http://forum.openscad.org/
Thank you Doug and Rudolf... that was exactly what I was missing!
-----Original Message-----
From: Discuss [mailto:discuss-bounces@lists.openscad.org] On Behalf Of Parkinbot
Sent: Tuesday, January 23, 2018 12:56 PM
To: discuss@lists.openscad.org
Subject: Re: [OpenSCAD] Question on defining a 'color' variable
I would use array syntax, like this:
myvar = 1;
color_ = ["red", "green", "black"];
color(color_[myvar]) cube();
or if you want to use a more general conditional scheme, you can use the conditional operator:
myvar = 1;
color_ = myvar==1?"red":myvar==2?"green": "black";
color(color_) cube();
The rule is: There are only constants in OpenSCAD. You can define, but not alter the value of a constant.
--
Sent from: http://forum.openscad.org/
OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org