[Closed] Float Script for Brake Lights along a path constraint – Ressurected
Hi,
I’ve been following the great assistance ZeBoxx2 gave a user a number of years ago who wanted to have materials switch when cars slowed down to simulate brake lights coming on. Like him and a lot of us, I’m not a seasoned coder. I mainly ‘Frankencode.’ But, I’m not timid about it.
I do need a bit of help though. The code the guys developed only works on polys in a group. I’m using Civil View to import cars onto VSim data. I have 1000’s of them. A Civil View requirement is that the meshes are ‘attached’ to form a single edit poly. This script does not work on ‘attached’ poly objects.
I was hoping someone could assist me to find some script that could dig in and find the ‘elements’ so that the script could run on them instead of individual edit polys within ‘group.’
Regards,
Rob
Here are the original threads:
http://forums.cgsociety.org/showthread.php?f=98&t=640351&highlight=brake+light
http://forums.cgsociety.org/showthread.php?t=640818
using of “at time” context in scripted controllers is a bad practice. here is an example how to avoid it:
SpeedTest = attributes SpeedTest
(
parameters params rollout:params
(
position_target type:#maxobject
speed type:#float ui:ui_speed
acceleration type:#float ui:ui_acceleration
brake_light type:#color ui:ui_brake_light
on brake_light get val do
(
if acceleration < 0 then red else black
)
)
rollout params "Speed Test"
(
spinner ui_speed "Speed: " range:[-1e9, 1e9, 0] fieldwidth:56 align:#right offset:[4,0]
spinner ui_acceleration "Acceleration: " range:[-1e9, 1e9, 0] fieldwidth:56 align:#right offset:[4,0]
colorpicker ui_brake_light "Brake Light: " fieldwidth:66 align:#right offset:[4,4]
)
)
(
/** create scene **/
--stopanimation()
delete objects
global car = dummy name:"car"
custattributes.add car SpeedTest baseobject:on
p = car.SpeedTest.position_target = car[#transform][#position]
v = car.SpeedTest.speed.controller = float_script()
v.addtarget "p0" p offset:-1f
v.addtarget "p1" p offset:0f
--v.setexpression "if (iscontroller p0) and (iscontroller p1) then (p0 - p1) else -1"
v.setexpression "(distance p0 p1)"
a = car.SpeedTest.acceleration.controller = float_script()
a.addtarget "p0" p offset:-1f
a.addtarget "p1" p offset:0f
a.addtarget "p2" p offset:1f
a.setexpression "(distance p2 p1) - (distance p0 p1)"
)
(
/** animate scene **/
select car
animate on
(
at time 20 move car [100,0,40]
at time 40 move car [40,0,0]
at time 80 move car [40,0,-40]
)
max modify mode
--playanimation()
)
Denis, thanks for looking at this. I need you to hold my hand just a bit.
Is this supposed to be a script that is complete and replaces what I posted or is this just one component to replace one of the time components?
I don’t know scripting enough yet (but learning) to know if what you sent me is generic or specific, that is, are there fields where I need to replace text? I assume I’ll replace anything that names the mesh I’m working on.
Can you give me a little more direction of what’s intended and how to implement what you sent?
I really appreciate this. It’s helping me claw my way to the surface.
EDIT: Sorry, I didn’t realize I hadn’t posted the code I was playing with.
braking = .96
theCar = $Sph1
PositionNext = at time (currentTime + 1) theCar.position.controller.percent
PositionCurrent = at time currentTime ( theCar.position.controller.percent )
PositionPrevious = at time (currentTime - 1) ( theCar.position.controller.percent )
travel = abs (PositionNext - PositionCurrent)
travelPrev = abs (PositionCurrent - PositionPrevious)
--ID = 1
if (travel == 0.001 ) then (ID = 2 ) -- standing still
--else (
else if ((travel / travelPrev) < braking) then ( ID = 2 ) -- braking
else( ID = 1 ) -- coasting
--)
This works pretty well but when the object ( Sph1) is sitting still, the lights don’t have a steady “on” state. The curve goes “on” and “off” and “on” and “off” every few frames. One post suggested this was a Max calculation error and that if I used ‘if (travel == 0.001 )’ instead of the original ‘if (travel == 0)’ that the blinking would cease. One user confirmed that it worked for him. However, it does not work for me. I can’t get the graph to stay linear and horizontal at the zero speed frames.
Graph Error
If you open the image you can see it better.
i’ve a mxs solution. as you can see in my snippet “Brake Light” changes the color to red before “slowing down”. the script check negative acceleration (braking) one frame ahead.
so to make it helpful to you we need to know what you want to ‘animate’ to show a ‘braking’ stage.
you can post a link to your scene and explain what should show ‘braking’. it makes the process easier
Here is what I’m doing. I have 1000’s of cars that will be controlled by VSim data from our Civil Engineers. I’m doing a simulation of a large intersection. I’d like to have brake lights work for the cars to add realism.
I found the original threads here from 2008 where two users were discussing how to do it. I downloaded his file and have been playing with it.
In the file I attached, ‘Line01’ is his rig. I’ve moved the tail lights off to the side to make them easier to select and diagnose but it works well except he too had the problem of the lights flashing when it was sitting with ‘Zero’ speed. Changing ‘if (travel ==0 )’ to ‘if (travel ==0.01 )’ did not work for me but was a suggestion in the original thread.
So, I made two new lines, ‘Line002’ and ‘Line003’ to experiment with. You can see that ‘Line002’ with just a sphere works OK but still flashes when stopped.
‘Line003’ represents where I actually need to get to. I’m using Civil View to place the cars and per Civil View, the car has to be “one” mesh that is attached. The original car on ‘Line01’ is a “Grouped” collection of polys. The script that works on it won’t work on the “Attached” mesh. I figured I’d deal with that issue later as I’ll need to find a way for the script to reach in and access the “elements” in an “Attached” poly.
But for now I’m experimenting. If I can get the sphere to behave properly and not “flash” when it has no velocity, then I’ll at least be making progress. All of my cars will ultimately be controlled by VSim traffic movement data so I won’t have the ability to manually control anything.
In the end, what I need to figure out is how to the Civil View cars with VSim traffic data driving them, into a scene and have the brake lights work.
what you have to do…
make a scene with any car (object) which has a ‘braking’ material (standard) applied.
i will make a script that connects ‘braking’ event to the material color. using naming and hierarchy convention we will be able to find and connect all cars in the scene following the same rule
I appreciate the help. I’ll do this tomorrow morning. I have to go into a meeting in 8-minutes.
Thank you…
Here is a file that is typical for what I’m trying to do. VSim data(.fzp file) was imported. It reated boxes that represent cars moving in a traffic circle simulation. I then used Civil View to substitute cars onto those boxes.
I deleted all but 2 cars but the two I left are travelling at a constant speed, then they slow down to enter the traffic circle. The mesh for the road is not there but you’ll see what’s going on beginning around frame 290.
The cars use the required Civil View material. I can add extra slots though. I now need to build a script that will have brake lights function per the previous discussion in this thread.
I really appreciate your willingness to assist me with this. I’m on a bit of a deadline. I plan to take a Maxscript course soon so my skills will be greatly improved. But, I told my team, when asked if I could get brake lights to work, yes. So now I’m committed. Anything you I can do with you to get this working will be appreciated.
https://nvision4d.box.com/s/cyew9urwzynjnmlkig0xq6q80mqulvxz
Here is a tool (not really a tool) that does do the job. After running the script you will see a dialog.
on “Setup Brake” button pressed it has to find all cars and their materials. Following some rules and conventions it sets up braking display behavior.
I’m using “Self Illumination Filter Color” to mask braking effect of “Rear Lights” material
SpeedTest = attributes SpeedTest attribID:#(0x123456, 0x111222)
(
parameters params rollout:params
(
brake_on_stop type:#boolean default:on animatable:off ui:ui_brake_on_stop
brake_threshold type:#float default:0.002 ui:ui_brake_threshold
slow_threshold type:#float default:0.02 ui:ui_slow_threshold
position_target type:#maxobject
speed type:#float ui:ui_speed
acceleration type:#float ui:ui_acceleration
braking type:#boolean ui:ui_braking
on braking get val do
(
(acceleration <= -brake_threshold)
)
brake_light type:#color animatable:on ui:ui_brake_light
on brake_light get val do
(
if braking or (brake_on_stop and speed <= slow_threshold) then red else black
)
)
rollout params "Speed Test"
(
checkbox ui_brake_on_stop "Brake On Stop" offset:[-4,0]
spinner ui_brake_threshold "Brake Threshold: " range:[0, 1e9, 0] type:#float scale:0.0001 fieldwidth:56 align:#right offset:[4,0]
spinner ui_slow_threshold "Slow Threshold: " range:[0, 1e9, 0] type:#float scale:0.0001 fieldwidth:56 align:#right offset:[4,0]
spinner ui_speed "Speed: " range:[-1e9, 1e9, 0] fieldwidth:56 align:#right offset:[4,10]
spinner ui_acceleration "Acceleration: " range:[-1e9, 1e9, 0] fieldwidth:56 align:#right offset:[4,0]
checkbox ui_braking across:2 offset:[-4,10]
colorpicker ui_brake_light "Brake Light: " fieldwidth:66 align:#right offset:[4,8]
)
)
try (destroydialog CarBrakeSetup) catch()
rollout CarBrakeSetup "CarBrake Setup by denisT" width:200
(
fn findCars pattern:"Traffic-???-???" =
(
for node in geometry where matchpattern node.name pattern:pattern collect node
)
fn findBrakeMaterial mat name:"Rear Lights" =
(
n = 0
if iskindof mat MultiMaterial do
(
for k=1 to mat.count while n == 0 where stricmp mat[k].name name == 0 do n = k
)
if n != 0 do mat[n]
)
fn bindSelfColorFilter mat target = if iskindof mat Arch___Design__mi do
(
mat.self_illum_on = on
c = mat.filter_color.controller = Point4_Script()
c.addtarget "light" target
c.setexpression "light"
c
)
fn deleteSpeedTestAttr node baseobject:on =
(
base = if baseobject then node.baseobject else node
num = 0
for k = base.custattributes.count to 1 by -1 where (attr = base.custattributes[k]) != undefined and attr.name == "SpeedTest" do
(
deleteitem base.custattributes k
num += 1
)
num
)
fn applySpeedTestAttr node data: baseobject:on =
(
custattributes.add node SpeedTest baseobject:baseobject
attr = node.SpeedTest
if data != unsupplied do
(
attr.brake_on_stop = data[1]
attr.brake_threshold = data[2]
attr.slow_threshold = data[3]
)
p = attr.position_target = node[#transform][#position]
v = attr.speed.controller = float_script()
v.addtarget "p0" p offset:-1f
v.addtarget "p1" p offset:0f
v.setexpression "(distance p0 p1)"
a = attr.acceleration.controller = float_script()
a.addtarget "p0" p offset:-1f
a.addtarget "p1" p offset:0f
a.addtarget "p2" p offset:1f
a.setexpression "(distance p2 p1) - (distance p0 p1)"
attr
)
group "Default Settings: "
(
checkbox ui_brake_on_stop "Brake On Stop" checked:on offset:[-4,0]
spinner ui_brake_threshold "Brake Threshold: " range:[0, 1e9, 0.002] type:#float scale:0.0001 fieldwidth:56 align:#right offset:[4,0]
spinner ui_slow_threshold "Slow Threshold: " range:[0, 1e9, 0.02] type:#float scale:0.0001 fieldwidth:56 align:#right offset:[4,0]
)
button setup_bt "Setup Brake" width:180 align:#center offset:[0,4]
group ""
(
label info_lb "" width:180 align:#center offset:[0,-4]
)
on setup_bt pressed do undo "Setup Brake" on
(
cars = findCars()
info_lb.text = cars.count as string + " cars found."
for car in cars do
(
deleteSpeedTestAttr car
attr = applySpeedTestAttr car data:#(ui_brake_on_stop.state, ui_brake_threshold.value, ui_slow_threshold.value)
if (mat = findBrakeMaterial car.mat) != undefined do bindSelfColorFilter mat attr[#brake_light]
)
cars
)
on CarBrakeSetup close do
(
)
on CarBrakeSetup open do
(
)
)
createdialog CarBrakeSetup
(everyone free to add any advanced features, save and load setting service, fancy UI, etc. )