Newb loop question

Newb loop question

Anonymous
Not applicable
256 Views
3 Replies
Message 1 of 4

Newb loop question

Anonymous
Not applicable
Hi there!

I'm currently learning max and trying some loops for placement of objects. Currently I'm migrating from actionscript so I'm having a bit of trouble transferring the code.

Below is a script where I'm trying something as simple as creating uniquely named boxes but using the suffix wont work for me...


for i = 1 to 3 do
(
newBox = box()
)


I would like for the code to come out with 3 boxes with the instance names newBox1 newBox2 and newBox3 obviously.

Just wondering why this doesnt work?
0 Likes
257 Views
3 Replies
Replies (3)
Message 2 of 4

Steve_Curley
Mentor
Mentor
Firstly, it helps if we know which version of Max you're using - Maxscript differs considerably between versions (in terms of new features). Put your Max version (and brief system specs) in your sig, that way we always know which version to use.

The loop itself is ok, the problem is what you're doing inside the loop.

newBox

This would be an array variable, so each new box you create (or rather a reference to it) will be stored in that array. Nothing wrong with that if you need to loop through those boxes again, if not then a simple variable will suffice.

newBox = box()

You havent passed any parameters to the Box constructor, so Max will give each a unique name based on the object type, in this case a box, so you'll get "Box01", "Box02" etc. To override the prefix "Box" you need to create each name like so:-
box name:(uniquename "newBox") 

Alternatively, you could construct the name yourself based on the prefix and the value of the loop counter, but the above method is much easier and guarantees unique names.

( -- always keep your code properly "scoped" with parentheses
local newBox = #() -- pre-declaring variables can ve very important

for i = 1 to 3 do
(
newBox = box name:(uniquename "newBox")
)

-- other stuff here using the array, if necessary
)

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 3 of 4

Anonymous
Not applicable
Thanks for the detailed reply Steve, I'll give it another try and get back to you.

Cheers!
0 Likes
Message 4 of 4

Anonymous
Not applicable
I just finished the script I was working on. The problem I was having was that I couldnt get a ProBoolean to recognise an array so I had to make a loop adding a setOperandB so each new box created (which i put in an array) would be part of the boolean operation.

I think the way arrays work are a little more useful and important than the way they are in Actionscript so I'm still getting used to it.

Cheers
0 Likes