[Closed] A condition for if value incresed
Hi guys , i created a script controller for Z position , i want to run script only when my object moves in +Z . i don`t want to use time (to check a value in previous frame and know it rise or not ) , because i need a interactive controller , in time u need to click on play button.
is there any solution ?
You can use the “timer” UI element which basically calls a function every x ticks. You can use this to “constantly” check for a change in any parameter you like, and it’s not based on keyframes. Of course, it’s not technically “constantly” because it’s just very high frequency, or to lower requirements you can decrease that time so that it only checks ever quarter of a second or even less. But if it’s something simple, it shouldn’t impact performance too much.
how can i get previous value in timer ? for example in time i can check value in previous frame (f – 1) , how can i check previous frame in timer ? can give me a maxscript syntax for that ?
thanks
How and when you save the timer value is up to you. If you don’t want to use frames at all, you can simply do something like…
old_val = $Box.pos -- initial state of the position of an object called "Box"
timer clock "testClock" interval:1000 --tick once a second
on clock tick do -- on every tick of the clock do...
(
new_val = $Box.pos -- the current value of Box's position
if new_val != old_val do -- checks to see if there's a difference - the first iteration of this won't have a difference, because you're setting both variables at the same time.
(
print "I love dalmatians" -- do whatever function you want
)
old_val = $Box.pos -- assign the current position of the box to the old_val variable so that next time in the loop it's being compared to the new position.
)
What about this as a starting point?
(
local obj = Box width:5 length:5 height:5
local def = attributes controls attribID:#(0x3141fde2, 0x68b73b77)
(
parameters main (prevPosZ type:#float)
)
obj.pos.controller = position_list()
obj.pos.controller.available.controller = position_XYZ()
obj.pos.controller.active = 2
local scriptPosCtrl = position_script()
custAttributes.add scriptPosCtrl def
scriptPosCtrl.prevPosZ = obj.pos.z
scriptPosCtrl.addTarget #posZ obj.pos.controller[2][#Z_Position]
scriptPosCtrl.script = "if posZ > this.prevPosZ do print \"Object moved in positive direction\"; this.prevPosZ = posZ; [0,0,0]"
obj.pos.controller[1].controller = scriptPosCtrl
)
I don’t use realtime controllers in this way, only frame-based ones, so I haven’t ever dug deep enough to know why it works when you click on the spinners/enter the value directly but not when you drag the spinners (it switches from reporting node position to reporting the starting position which throws it off) and how to work around that.
i changed if to this :
if posZ > this.prevPosZ then print "Object moved in positive direction" else "Object moved in negative direction"
but i got this message “Object moved in positive direction” when i moved my object in -Z
You’d do the same with timer, compare to previous and save current position as previous at timer tick. As for that, did you read the ending paragraph of my post? I don’t know why but when not changing the value in one step (one click on a spinner/entering a new value in the spinner etc.) the values oscillate between the starting position and current position – add some debug print to the controller to see it yourself – maybe there’s a simple workaround, maybe not, I’ve never needed something like this.
yes i read that , actually i want to try use relative position to check object move up or down , maybe its work.