ScriptSpot

Forum topic · General Scripting

change object properties depending on his moving

By poshlovsenaher · 2013-09-07

Description

First of all I want to apologize for my english, i'm not a native speaker.
I just started to learn maxscript and trying to do simplest things. Thing like that:
I have to objects (for example, sphere and a box). Sphere must change it's radius depending on it's position: if sphere.pos.x < box.pos.x, sphere radius must be equal 5. If sphere.pos.x > box.pos.x, sphere radius must be equal 10. This changes must be displayed in viewport while I move sphere or box in realtime.
Here my script at this moment:


clearlistener()
max select All
max delete
a = Sphere radius:7
b = box length:5 height:5 width:5 pos: [-10, 10, 0]

select a

if a.pos.x < b.pos.x then
(
a.radius = 5.0
)
else
(
a.radius = 10.0
)

select a

When I move objects, nothing happened. Tell me, please, what I missed? How code must execute conditional test when object moves?

Comments (8)

*

poshlovsenaher · 2013-09-09

It's me again. I tried to change sphere visibility now, instead of radius and add second dimension. I change a little a code above, so here it is:


delete objects
sph = Sphere radius:7 name:(uniquename "MySphere")
bx = box length:5 height:5 width:5 pos: [-10, 10, 0] name:(uniquename "MyTarget")


ctrl = sph.visibility.controller = float_script ()
ctrl.AddTarget "bxPosx" bx[3][1][1].track
ctrl.AddTarget "bxPosy" bx[3][1][2].track
ctrl.AddNode "sphObj" sph
ctrl.setExpression "if sphObj.pos.x <= bxPosx and sphObj.pos.y <= bxPosy then 1 else 0.1"

 
-- show text
unRegisterRedrawViewsCallback showRadius 
fn showRadius = if isvalidNode (sph = getNodeByName "MySphere001") do
(
	gw.setTransform (Matrix3 1)
	gw.text (sph.pos + [0,0,(sph.radius + 1)]) (sph.visibility as string) color:[0, 255, 255]
	gw.enlargeUpdateRect #whole
	gw.updateScreen()
)
registerRedrawViewsCallback showRadius
--to remove text from screen just uncomment and execute next line 
--unRegisterRedrawViewsCallback showRadius    

And, of course, it dodn't work. It works with sphere radius, but not visibility. I know that somehow visibility controller must be be bezier_float, but I completly don't know, where exactly i must to set it.

P.S. I know, it may be very stupid question, but I'm very beginner and my knowlege of english as poor as my programming skills, so it's hard to me to read maxhelp quickly, though I trying to do it.

barigazy · 2013-09-09

Visibility track only have range from 0 to 1.
Why do U need this?

Actually...

poshlovsenaher · 2013-09-09

my plan was to create a box. When some object (or objects) moved inside this box (I planned to use .min and .max properties), it must become invisible. When object moved out of the box, it must become visible again.
I didn't want to ask to solve exactly this problem at start, because I wanted to figure all out step by step, not to get simple answer at once.

poshlovsenaher · 2013-09-09

Nope, I cant't understand too many points in this code... Could you explain me this moment, please:

ctrl.AddTarget "bxPos" bx[3][1][1].track

As I understood, this line renurns box position, but why three numbers/indexes? [3] - it's mean position, right? and first [1] - is specified x-position, but the other [1]?

I asked you, because I want to make this script more complex in future. And now I would like to set a relationship between sphere parameters and it's position in relation to box in two or three dimensions.

I would be very grateful if you would find time to answer me.

poshlovsenaher · 2013-09-09

of course!
transform - position - X-axis!
thanks!

poshlovsenaher · 2013-09-07

Great thanks!!! Now I will analyse your code, it's always more easily, when you have an example.

barigazy · 2013-09-07

This is not easy task for beginers but anyway.
I will show you a simple code but you need to read more about controllers and oder stuffs in mxs help.
Also on this forum you can find some interestig threads about that. So


delete objects
sph = Sphere radius:7 name:(uniquename "MySphere")
bx = box length:5 height:5 width:5 pos: [-10, 10, 0] name:(uniquename "MyTarget")
ctrl = sph.radius.controller = float_script ()
ctrl.AddTarget "bxPos" bx[3][1][1].track
ctrl.AddNode "sphObj" sph
ctrl.setExpression "if sphObj.pos.x <= bxPos then 5 else 10"

-- show text
unRegisterRedrawViewsCallback showRadius 
fn showRadius = if isvalidNode (sph = getNodeByName "MySphere001") do
(
	gw.setTransform (Matrix3 1)
	gw.text (sph.pos + [0,0,(sph.radius + 1)]) (sph.radius as string) color:[0, 255, 255]
	gw.enlargeUpdateRect #whole
	gw.updateScreen()
)
registerRedrawViewsCallback showRadius
--to remove text from screen just uncomment and execute next line 
--unRegisterRedrawViewsCallback showRadius 

Move box object in X direction to see the result
Cheers!