Script Operator Example

Description:

The following example shows the default Script Operator script with detailed comments.

Can Be Used With:

Used by the Script_Operator as default script.

Effect:

The script slows particles down, and when they are slow enough, splits off the first 50 particles into a stream traveling in a negative direction on the world X axis, and the remainder in the opposite direction.

EXAMPLE

--The ChannelsUsed handler defines the channels
--to be made available to the script.
--For the list of all channels, see
--Interface: MaxscriptParticleContainer
--The parameter pCont passed to the handler
--contains the Particle Container the script is applied to
on ChannelsUsed pCont do
(
pCont.useTime = true--enable the Time channel
pCont.useSpeed = true--enable the Speed channel
)

--The Init handler is called on initialization.
--It is not used in this case.
on Init pCont do
(
)

--The Proceed handler contains the main script
--applied to the Particles.
--The parameter pCont passed to the handler
--contains the Particle Container the script is applied to:
on Proceed pCont do
(
--Get the total number of particles:
count = pCont.NumParticles()
--Define slow speed as one unit per frame:
slowSpeed = 1/160.0
--Define very slow speed as 0.01 units per frame:
verySlowSpeed = 0.01/160.0
--Define random angular deviation per frame
divAngle = 12.0
--Get the start time in ticks
--(when conerting time values to float, the result is ticks)
tStart = pCont.getTimeStart() as float
--Get the end time in ticks
tEnd = pCont.getTimeEnd() as float
--Calculate the length of the time interval in ticks
timeDif = tEnd - tStart
--Then convert to frames (160 ticks/frame when using 30 fps NTSC)
timeDif /= 160.0
--Define a drag coefficient
dragCoef = 0.08


--Loop from 1 to the number of particles...
for i in 1 to count do
(
--Set the particle index in the container to the current i value.
--This makes the i-th particle in the container the current one:
pCont.particleIndex = i

--If the particle is faster, then slow it down
if length (pCont.particleSpeed) > slowSpeed then
pCont.particleSpeed -= dragCoef*timeDif*pCont.particleSpeed

--Calculate the length of the particle speed vector
newLen = length(pCont.particleSpeed)
--If the current speed is higher than the defined very slow speed
--then calculate a random divergence using the deviation angle.
--If it is slower (very slow!), then don’t use the angle but a
--fixed value.
if newLen > verySlowSpeed then
pCont.particleSpeed = pCont.randDivergeVector(pCont.particleSpeed)(divAngle/(160*newLen))
else
pCont.particleSpeed = pCont.randDivergeVector(pCont.particleSpeed)(180.0)

--If slow enough, the first 50 particles go to the left.
--The particles with ID above 50 go to the right.
if length(pCont.particleSpeed) < 1.5*slowSpeed then
(
if pCont.particleID < 50 then
pCont.particleSpeed += [-0.005*timeDif, 0, 0]
else
pCont.particleSpeed += [0.005*timeDif, 0, 0]
)
)
)

--The Release handler is used to do cleanup work.
--Not used in this case.
on Release pCont do
(
)