MAXScript Extensions > MAXScript Extensions for Particle Flow > Particle Flow Script Examples > Birth Scripts >

# Fragmentation Sample Script

| Particle Flow - Quick Navigation

Description:

The following example shows a sample Fragmentation script using the Parray particle system and the Mesher Compound object to generate object fragments on the fly.

Can Be Used With:

Should be used in a Birth_Script Action to generate fragment particles.

Effect:

In the Init handler, the script prepares the fragment source object, a PArray, a Mesher Compound and two Editable meshes to hold the fragments. It then assigns the PArray to the Mesher.

In the Proceed handler, the script checks whether the particle system time is at frame 0 and no particles exist. In this case it gets the mesh of the PArray from the Mesher, splits the mesh into elements, gives birth to new particles and assigns them the mesh and position of the fragment.

After the script has created the particles with the fragments, they can be controlled by any Particle Flow operators as usual.

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.useAge = true--enable the Age channel
    pCont.usePosition = true--enable the Position channel
    pCont.useSpeed = true--enable the Speed channel
    pCont.useShape = true--enable the Shape channel
    )

    --The Init handler is called on initialization.
    --This is where preparation works can be performed.
    --The parameter pCont passed to the handler
    --contains the Particle Container the script is applied to
    on Init pCont do
    (
    -- Create a geometry primitive to break into fragments.
    global bb = $Teapot_69e13b6c--a unique name
    -- Create a PArray particle system to create the fragments.
    global a = $PArray_36ce15a4--a unique name
    -- Create a Mesher Compound object to turn PArray into mesh.
    global mshr = $Mesher_23951616--a unique name
    -- Create a mesh to hold the total mesh.
    global totalMesh = $EMesh_5990501d--a unique name
    -- Create a mesh to hold separate fragments.
    global fragmentMesh = $EMesh_1589735c-- a unique name
    --Disable screen redraw to speed up execution
    withredrawoff(
    -- If the source object does not exist in the scene...
    if (bb == undefined) then (
    -- create a Teapot for fragmentation, give it a unique name
    bb = Teapot radius:20 smooth:on segs:6 body:on handle:on spout:on lid:on mapCoords:off
    bb.name ="Teapot_69e13b6c"
    hide bb-- hide the teapot
    )
    -- If the PArray object does not exist in the scene...
    if (a == undefined) then
    (
    -- Create and setup a PArray
    a = PArray()
    a.name ="PArray_36ce15a4"
    a.speed = 0
    a.emitter = b
    ba.particleType = 2
    a.viewtype = 2
    hide a
    )
    -- If the Mesher object does not exist in the scene...
    if (mshr == undefined) then (
    -- Create a Mesher Compound Object
    -- The Mesher gets mesh data out of the PArray
    mshr = mesher()
    mshr.name ="Mesher_23951616"
    mshr.pick = a-- Assign the Parray to the Mesher
    select mshr
    modPanel.addModToSelection (Edit_Mesh ()) ui:on
    clearSelection()
    hide mshr
    )

    -- If the total Emesh object does not exist in the scene...
    if(totalMesh == undefined)then(
    -- Create a new editable meshes with unique name
    totalMesh = editable_mesh()
    totalMesh.name ="EMesh_5990501d"
    hide totalMesh
    )
    -- If the fragments Emesh object does not exist in the scene...
    if(fragmentMesh == undefined)then(
    -- to create editable meshes our of Mesher
    fragmentMesh = editable_mesh()
    fragmentMesh.name ="EMesh_1589735c"
    hide fragmentMesh
    )
    )
    )
    --The Proceed handler contains the particle creating script

    --The parameter pCont passed to the handler

    --contains the Particle Container the script is applied to:

    on Proceed pCont do(
    -- Get the time range
    t1 = pCont.getTimeStart() as float
    t2 = pCont.getTimeEnd() as float
    -- Only if the current time is at moment 0...
    if((t1 <= 0) and (t2 >= 0))then(
    -- Initialize a counter
    i = 0
    -- If there are still no particles in the system
    if(pCont.NumParticles() <= 0)then
    (
    print("should be making fragments")
    -- You can modify the fragmentation parameters here:
    a.Fragment_Thickness = 1
    a.fragmentmethod = 1
    a.fragChunkMinimum = 202
    -- Get the complete mesh from the mesher
    totalMesh.mesh = mshr.mesh
    -- Create a bitarray with all faces set
    print("assigning particles")
    -- Repeat while there are still faces in the mesh
    while totalMesh.numfaces != 0 do
    (
    -- Get the mesh element the first face belongs to
    el = meshop.getelementsusingface totalMesh #{1}
    -- Detach the faces of the element
    tMesh = meshop.detachFaces totalMesh el delete:true asMesh:true
    -- Set the mesh of the fragment Emesh to the element
    fragmentMesh.mesh = tMesh
    -- Update the fragment Emesh
    update fragmentMesh
    -- Get the center of the fragment mesh
    meshCenter = fragmentMesh.center
    -- The original particle position is the center of the fragment
    particlePos = fragmentMesh.center
    -- Adjust vertex location to center around the chunk pivot
    for j in 1 to fragmentMesh.verts.count do
    (
    curVert = getVert fragmentMesh.mesh j
    curVert = curVert - meshCenter
    setVert fragmentMesh.mesh j curVert
    )
    -- Move the pivot of the fragment to the original center
    -- of the fragment
    fragmentMesh.pivot = meshCenter
    -- Give birth to a new particle
    pCont.AddParticle()
    -- Increase the counter
    i += 1
    -- Set the current particle index to the counter
    pCont.particleIndex = i
    -- Set particle time and age to 0 (newborn particle!)
    pCont.particleTime = 0
    pCont.particleAge = 0
    -- Set the particle’s position to the original fragment position
    pCont.particlePosition = particlePos
    -- Reset the speed to 0
    pCont.particleSpeed = [0,0,0]
    -- Set the shape of the particle to the mesh of the fragment
    pCont.particleShape = fragmentMesh.mesh
    )
    print("assigned particles "+ (pCont.NumParticles() as string))
    )
    )
    )

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

    )

#### See Also

- PArray : GeometryClass

- Mesher : GeometryClass

- Using MAXScript in Script Actions

- Particle Flow Sample Scripts