Scripting Newbie: Scripts tests, questions, etc. (color by position, etc.)

Scripting Newbie: Scripts tests, questions, etc. (color by position, etc.)

Anonymous
Not applicable
422 Views
7 Replies
Message 1 of 8

Scripting Newbie: Scripts tests, questions, etc. (color by position, etc.)

Anonymous
Not applicable
Hello,

So here are some random tests I'm doing just trying to get a good grasp at scripting. Never programmed/scripted before, so it's a fun learning experience. :)

For one is to change the object's color (wire color) based on its position in world space.

for o in selection do
(
$.wirecolor = color (o.pos.x) (o.pos.y) (o.pos.z)
)


Currently it's changing the whole selected group to one color, so how do I get it to correctly loop through each individual object to do this instead of all the selection going to one color? Thanks :)

I'll go ahead with another test after I get this one solved. I'll try to solve it before anyone answers, but don't hesitate to answer this question. :)
0 Likes
423 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable
for o in selection do
(
o.wirecolor = color (o.pos.x) (o.pos.y) (o.pos.z)
)
0 Likes
Message 3 of 8

Anonymous
Not applicable
Ah, very simple solution. I was applying it to the whole selection instead of per object, as was the desired result. Thank you. :)
0 Likes
Message 4 of 8

Anonymous
Not applicable
For this test, next I am wanting to delete any object that has a wirecolor of green.

for o in objects where o.wirecolor == green do
(
delete
)


Now, if I do this, all I get is an "OK" from the MaxScript results window. I guess I need to "collect" the object first, then delete it, or is there something else I'm doing wrong?

Thanks :)

Fixed, I think:

Ok, this seems to work: :)
for o in objects where o.wirecolor == green do
(
delete o
)
0 Likes
Message 5 of 8

Anonymous
Not applicable
Ok, next one is selecting objects with no material (i.e.-undefined), but for some reason it's only selecting one object at a time, and it only selects the next when that one is deleted or changed.

So here's the code, that's only doing partially what is expected,

for o in objects where o.material == undefined do
(
select o
)
0 Likes
Message 6 of 8

Anonymous
Not applicable
Use selectMore() instead of select() , ie:
clearSelection() -- 1st clear current selection
for o in objects where o.material == undefined do
(
selectMore o
)
0 Likes
Message 7 of 8

Anonymous
Not applicable
Thanks!

So as you said, this does work,
clearSelection()
for o in objects where o.material == undefined do
(
selectMore o
)


And then after reading some through the MAXScript Help files, I came up with this quicker way, which is faster than the earlier way I was doing it (the code above took about 9 seconds on 950 teapots, and this code took about half a second) :)

clearSelection()
select (for o in objects where o.material == undefined collect o)
0 Likes
Message 8 of 8

Anonymous
Not applicable
Yep, optimization is important, congratulation!
0 Likes