[Closed] Calculate distance of moving objects
Hi,
I have two teapots in my scene. When I move either one of them, the distance is being calculated and written into the label of the rollout.
Now, when I animate the teapots with AutoKey, the distance is not updated.
Can you help me out, because I’d like to get the distance also when my objects are animated.
Here is the code I have by now:
clearlistener()
try(destroyDialog roCalcDistance)
catch()
global roCalcDistance
rollout roCalcDistance "Calculate Distance" width:150
(
group "Display"
(
label lblDistance "Distance" style_sunkenedge:true width:120 height:19
)
)
createDialog roCalcDistance
when transform $Teapot002 changes do
(
roCalcDistance.lblDistance.text = (distance $Teapot001 $Teapot002) as string
)
when transform $Teapot001 changes do
(
roCalcDistance.lblDistance.text = (distance $Teapot002 $Teapot001) as string
)
Cheers,
Michael
why do you need the spinner being updated during the animation plays? is it just for reference or do you want to use it for anything else?
the timer will update the UI for you, but you have to understand that the having of all time active timer that ticks every millisecond (10, 50, 100) is very expensive.
the right way to have a spinner updated and redrawn is to connect it to a controller. in your case it can be float_script or float_expression.
Here is the code
try(destroyDialog roCalcDistance)
catch()
global roCalcDistance
rollout roCalcDistance "Calculate Distance" width:150
(
group "Display"
(
label lblDistance "Distance" style_sunkenedge:true width:120 height:19
timer the_clock interval:1
)
on the_clock tick do
(
roCalcDistance.lblDistance.text = (distance $Teapot001 $Teapot002) as string
)
)
createDialog roCalcDistance
Hi,
I didn’t think of using a timer. Going to try that.
Thank you very much!
Cheers,
Michael
Actually, updating the rollout is not mandatory. It was just to see an output.
I know that this will generate a heavy load, so I’d be glad to realize it with a callback or something that only updates when the objects are moved or animated.
The reason I want to do this is that I am writing a script which alters the near/far distance of my zdepth material according to the distance of two planes connected to the camera.
I tried it with a paramWire.connect but wasn’t very happy with that.
so the float_script or the float_expression (which i would use) connected to the spinner is the solution. being added to a controller as nodes, the two controlled nodes will automatically send all transform change messages to the controller, and update it and its linked spinner.
Thank you very much for your immediate reply, denisT. I’m going to have a look at this:
so the float_script or the float_expression (which i would use) connected to the spinner is the solution.
this snippet has to help:
try(destroydialog distanceMeter) catch()
rollout distanceMeter "Distance Meter" width:160
(
label distance_lb "Distance:" align:#right offset:[0,0] across:2
spinner distance_sp fieldwidth:64 range:[-1,1e9,0] type:#worldunits readonly:on align:#right offset:[9,0] enabled:off
fn setupController source: target: =
(
c = distance_sp.controller = createinstance float_expression
c.AddVectorNode "source" source
c.AddVectorNode "target" target
c.SetExpression "length(source-target)"
c
)
)
createdialog distanceMeter
(
delete objects
source = point name:"source" size:10 pos:[-10,0,0] wirecolor:green
target = point name:"target" size:10 pos:[10,0,0] wirecolor:orange
bar = cylinder name:"bar" height:0 radius:5 heightsegs:1 wirecolor:red
bar.height.controller = distanceMeter.setupController source:source target:target
)
Thank you very much, this is exactly what i needed!
Cheers,
Michael