[Closed] Maxscript Array / For Loop assistance
I’m new to maxscript and I’m just now learning the ins and outs of it. I have created a few things using the Maxscript Listener and that has helped out quite a bit. However what I’m learning now I think will involve arrays and for loops.
Here’s the scenerio –
I have a value (soon to be a spinner) for designating rows and columns. I want to create a cylinder, then create a grid of cylinders based off the rows and columns listed in the value at the beginning of the script.
Below is the code for this. Can someone look at what I have and tell me or show me a more efficient way to accomplish this?
fn myrows = 3
fn mycolumns = 4
-- Cylinder Creation
mycylinder = cylinder smooth:on heightsegs:1 capsegs:1 sides:32 height:1.8 radius:2.4 pos:[4,4,0]
select mycylinder
ConvertTo $ Editable_Poly
-- Arrays for holding studs/cylinders
tempcolstuds = #()
colstuds = #()
temprowstuds = #()
rowstuds = #()
-- Creates columns of cylinders across based off the numbers in mycolumns and attaches them
for i=1 to j=(mycolumns()-1) do
(
aocols =i*8
maxOps.CloneNodes mycylinder cloneType:#copy offset:[aorows,0,0] newNodes:&tempcolstuds
colstuds[i] = tempcolstuds[1]
)
for item in colstuds do
polyop.attach mycylinder item
-- Creates rows of cylinders from previous step based off the number in myrows and attaches them
for i=1 to i=(myrows()-1) do
(
aorows = i*8
maxOps.CloneNodes mycylinder cloneType:#copy offset:[0,aocols,0] newNodes:&temprowstuds
rowstuds[i] = temprowstuds[1]
)
for item in rowstuds do
polyop.attach mycylinder item
Thanks in advanced.
To create the array of nodes you could use something like this:
(
-- Delete all objects in scene
delete objects
-- Define some variables
local rows = 3 -- Amount of rows
local columns = 4 -- Amount of columns
local dist_x = 12 -- Space between nodes in X
local dist_y = 6 -- Space between nodes in Y
local offset = [0,0,0] -- The position of the first cylinder
-- Create a temporary base cylinder
local cyl = converttopoly (cylinder heightsegs:1 capsegs:1 sides:32 height:1.8 radius:2.4)
for y = 1 to rows do
(
for x = 1 to columns do
(
-- Calculate the X positoin of the node
pos_x = ((x-1) * dist_x) + offset.x
-- Calculate the Y positoin of the node
pos_y = ((y-1) * dist_y) + offset.y
-- Create the Cylinder in the calculated position
copy cyl pos:[pos_x, pos_y, 0]
)
)
-- Delete the temporary cylinder
delete cyl
)
To attach the nodes you can find a fast function in this thread “Cluster Attach (fast) Algorithm”
So I just tested it out. Thank You kind sir for showing me a better way to do this. It worked perfectly and has streamline and sped up my little script considerable.