[Closed] if..do with colors
Hi,
I want to change the wire color of an object but only if the existing wire color is a certain color. I’m trying to do this with an if…do but it’s not working eg
if $object.wirecolor = blue do ($object.wirecolor = green)
Does anyone know how to get it to work?
Cheers
Garry
Hi Garry,
First of all, [i]if [/i]should be followed by [i]then[/i]. [edit] Or [i]do[/i], see discussion below.
Second, if you're comparing something, you should use "==".
And if you'd like to use the selected object, you can use $.
So in this case, it would be:
[i]if $.wirecolor == blue then $.wirecolor = green[/i]
This only works with the exact color codes (blue = a variable that actually contains (color 0 0 255)).
[edit] You probably want to loop this. In that case, you can use a for loop. In this case, the for loop would be something like this:
for i in $ where i.wirecolor == blue do i.wirecolor = green
This loops through all the objects in the selection that have a blue wirecolor, and changes it to green.
Good luck,
Martijn
Hi Garry,
The equivalence test is ==, while = is assignment.
The two things that come to mind are about selection and actual object color.
Unless your object name is “object”, you don’t get anything from scene with $object. Take a look at “Pathname Literals” in MaxScript Reference for a full explanation. If you want to cycle over every geometry object in the scene and modify its wirecolor if matches yours then you can use a for loop, like this
for item in objects do
(
if (item.wireColor == (color 0 0 255)) then
(
item.wireColor = (color 0 255 0)
)
)
Second issue I see is: unless you previously set manually wireColor to pure blue, or green, like expressed by literals “blue” and “green”, none of the standard wireColor would match since those pure color aren’t in the standard palette.
- Enrico
oops, late
Hi,
Thanks very much for the replies, I’m a bit new to Maxscript so things like the double == trip me up from time to time.
I’ve got it working great now.
Kind Regards
Garry
No it shouldn’t.
If you have no “Else” then you shouldn’t use a “Then”.
I’ve had a lot of problems with conditional statements with ‘hanging then”s.
Do ensures the conditional doesn’t continue waiting for an “else”.
I think that both are acceptable. Personally I like to use IF THEN ELSE when an ELSE statement is needed and IF DO when it’s not, as it’s easier for me to understand what SHOULD be happening when I’m debugging.
However the MAXScript coding standards on ScriptSpot say:
The conditional form of if/do should never be used in a MAXScript. While if/do is syntactically correct in MAXScript its use can cause problems when the script is edited later. If/do does not allow for an else clause. If there is an if/do conditional it is possible for a person editing the code later to add an else clause and then be presented with the cryptic error,
-- Syntax error: at else, expected <factor>
– In line: else
In extremely large scripts it could take quite some time to track down the cause of this error. Be safe, always use if/then. It doesnt require an else clause, but will use one if it is available.
So… I guess do whatever feels right… unless you are working for someone, then do what they tell you
Hmm interesting. I’m so used to if/then statements that I didn’t even know about if/do.
Gavin, can you show such a case? I use if then almost exclusively as well, even when there’s no else (I never know if I need an else later on… )… I’d like to see a case where this can cause problems, because until now I haven’t had any (that I know of!)…
Thanks,
-Johan
you should never get a ‘hanging then’ in local scopes*. However, in a global scope (e.g. the maxscript listener, or even in a new script)…
if (true) then ( print "HELLO" )
will never finish, as MaxScript expects an else()… there’s nothing to indicate that one wouldn’t follow at a later time.
- this, therefore, does work
(
if (true) then ( print "HELLO" )
)
As now it sees that the scope was closed and it has no reason to expect an else anymore.
It’s also much easier to use do() where you won’t be needing an else() for script legibility. If there’s a good chunk of code inside the then(), you’d have to collapse/scroll down to see the else() case. If you use do(), then you don’t ever have to bother scrolling down as no such case can exist ( “if (true) do (); else ()” throws an error )
Edit: turns out the word “follow” has 2 letters ‘l’; I knew that! But why does my Firefox think folow is a perfectly cromulent word? hmm.
Man… I’ve got alot of THENs to replace…
Interesting thread… Didn’t really think there was a practical difference… Learned something today! Now I’m going drinking…
What Richard said.
I also don’t buy that scriptspot FUD about Else/Do being indecypherable. Considering max now highlights the script line that failed it should be pretty easy.
I know I’ve made that very mistake many many times before Do() Else(). Never remember it causing me any confusion.
Alright, learned something to. Happily to say that none of my scripts work in global scope, but from now on I will try to use do(), I promise
Thanks!
-Johan