Create and move object

Hello!

I have a simple but important question to ask you. I'm studying a bit Maxscrpit and I have this problem:

I have a rollout with a button called "create & Move". I press the button and I create a sphere called Ball_01 to position [0,0,0]. All Ok...
Now, press the button again, I want create a new sphere (Ball_02) to [50,0,0] units along the axis X from a Ball_01. Press the button again and I create new sphere (Ball_03) to [100,0,0] along the axis X from Ball_01 and so on...
Similar to the loop in MaxScript Help:
------------------------------------------------------------------------------------
for i = 1 to 5 do
(
box_copy = copy mybox
box_copy.pos = [i * 50, 0, 0]
)
------------------------------------------------------------------------------------

I write this simple example, but I do not know how to go:
------------------------------------------------------------------------------------
fn create Sp = (

s = Sphere()
s.name = uniquename "Ball_"

)--- end create _sphere

rollout test "Move Sphere"
(
button b "Create & Move"

on b pressed do create Sp
)
createdialog test
------------------------------------------------------------------------------------

How To?

Thank you for your cooperation :)

Ciao
Michele71

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Michele71's picture

Wow, I still have much to

Wow, I still have much to learn!
Thanks so much for your note Anubis! :)
Now I will take your example to other studies of scripts. Thanks again!!

Anubis's picture

Glad I helped. Allow me to

Glad I helped. Allow me to propose one improvement to the code.
In fact, because ".pos[1]" is sinunim to ".pos.x", ".pos[2]" is equal to ".pos.y", and ".pos[3]" is the same as ".pos.z", so you can use your radiobuttons index and...

-- this part of the code
if allBalls.count > 0 do
(
   if ra1.state == 1 then s.pos.x = allBalls[allBalls.count].pos.x + sp1.value
      else if ra1.state == 2 then s.pos.y = allBalls[allBalls.count].pos.y + sp1.value
         else if ra1.state == 3 then s.pos.z = allBalls[allBalls.count].pos.z + sp1.value
)
 
-- will become shorter like this
if allBalls.count > 0 do
(
   s.pos[ra1.state] = allBalls[allBalls.count].pos[ra1.state] + sp1.value
)
 
-- or even more shorter
if allBalls.count > 0 do
(
   s.pos[ra1.state] = allBalls.count * sp1.value
)

my recent MAXScripts RSS (archive here)

Michele71's picture

Yes Anubis, yuor explanation

Yes Anubis, yuor explanation is clear and linear.

In fact, I could not understand this part: allBalls[allBalls.count] but I now understand! I tried your advice and everything works great I have replaced the "50" with a .value so that I can used a spinner to move the ball in the axes and I also added one radiobuttons to choose the axes xy and z:

fn a b =(
  allBalls = $Ball_* as array
    s = Sphere name:(uniqueName "Ball_")
 )--end a b

rollout test "Move Sphere"
(
 radiobuttons ra1 "Choose Axis" labels:#("x","y","z")
button b "Create Object"
spinner sp1 "Move object = " range:[-1000,1000,0] type:#integer
   
 
on b pressed do
(
 a b
 
       if allBalls.count > 0 do
    (
 if ra1.state == 1 then s.pos.x = allBalls[allBalls.count].pos.x + sp1.value
  else if ra1.state == 2 then s.pos.y = allBalls[allBalls.count].pos.y + sp1.value
   else if ra1.state == 3 then s.pos.z = allBalls[allBalls.count].pos.z + sp1.value
   )
)
)
createdialog test

Truly a great big thank!! :)

Anubis's picture

Also you can use index

Also you can use index multyplayer as in the MaxScript Help example, ie:

-- you can replace this command
s.pos.x = allBalls[allBalls.count].pos.x + 50
-- with this one
s.pos.x = allBalls.count * 50

my recent MAXScripts RSS (archive here)

Anubis's picture

Well, at the beginning I

Well, at the beginning I just collect as array all objects which names start with "Ball_". Then create a new shpere with unique name "Ball_" and after that check if my array (allBalls) is not empty. So if its empty that mean this is the first sphere created and dont need to be moved by X. Else - this "allBalls[allBalls.count]" found last item in the array named allBalls and asign its position X + 50 to the new created sphere position X. I hope my explanation is clear :)

my recent MAXScripts RSS (archive here)

Michele71's picture

Anubis I want to give you a

Anubis I want to give you a big kiss!
And the right solution to my problem :)

If I am not disturbing you, can you give me an explanation of your solution. I want to understand...

A big thank!

Anubis's picture

try this: on b pressed do (

try this:

on b pressed do
(
    allBalls = $Ball_* as array
    s = Sphere name:(uniqueName "Ball_")
    if allBalls.count > 0 do
    (
        s.pos.x = allBalls[allBalls.count].pos.x + 50
    )
)

my recent MAXScripts RSS (archive here)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.