Tips and Tricks for Working with Particles

This section is a grab-bag of useful tricks for working with particles. The following procedures are available:

• Associating a Particle Type to an Emitter

• Accessing Particle Clusters under a Particle Cloud

• Creating and Applying a Force to a Particle Cloud

• Creating and Applying an Obstacle to a Particle Cloud

• To Create and Apply a Goal to a Particle Cloud

Associating a Particle Type to an Emitter

If you set up a particle event on a ParticleType that was already attached to an emitter, you don’t have to worry about explicitly adding it to an emitter. However, you may have created a particle type independently of an emitter and now you need to find a way to associate it.

This is a fairly straightforward process in the UI: you can open the emission property and select the desired particle type from the ParType drop-down list. However, it is a more complicated process if you are doing it from scripting (which you probably are if you created your particle type using scripting).

1. Get a pointer to the target emitter property (see Accessing the Emitter Property).

2. Get a pointer to the emitter property’s particle type property (see Accessing the Particle Type Property under the Emitter Property).

3. Use the SetParticleType command with the following input parameter values:

- Target: Pass the pointer to the emitter property’s particle type property (the particle type to be changed).

- Source: Pass the pointer to the new particle type you want the emission property to use.

The following example demonstrates these steps. To see it in action, copy and paste it into the Script Editor and then click Run:

    // Set up an out-of-the-box cube particle cloud
    NewScene( null, false );
    CreateParticleCloud( "", "Cube" );

    // Start with the particle cloud and its primitive
    var pcloud = ActiveSceneRoot.FindChild( "", siCloudPrimType );
    var cprim = pcloud.ActivePrimitive;

    // Cruise through the operator stack to find the simulation op
    var op;
    var opstack = new Enumerator( cprim.ConstructionHistory );
    for ( ; !opstack.atEnd(); opstack.moveNext() ) {
           if ( opstack.item().Name == "ParticlesOp" ) {
           op = opstack.item();
           break;
       }
    }

    // Find the emission property under the simulation op
    var eprop;
    if ( op != null ) {
       for ( var i=0; i<op.InputPorts.Count; i++ ) {
           if ( op.InputPorts(i).Target2 != null
                  && op.InputPorts(i).Target2.Type == "EmissionProp" ) {
              // Get the EmissionProp nested under the particle operator
              // and exit the loop
              eprop = op.InputPorts(i).Target2;
           }
       }
    }

    // Get the old ptype from the emission property
    var ptype_old;
    rtn = eprop.NestedObjects;
    var e = new Enumerator( rtn );

    for ( ; !e.atEnd(); e.moveNext() ) {
       if ( e.item().Type == "ParType" ) {
           ptype_old = e.item();
           break;
       }
    }

    // You can create a new particle type and get a pointer to it
    var ptype_new = CreateParticleType( siSphereType )(0);

    // Now you can change the particle type
    SetParticleType( ptype_old, ptype_new );